Advertisement
Tooster

floyd-warshall

Apr 30th, 2016
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #define __USE_CIN
  2. //   __USE_CIN or __USE_SCANF
  3. //   desynchronized <iostream> is faster than <cstdio> !!!
  4. using namespace std;
  5.  
  6. #include <vector>
  7. #include <set>
  8. #include <queue>
  9. #include <map>
  10.  
  11. #include <cmath>
  12. #include <algorithm>
  13. #include <utility>
  14. #include <functional>
  15. #include <string>
  16.  
  17. #ifdef __USE_CIN
  18. #include <iostream>
  19. #define INT(x) int x; cin>>x
  20. #define LL(x) long long x; cin>>x
  21. #define ULL(x) unsigned long long x; cin>>x
  22. #define STR(s) string (s); cin>>(s);
  23. #define nl cout << '\n';
  24.  
  25. #elif defined __USE_SCANF
  26. #include <cstdio>
  27. #define INT(x) int x; scanf("%d", &x)
  28. #define LL(x) long long x; scanf("%lld", &x)
  29. #define ULL(x) unsigned long long x; scanf("%llu", &x)
  30. #define STR(s, i) char *(s) = new char[(i)+1]; scanf("%s", (s));
  31. #define nl printf("\n");
  32. #elif !defined __USE_CIN && !defined __USE_SCANF
  33. #error Standard input flag not set.
  34. #endif
  35.  
  36. #define ll long long
  37. #define ull unsigned long long
  38. #define ui unsigned int
  39.  
  40. typedef pair<int, int> pi;
  41. typedef pair<ll, ll> pll;
  42. typedef pair< pair<int, int>, pair<int, int> > ppi;
  43. typedef vector< int > vi;
  44. typedef vector<bool > vb;
  45. typedef vector< long long > vll;
  46. typedef vector< pi > vpi;
  47. typedef vector<vector<int> > vvi;
  48. typedef vector< vector<long long> > vvll;
  49. typedef vector<vector< pi > > vvpi;
  50.  
  51. const int INT_INF = 2147483647;
  52. const ll LL_INF = 9223372036854775807;
  53.  
  54. #define fr(x,y) for(int (x)=0; (x)<(y); (x)++)
  55. #define fr1(x,y) for(int (x)=1; (x)<=(y); (x)++)
  56. #define rf(x,y) for(int (x)=(y); (x)>=0; (x)--)
  57. #define frl(x,y) for(ll (x)=0; (x)<(y); (x)++)
  58. #define zajmodulo(x,y) ((x)%(y)+(y))%(y)
  59. #define matrixINT(NAME,X,Y,VAR) vvi (NAME)((X), vi((Y), (VAR)))
  60. #define matrixLL(NAME,X,Y,VAR) vvll (NAME)((X), vl((Y), (VAR)))
  61.  
  62. pll EGCD(ll a, ll b) { if (b == 0) return pll(1, 0);    pll xy = EGCD(b, a%b);  return pll(xy.second, xy.first - (a / b)*xy.second); }
  63. //returni a(mod p) ale trzeba użyć zajmodulo(ECGD.f, ECGD.s)
  64. /*
  65.     .........................................
  66.     #########################################
  67.         _______              _
  68.        |__   __|            | |
  69.           | | ___   ___  ___| |_ ___ _ __
  70.           | |/ _ \ / _ \/ __| __/ _ \ '__|
  71.           | | (_) | (_) \__ \ ||  __/ |
  72.           |_|\___/ \___/|___/\__\___|_|
  73.  
  74.     #########################################
  75.     .........................................
  76. */
  77.  
  78. /*
  79.    --------
  80.     Floyd-Warshall for calculating shortest paths between each pair of nodes in an undirected graph;
  81.     input:
  82.     T - test cases
  83.     n m - nodes, edges
  84.     a b c - edge a-b and b-a with weight c
  85.     output:
  86.     matrix n x n with shortest paths - dist[i][j] = -1 if path i-j does not exists
  87.    --------
  88. */
  89. int n, m;
  90. vvll dist;
  91. const ll MX = 5000000000;
  92.  
  93. void pie(){
  94.     cin >> n >> m;
  95.     dist.resize(0);
  96.     dist.resize(n);
  97.     fr(i, n) {
  98.         dist[i].resize(n-i, MX);
  99.         dist[i][0] = 0;
  100.     }
  101.  
  102.     int a, b;
  103.     ll c;
  104.     fr(i, m) {
  105.         cin >> a >> b >> c; --a; --b;
  106.         dist[min(a, b)][max(a,b)-min(a,b)] = c;
  107.     }
  108.     fr(u, n){
  109.         fr(v1, n){ // v1 <= v2
  110.             for(int v2 = v1; v2 < n; v2++)
  111.                 if(dist[v1][v2-v1] > dist[min(v1, u)][max(v1, u) - min(v1, u)] +  dist[min(v2, u)][max(v2, u) - min(v2, u)])
  112.                     dist[v1][v2-v1] = dist[min(v1, u)][max(v1, u) - min(v1, u)] +  dist[min(v2, u)][max(v2, u) - min(v2, u)];
  113.         }
  114.     }
  115.  
  116.     fr(i, n) {
  117.         fr(j, n){
  118.             if (dist[min(i, j)][max(i, j) - min(i, j)] == MX) cout << "-1 ";
  119.             else cout << dist[min(i, j)][max(i, j) - min(i, j)] << " ";
  120.         }
  121.         nl;
  122.     }
  123.    
  124.     return;
  125. }
  126. /*
  127. *
  128. *
  129. *
  130. *
  131. *
  132. *
  133. *
  134. */
  135. int main() {
  136. #ifdef __USE_CIN
  137.     ios_base::sync_with_stdio(false);
  138. #endif
  139.     INT(T);
  140.     while(T--) pie();
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement