POJ 1125 Stockbroker Grapevine

给你n个人的联系情况,对任意一个人,求出这个人发消息到其他n-1个人的时间,得到n-1个时间中的最大值,n个最大值中的最小值就是所求。如果网络不通,那就输出disjoint

Floyed算出任意两个人的最小时间 就OK了

 
   
   
     
#include < iostream >
#include
< fstream >
using namespace std;
const int MAXN = 101 ;
const int INF = 0x7FFF ;
int m , n;
int G[MAXN][MAXN];

void in (){
int i,j,w,t;
for (i = 1 ; i <= m;i ++ )
for (j = 1 ; j <= m; j ++ )
G[i][j]
= INF;
for (i = 1 ; i <= m; i ++ )
G[i][i]
= 0 ;
for (i = 1 ; i <= m; i ++ ){
cin
>> n;
for (j = 1 ; j <= n;j ++ ){
cin
>> w >> t;
G[i][w]
= t;
}
}
}

void floyd(){
int i,j,k;
for (k = 1 ; k <= m; k ++ )
for (i = 1 ; i <= m; i ++ )
for (j = 1 ; j <= m; j ++ ){
if (G[i][j] > G[i][k] + G[k][j])
G[i][j]
= G[i][k] + G[k][j];
}

}

void out (){
int i,j,temp = 0 ,bestN,best = INF;
bool flag = false ;
for (i = 1 ; i <= m; i ++ ){
flag
= false ;
temp
= 0 ;
for (j = 1 ; j <= m; j ++ ){
if ( temp < G[i][j] && i != j){
temp
= G[i][j];
}
if (G[i][j] == INF){
flag
= true ;
break ;
}
}
if ( ! flag){
if (best > temp ){
best
= temp;
bestN
= i;
}
}
}
if (bestN == INF)
cout
<< " disjoint " << endl;
else
cout
<< bestN << " " << best << endl;
}

int main(){
while (cin >> m, m != 0 ){
in ();
floyd();
out ();
}
return 0 ;
}

你可能感兴趣的:(broker)