首页 > 代码库 > POJ 1125 Stockbroker Grapevine

POJ 1125 Stockbroker Grapevine

 

/**POJ 1125 Stockbroker Grapevine*maxdist 为从第i个人出发 传递完成所需的最大距离*ans 为maxdist 中的最小值*/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 110;const int INF = 0x3f3f3f3f;int dist[MAXN][MAXN];int n;int floyd(){	int i, j, k;	for (k = 0; k < n; k ++) {		for (i = 0;  i < n; i++) {			for (j = 0; j < n; j++) {				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);			}		}	}}int main(){	int i, j, num;	while (~scanf("%d", &n) && n) {		memset(dist, INF, sizeof(dist));		for (i = 0; i < n; i ++) {			scanf("%d", &num);			while (num--) {				scanf("%d", &j);				scanf("%d", &dist[i][j - 1]);			}		}		floyd();		int ans = INF, maxdist, pos;		for (i = 0; i < n; i++) {			maxdist = -1;			for (j = 0; j < n; j ++) {				if (i != j && maxdist < dist[i][j]) {					maxdist = dist[i][j];				}			}			if (ans > maxdist) {				ans = maxdist;				pos = i;			}		}		if (ans == INF) {			printf("disjoint\n");		} else {			printf("%d %d\n", pos+1, ans);		}	}	return 0;}

  

POJ 1125 Stockbroker Grapevine