您的位置:首页 > 其它

Heavy Transportation(最短路变形)

2015-10-18 23:09 190 查看
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

一个图,每条路有运输的上限,运输的上限就是这条路上的最小值,问运输量最大的路线。

其实就是一个最大流,但是又不是最大流,只需要把起点设为无限流量,其他全设为inf,比较取最小值的最大值就行了,就像上一题青蛙一样,只是反过来了而已。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int head[1005],d[1005],leap[1005];
struct Node
{
int u,v,w,next;
}node[2000005];
void add_edge(int u,int v,int w,int tot)
{
node[tot].u = u;node[tot].v = v;
node[tot].w = w;
node[tot].next = head[u];
head[u] = tot;
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
int t,kase = 1;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)head[i] = -1;
for(int i = 1;i <= m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w,i*2 - 1);
add_edge(v,u,w,i*2);
}
for(int i = 1;i <= n;i++)d[i] = 0;
for(int i = 1;i <= n;i++)leap[i] = 0;
queue<int>p;
p.push(1);
d[1] = inf;leap[1] = 1;
while(p.size())
{
int u = p.front();p.pop();
leap[u] = 0;
for(int i = head[u];i != -1;i = node[i].next)
{
int e = node[i].v;
int temp = min(d[u],node[i].w);
if(d[e] < temp)
{
d[e] = temp;
if(!leap[e])
{
leap[e] = 1;p.push(e);
}
}
}
}
printf("Scenario #%d:\n",kase++);
printf("%d\n\n",d
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: