您的位置:首页 > 其它

POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】

2015-10-22 21:37 441 查看
Heavy Transportation
Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 1797DescriptionBackground
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.InputThe 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.OutputThe 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---n的路径中,找一条最短边的最大值。也就是在这个路径中,这条边的长度小于这条路径中所有边,但是大于这条路径之外的所有边长度。

解题思路:最短边最大化。d[i]表示从源点到i点的最短边长度。跟POJ 2253解法类似。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
const int maxn = 1e4+200;
const int INF = 0x3f3f3f3f;
int n,m;
struct HeapNode{
int d;
int u;
bool operator < (const HeapNode &rhs)const {
return d < rhs.d;                //
}
};
struct Edge{
int from,to,dist;
};
vector<Edge>edge;
vector<int>G[maxn];
priority_queue<HeapNode>PQ;
int d[maxn] , vis[maxn];
void AddEdge(int u,int v,int w){
edge.push_back((Edge){u,v,w});
m = edge.size();
G[u].push_back(m-1);
}
void init(){
for(int i = 0; i<= n;i++){
G[i].clear();
}
edge.clear();
}
void Dijstra(int s){
for(int i = 0;i <= n; i++){
d[i] = 0;
}
d[s] = INF;
memset(vis,0,sizeof(vis));
PQ.push( (HeapNode){d[s],s} );
while(!PQ.empty()){
HeapNode x = PQ.top();
PQ.pop();
int u = x.u;
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0; i < G[u].size(); i++){
Edge & e = edge[G[u][i]];
if(vis[e.to]) continue;
if(d[e.to] < min(d[e.from] , e.dist)){
d[e.to] = min(d[e.from], e.dist);
PQ.push((HeapNode){ d[e.to], e.to });
}
}
}
}
int main(){
int T,cnt = 0,mm;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&mm);
init();
int a,b,c, i;
for( i = 1; i <= mm; i++){
scanf("%d%d%d",&a,&b,&c);
a--,b--;
AddEdge(a,b,c);
AddEdge(b,a,c);
}
Dijstra(0);
printf("Scenario #%d:\n",++cnt);
printf("%d\n",d[n-1]);
puts("");
}
return 0;
}
  

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: