Guest User

Orcs

a guest
Nov 19th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f(i,a,b) for(int i = (a); i <= (b); i++)
  3. #define SZ(x) ((int)x.size())
  4.  
  5. const int INF = 1000000002;
  6. int D[105][105], N, M, T;
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     cin >> T;
  12.  
  13.     f(tt,1,T)
  14.     {
  15.         cin >> N >> M;
  16.         f(i,1,N) f(j,1,N) D[i][j] = INF;
  17.         f(i,1,N) D[i][i] = 0;
  18.         f(i,1,M)
  19.         {
  20.             int a,b,c;
  21.             scanf("%d %d %d", &a, &b, &c);
  22.             D[a][b] = min(D[a][b],c);
  23.             D[b][a] = min(D[b][a],c);
  24.         }
  25.         f(k,1,N) f(i,1,N) f(j,1,N) D[i][j] = min(D[i][j], D[i][k]+D[k][j]);
  26.         int ans = INF;
  27.         f(i,6,N)
  28.         {
  29.             unordered_set<int> s;
  30.  
  31.             f(p,1,5) s.insert(D[i][p]);
  32.             if(s.find(INF) != s.end()) continue;
  33.             if(SZ(s) != 1) continue;
  34.  
  35.             int far = 0;
  36.             f(j,1,N) far = max(far, D[i][j]);
  37.             ans = min(ans,far);
  38.         }
  39.  
  40.         if(ans < INF) cout << "Map " << tt << ": " << ans << "\n";
  41.         else cout << "Map " << tt << ": -1\n";
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment