【POJ 1797 --- Heavy Transportation】dijkstra || kruskal, 最短路径变形
题目来源:点击进入【POJ 1797 — Heavy Transportation】
Description
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
解题思路1(dijkstra)
和poj2253一样,只不过那题n<=200,可以用floyd,而这题floyd会TLE,所以用dijkstra或者kruskal来做。
提一下floyd的做法,用dp[i][j]表示i到j的所有路径中最小边的最大值,那么转移方程是: dp[i][j]=max(dp[i][j] , min(dp[i][k] , dp[k][j]) )。
dijkstra的做法也是一样的,修改dis数组的定义,即dis[i][j]表示i到j的路径中最小边的最大值,那么松弛操作就是:dis[j]=max(dis[j] , min(dis[i] , e[i][j]) )。
AC代码1(dijkstra):
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
const int MAXN = 1005;
const int inf = 0x3f3f3f3f;
int n,m,x,y,val;
int dis[MAXN],map[MAXN][MAXN];
bool vis[MAXN];
void dijkstra()
{
for(int i=1;i<=n;i++)
dis[i]=map[1][i],vis[i]=false;
dis[1]=0;
while(true)
{
int v=-1;
for(int i=1;i<=n;i++)
if(!vis[i] && (v==-1 || dis[i]>dis[v])) v=i;
if(v==-1) break;
vis[v]=true;
for(int i=1;i<=n;i++)
if(dis[i]<min(dis[v],map[v][i]))
dis[i]=min(dis[v],map[v][i]);
}
}
int main()
{
SIS;
int T,cas=0;
cin >> T;
while(T--)
{
memset(map,0,sizeof(map));
cin >> n >> m;
for(int i=0;i<m;i++)
{
cin >> x >> y >> val;
map[x][y]=map[y][x]=val;
}
dijkstra();
cout << "Scenario #" << ++cas << ":" << endl;
cout << dis[n] << endl << endl;
}
return 0;
}
解题思路2(kruskal)
通过结构体存储起始点,终点,以及路径长度。将所有结构体按边从大到小排序。然后遍历结构体数组通过并查集合并和判断1和n是否合并到一个集合。如果是那么当前边长就是答案。
AC代码2:
#include <iostream>
#include <algorithm>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
const int MAXN = 1005;
int n,m,x,y,val,ans,pre[MAXN];
struct Node
{
int u,v,w;
}node[MAXN*1000];
void init()
{
for(int i=0;i<=n;i++)
pre[i]=i;
}
int _find(int x)
{
if(pre[x]==x) return x;
return pre[x]=_find(pre[x]);
}
void unite(int x,int y)
{
x=_find(x);
y=_find(y);
if(x==y) return;
pre[x]=y;
}
bool compare(Node a,Node b)
{
return a.w>b.w;
}
void kruskal()
{
init();
sort(node,node+m,compare);
for(int i=0;i<m;i++)
{
ans=node[i].w;
int u=node[i].u,v=node[i].v;
if(_find(u)!=_find(v)) unite(u,v);
if(_find(1)==_find(n)) break;
}
}
int main()
{
SIS;
int T,cas=0;
cin >> T;
while(T--)
{
int num=0;
cin >> n >> m;
for(int i=0;i<m;i++)
cin >> node[i].u >> node[i].v >> node[i].w;
kruskal();
cout << "Scenario #" << ++cas << ":" << endl;
cout << ans << endl << endl;
}
return 0;
}