求助为何输出总是-1
查看原帖
求助为何输出总是-1
152652
AndyChen2005121楼主2019/1/29 19:28
#include <iostream>
#include <vector>
using namespace std;
int t[205];
struct node {
  int v, w;
};
int G[20001][20001];
void floyd(int n){
  for(int k = 1; k <= n; k++){
          for(int i = 1; i <= n; i++){
              for(int j = 1; j <= n; j++){
                  G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
              }
          }
      }
}
int main(){
    memset(G, -1, sizeof(G));
    for(int i = 0; i < 20001; i++){
        G[i][i] = 0;
    }
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      cin >> t[i];
    }
    for(int i = 0; i < m; i++){
      int u, v, w;
        cin >> u >> v >> w;
        G[u][v] = G[v][u] = w;
    }
    int q;
    cin >> q;
    while(q--){
      int x, y, time;
      cin >> x >> y >> time;
      if(t[x] != 0 && t[y] != 0){
        cout << -1 << endl;
        continue;
      } else {
        floyd(n);
        if(G[x][y] == -1){
          cout << -1 << endl;
          continue;
        } else {
          cout << G[x][y] << endl;
        }
      }
      for(int i = 0; i < n; i++){
        if(t[i] == 0) continue;
        else t[i]--;
    }

    return 0;
}
}
2019/1/29 19:28
加载中...