您的位置:首页 > 其它

POJ 1797 Heavy Transportation(dijkstra变形)

2016-04-24 00:07 363 查看
题目描述:

Heavy Transportation

Time Limit: 3000MS Memory Limit: 30000K

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到城市n的路径当中最大的承重是多少。

题目分析:

这道题我想到的办法是利用dijkstra的思路来解的

数组d[i]表示从起点到i的路中最大的承重

优先队列que的每一个元素都是一个二元组{最大承重,节点编号},优先队列是一个大根堆,按照first排,也就是说会优先弹出最大承重大的节点,由于que中存储的节点都是能够到达的节点(这点很重要),所以如果弹出来一个节点,编号是n的话,那么就可以停止搜啦,结果就出来了。既然用到了优先队列,什么时候才压进栈呢?

if(d[e.to]<min(d[v],e.w)){
d[e.to]=min(d[v],e.w);
que.push(P(d[e.to],e.to));
}


对于每一条单一的路线,要选择这条路中每段承重最小的那个,也就是在上面这段代码中min的部分,如果d[i]存的值比当前的最小值还小的话,那么就要跟新、压栈咯,这也就预示着d
的初值应该为0,针对1这个节点,从1运到自己,自己的承重上限就应该是INF啦,接下来看代码吧。

代码:

#include "stdio.h"
#include "vector"
#include "algorithm"
#include "string.h"
#include "queue"
#include "utility"
#define INF 1e8
using namespace std;
typedef pair<int,int> P;
int res;
int d[1000+5];
struct node
{
int to,w;
node(int to1,int w1)
{
to=to1;w=w1;
}node(){}
};
vector<node> G[1000+5];

int n,m;
int main()
{
int ca,t=1,from,to,w;
scanf("%d",&ca);
while(t<=ca)
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
{
G[i].clear();
}
for (int i = 0; i < m; i++)
{
scanf("%d%d%d",&from,&to,&w);
G[from].push_back(node(to,w));
G[to].push_back(node(from,w));
}
priority_queue<P> que;
memset(d,0,sizeof(d));
d[1]=INF;
que.push(P(INF,1));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(v==n){
res=p.first;
break;
}
for (unsigned int i = 0; i < G[v].size(); i++)
{
node e=G[v][i];
if(d[e.to]<min(d[v],e.w)){ d[e.to]=min(d[v],e.w); que.push(P(d[e.to],e.to)); }
}
}
printf("Scenario #%d:\n",t++);
printf("%d\n\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj dijkstra