Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following
m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight
that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.
Sample Input
1
3 3
1 2 3
1 3 4
2 3 5
Sample Output
Scenario #1:
4
我已经很努力试图讲清楚,图文配套。
题意:有n个城市m条路,输出道路的最大载重量,比如1->2载重量为3, 2->3载重量为5,虽然1->2->3的最大载重量为5,但是整条路的最小载重量为3,所以整条路的最小载重量就是能通过的最大载重量,而1->3这条路的载重量为4,所以在两条能从起点到达终点的路径中取最大值,最大载重量为4。
思路:Dijsktra的变形题,松弛条件改变一下。整体思路是:dis存的是从顶点1开始到顶点n那条路径上的最大载重值,由于从1到n的路不止一条,所以比较出((每条路径最大载重值)的最大值)。具体实现过程:先从源点找一个载重最大的出边顶点u往外拓展,注意已经取过的顶点要进行标记,取源点到u的载重和u到出边顶点i载重比较小的min值,如果从源点到出边顶点i的值小于min,更新dis[i],之后重复查找,直到找到n-1条边。
下面给出样例和运行结果方便解释:(可以自己用笔跟着这个思路在纸上画下)
2
4 4
1 2 6
2 4 1
1 3 5
3 4 2 还有一个需要注意的地方:输出样例后有空行!
#include<stdio.h>
#include<string.h>
#define N 1100
#define inf 99999999
int dis[N],e[N][N],book[N],t;
int min(int a,int b)
{
if(a > b)
return b;
return a;
}
int main()
{
int T = 0;
int n,m,count,u,i,j,t1,t2,t3,max;
scanf("%d",&t);
while( t--)
{
T++;
memset(book,0,sizeof(book));
memset(e,0,sizeof(e));
scanf("%d%d",&n,&m);
for(i = 1;i <= m; i ++)
{
scanf("%d%d%d",&t1,&t2,&t3);
if(t3 > e[t1][t2])
{
e[t1][t2] = t3;
e[t2][t1] = t3;
}
}//printf("注意:dis存的是整条路径的最大载重值!!!
");
for(i = 1; i <= n;i++)
dis[i] = e[1][i];
count = 0;
book[1] = 1;
for(i = 1; i <= m; i ++)
{
max = 0;
for(j = 1; j <= n; j ++)
{
if(!book[j]&&dis[j] > max)
{
max = dis[j];
u = j;
}
}
book[u] = 1;//printf("已找到未更新过的最大载重为 %d 和顶点 %d 的边
",dis[u],u);
count ++;
for(j = 1; j <= n; j ++)
{
if(!book[j]&&dis[j] < min(dis[u],e[u][j]))
{//printf("上一条边从源点到出边顶点 j=%d 的最大载重 dis[j]=%d 小于这条边的最大载重值 min=%d",j,dis[j],min(dis[u],e[u][j]));
dis[j] = min(dis[u],e[u][j]);
//printf(",更新dis[j]=%d
",dis[j]);
}
}
if(count == n-1) break;
}
printf("Scenario #%d:
",T);
printf("%d
",dis[n]);
}
return 0;
}
听说还可以用prime,等这个练习赛结束再来写