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;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章