POJ 1125 Stockbroker Grapevine (FloydWarshall 所有点对最短路径)

#include <stdio.h>
#define MAX_STOCKBROKERS 101
//10 minutes * 100 stockBrokers + 1
#define MAX_MINUTES 1001

int numOfStockBrokers;
int numOfContracts;
int from, to, pass;
int minutes;
int time[MAX_STOCKBROKERS][MAX_STOCKBROKERS];
int maxTime;
int minInMax;
int person;

int main(){
	freopen("input.txt", "r", stdin);

	while (scanf("%d", &numOfStockBrokers) != EOF && numOfStockBrokers != 0){
		
		for (from = 1; from <= numOfStockBrokers; from++)
			for (to = from + 1; to <= numOfStockBrokers; to++)
				time[from][to] = time[to][from] = MAX_MINUTES;

		for (from = 1; from <= numOfStockBrokers; from++){
			scanf("%d", &numOfContracts);
			while (numOfContracts--){
				scanf("%d%d", &to, &minutes);
				time[from][to] = minutes;
			}
		}

		for (pass = 1; pass <= numOfStockBrokers; pass++)
			for (from = 1; from <= numOfStockBrokers; from++)
				for (to = 1; to <= numOfStockBrokers; to++)
					if (time[from][pass] + time[pass][to] < time[from][to])
						time[from][to] = time[from][pass] + time[pass][to];
		
		minInMax = MAX_MINUTES;

		for (from = 1; from <= numOfStockBrokers; from++){
			maxTime = -1;
			for (to = 1; to <= numOfStockBrokers; to++)
				if (time[from][to] > maxTime)
					maxTime = time[from][to];

			if (maxTime < minInMax){
				minInMax = maxTime;
				person = from;
			}
		}

		if (minInMax == MAX_MINUTES)
			printf("disjoint\n");
		else
			printf("%d %d\n", person, minInMax);

	}
	
	return 0;
}


 

你可能感兴趣的:(最短路径,poj,Stockbroker,1125,Grapevin,Floy)