Advertisement
hidrogenio3

topcom16i

Apr 25th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define rep(i, a, b) for(int i = a; i < (b); ++i)
  3. #define sz(x) (int)x.size()
  4. #define isnum(c) (c >= '0' && c <= '9')
  5. #define endl "\n"
  6. using namespace std;
  7.  
  8. #define MAXN 100
  9. #define INF 0x3f3f3f3f
  10.  
  11. typedef pair<int, int> ii;
  12. int adjList[MAXN][MAXN], prox[MAXN][MAXN];
  13. int dist[MAXN], n, m;
  14. vector<int> path;
  15.  
  16. void floydwarshall(){
  17.     rep(k, 0, n){
  18.         rep(i, 0, n){
  19.             rep(j, 0, n){
  20.                 if(adjList[i][j] > adjList[i][k] + adjList[k][j]){
  21.                     adjList[i][j] = min(adjList[i][j], adjList[i][k] + adjList[k][j]), prox[i][j] = prox[i][k];
  22.                 }
  23.                
  24.                 }
  25.         }
  26.     }
  27. }
  28.  
  29. void Path(int u, int v){
  30.     if(prox[u][v] == -1)
  31.         return;
  32.     path.push_back(u);
  33.     while(u != v){
  34.         u = prox[u][v];
  35.         path.push_back(u);
  36.     }
  37. }
  38.  
  39. int main(){
  40.     while(true){
  41.         rep(i, 0, MAXN){
  42.             rep(j, 0, MAXN){
  43.                 if(i == j){
  44.                     adjList[i][j] = 0;
  45.                     prox[i][j] = i;
  46.                 }
  47.                 else
  48.                     adjList[i][j] = INF, prox[i][j] = -1;
  49.             }
  50.         }
  51.             //memset(adjList[i], 0, sizeof adjList[i]);
  52.        
  53.         int p, r;
  54.         cin >> p >> r;
  55.         if(!p && !r)
  56.             break;
  57.         int nvit;
  58.         cin >> nvit;
  59.         int vit[nvit];
  60.         rep(i, 0, nvit)
  61.             cin >> vit[i];
  62.         n = p*p - r;
  63.  
  64.         cin >> m;
  65.         rep(i, 0, m){
  66.             int u, v, w;
  67.             cin >> u >> v >> w;
  68.             adjList[u-1][v-1] = w;
  69.             prox[u-1][v-1] = v-1;
  70.         }
  71.  
  72.         floydwarshall();
  73.         cout << "Caminhos e esforco do robo do ponto de apoio ate as vitimas:\n";
  74.         rep(i, 0, nvit){
  75.             if(vit[i] <= n){
  76.                 cout << "Vitima " << i + 1<< ":\n";
  77.                 path.clear();
  78.                 Path(0, vit[i] - 1);
  79.                 for(auto &a: path){
  80.                     cout << a + 1 << " ";
  81.                 }
  82.                
  83.                 cout << "esforco = " << adjList[0][vit[i] - 1] << endl;
  84.             }
  85.         }
  86.        path.clear();
  87.     }
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement