您的位置:首页 > 其它

pku 1125 Stockbroker Grapevine(Floyd-Warshall)

2009-07-13 17:13 274 查看
 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;
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法