POJ1125 Stockbroker Grapevine

来源:互联网 发布:深度linux硬盘怎么分区 编辑:程序博客网 时间:2024/05/28 04:54
Floyd求每点到其它点的最短路,最长路径即是传播所需的最大时间。

#include <iostream>#include <cstdlib>#include <string>#include <algorithm>using namespace std;bool cmp(int a, int b) {    return a> b;}int main() {    int t;    int n;    int contact;    int time;    const int max =10000;    int current_t;    int current_p;    int dp[150][150];    int result[150];    while(cin>> t) {       if(t == 0)          break;       memset(dp, max, sizeof(dp));       current_t = 10000;       for(int i = 0; i < t; i++){           cin>> n;           for(int j= 0; j < n; j++) {              cin>> contact>> time;              dp[i][contact-1] =time;           }       }       for(int i = 0; i < t; i++){           dp[i][i] =0;       }       for(int k = 0; k < t; k++){           for(int i= 0; i < t; i++) {              for(int j = 0; j< t; j++) {                 if(dp[i][j] > dp[i][k]+dp[k][j]&& i != j&& i != k&& j != k) {                     dp[i][j] =dp[i][k]+dp[k][j];                 }              }           }       }       for(int i = 0; i < t; i++){          sort(dp[i], dp[i]+t, cmp);       }              for(int i = 0; i < t; i++){          if(dp[i][0] < current_t) {              current_t = dp[i][0];              current_p = i;           }       }       if(current_t == max) {           cout<< "disjoint"<< endl;       } else {           cout<< current_p+1<< " "<< current_t<< endl;       }    }    //system("pause");    return 0;}



原创粉丝点击