pku 1125 Stockbroker Grapevine(Floyd-Warshall)

 Floyd-Warshall算法计算各点对之间的最短路径权值,最后把所有起始点遍历一下,选出满足要求的点。

 

// pku 1125.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; #define rm(x,y) (x=x<y?x:y)//replace if smaller int net[105][105]; int n; int main() { //freopen("d://1.txt","r",stdin); int i,j,k,con_point,time,start,least_time,temp; while(scanf("%d",&n)&&n) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i!=j) net[i][j]=SHRT_MAX; else net[i][j]=0; } } for(i=1;i<=n;i++) { scanf("%d",&k); for(j=0;j<k;j++) { scanf("%d",&con_point); scanf("%d",&net[i][con_point]); } } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { rm(net[i][j],net[i][k]+net[k][j]); } } } least_time=SHRT_MAX; for(i=1;i<=n;i++) { temp=0; for(j=1;j<=n;j++) { if(i==j) continue; if(net[i][j]>temp) temp=net[i][j]; } if(temp<least_time) { start=i; least_time=temp; } } printf("%d %d/n",start,least_time); } return 0; }  

你可能感兴趣的:(pku 1125 Stockbroker Grapevine(Floyd-Warshall))