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.
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点运送货物到n点,求能运送货物的最大重量。
对于数据,第一行为t代表测试数据个数,第二行为n和m(意义见上),接着m行,每行三个整数分别是代表一条边的起点,终点及最大承重量。输出能运送货物的最大重量,格式见样例。注意数据输完后还要再多输一个空行。
思路:
//单源最短路Dijkstra算法
//变形的Dijkstra求start到end的最大距离
//最大距离是指从start–>end的一条路上最小的权值边
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int MAX = 1005;
const int INF = 0x3f3f3f3f;
struct A{
int arcs[MAX][MAX];//权值-->路径长度
int arcnum; //边的数目
int vexnum; //顶点的数目
}G;
bool visit[MAX]; //顶点的访问情况
int dis[MAX]; //出发点到其余点的最短距离
int v;
//单源最短路Dijkstra算法
//变形的Dijkstra求start到end的最大距离
//最大距离是指从start-->end的一条路上最小的权值边
int Dijkstra(int start,int end){
for(int i=1 ; i<=G.vexnum ; i++){
dis[i] = G.arcs[start][i];
visit[i] = false;
}
visit[start] = true;
//再依次添加n-1个点,更新最短路
for(int i=1 ; i<=G.vexnum-1 ; i++){
int m = -1;
for(int j=1 ; j<=G.vexnum ; j++){
//找到距离最大的
if(!visit[j] && dis[j]>m){
m = dis[j];
v = j;
}
}
//已经找到了就不用找了
//因为更新的是未访问的点的最大距离
if(v == end){
return m;
}
//找到最长距离的点
visit[v] = true;
for(int j=1 ; j<=G.vexnum ; j++){
//更新起始点到未访问点的最大距离
if(!visit[j] && dis[v]>dis[j] && G.arcs[v][j]>dis[j]){
dis[j] = min(dis[v],G.arcs[v][j]);
}
}
}
return dis[end];
}
int main(void){
int T;
// cin>>T;
scanf("%d",&T);
for(int t=1 ; t<=T ; t++){
// cin>>G.vexnum>>G.arcnum;
scanf("%d %d",&G.vexnum,&G.arcnum);
//初始化图
for(int i=1 ; i<=G.vexnum ; i++){
for(int j=1 ; j<=G.vexnum ; j++){
G.arcs[i][j] = (i==j)?0:-1;
}
}
//输入权值(路径长度)
for(int i=1 ; i<=G.arcnum ; i++){
int x,y,w;
// cin>>x>>y>>w;
scanf("%d %d %d",&x,&y,&w);
G.arcs[x][y]=G.arcs[y][x]=w;
}
//最后输出是两个回车!!!
printf("Scenario #%d:\n%d\n\n",t,Dijkstra(1,G.vexnum));
// cout<<"Scenario #"<<t<<":"<<endl;
// cout<<Dijkstra(1,G.vexnum)<<endl;
}
return 0;
}