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,比较取最小值的最大值就行了,就像上一题青蛙一样,只是反过来了而已。
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; }
相关文章推荐
- 10个数中取最大(初)
- 求一个参数二进制形式1 的个数
- 文章标题
- 实验三 敏捷开发与XP实践
- MarkdownPad 2 在win10下html渲染错误This view has crashed
- react native andrroid 学习相关的资料
- 求出10个整数中的最大值
- 函数--
- KMP
- iOS UITableView-FDTemplateLayoutCell框架 cell重叠 高度返回0.5问题解决
- test02
- 简单的交换两个变量的数值
- HBase shell命令小记
- 地理学的十四大原理
- MyEclipse就会报 Errors running builder 'DeploymentBuilder' on project '工程名' xxx
- 10G数据量,只有2G内存,怎样找到中位数?
- Android开发系列(三) 跟随移动userguide
- UFT练习
- C学习笔记(六)函数、数组与指针
- JS模板引擎