Light oj 1002 - Country Roads 题目链接 , click here …
1002 - Country Roads
PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB
I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of my city t where I belong. Now from each city you have to find the minimum cost to go to my city. The cost is defined by the cost of the maximum road you have used to go to my city.
For example, in the above picture, if we want to go from 0 to 4, then we can choose
1) 0 - 1 - 4 which costs 8, as 8 (1 - 4) is the maximum road we used
2) 0 - 2 - 4 which costs 9, as 9 (0 - 2) is the maximum road we used
3) 0 - 3 - 4 which costs 7, as 7 (3 - 4) is the maximum road we used
So, our result is 7, as we can use 0 - 3 - 4.
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.
Each case starts with a blank line and two integers n (1 ≤ n ≤ 500) and m (0 ≤ m ≤ 16000). The next m lines, each will contain three integers u, v, w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 20000) indicating that there is a road between u and v with cost w. Then there will be a single integer t (0 ≤ t < n). There can be multiple roads between two cities.
Output
For each case, print the case number first. Then for all the cities (from 0 to n-1) you have to print the cost. If there is no such path, print ‘Impossible’.
Sample Input
2
5 6
0 1 5
0 1 4
2 1 3
3 0 7
3 4 6
3 1 8
1
5 4
0 1 5
0 1 4
2 1 3
3 4 7
1
Output for Sample Input
Case 1:
4
0
3
7
7
Case 2:
4
0
3
Impossible
Impossible
Note
Dataset is huge, user faster I/O methods.
题意: 一共n个城市 ,m 条路, 最后还有一个城市编号 t, 求从t 城市到所有城市的每一条路径中最长边中最小的那条边, 如果到不了 ,输出"Impossible",前边说的可能有点绕, 来看第一组样例:
t=1
1到0的距离(1,0)=4,
1到1的距离(1,1)=0,
1到2的距离(1,2)=2,
然而从1到3的距离有两条:
1> 1-0-3,长度是11,其中最长的边是7;
2> 1-3,长度是8;
这两条最长边中的最短的是7,所以样例输出的是7.
从1到4的距离也有两条:
1> 1-0-3-4,长度是17,其中最长边是7;
2> 1-3-6, 长度是14,其中最长边是8;
这两条边中最小的边是7,所以输出也是7.
这下应该懂了吧?再声明一遍 , 找从t到所有城市的所有可行路径中最长边的最短边.
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdio.h>
using namespace std;
const int maxn = 505;
const int inf = 0x3f3f3f;
int vis[maxn],dis[maxn];
int mp[maxn][maxn];
int main()
{
int T; //T组数据;
int m,n; //n个点,m条边;
int a,b,c; //接收临时数据;
int t ; //源城市;
int flag=1;//标记 Case;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);// n 城市个数, m 数据的组数;
//初始化;
//不能用memset() ,因为memset只能初始化为 0 ro -1 , 其他的会出错;
for(int i=0; i<=n; i++)
for(int j=0; j<=n; j++)
mp[i][j]=mp[j][i]=i==j?0:inf;
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&a,&b,&c);
if(mp[a][b]>c)
mp[a][b]=mp[b][a]=c;
}
scanf("%d",&t);//源城市;
memset(vis,0,sizeof(vis)); //vis[] 初始化;
for(int i=0; i<n; i++)// dis[] 数组的初始化;
dis[i]=i==t?0:inf;
int minn,k;// 最小值 下标;
for(int i=0; i<n; i++)
{
minn=inf;
//找到这个数的下标 , 将这一点放入集合中 ;
for(int j=0; j<n; j++)
if(vis[j]==0&&dis[j]<minn)
minn=dis[k=j];
vis[k]=1;//将找到的点标记成已访问;
//遍历循环一遍,更新dis[];
for(int v=0; v<n; v++)
if(mp[k][v]<inf)
dis[v]=min(max(dis[k],mp[k][v]),dis[v]);
//这个式子手推一下就能够写出来, dis[]记录最长的边中最短的边;
}
//输出;
printf("Case %d:\n",flag);
for(int i=0; i<n; i++)
{
if(dis[i]==inf)
printf("Impossible\n");
else
printf("%d\n",dis[i]);
}
flag++;
}
return 0;
}