poj 1797 Heavy Transportation
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 1797 Heavy Transportation相关的知识,希望对你有一定的参考价值。
Time Limit: 3000MS Memory Limit: 30000K
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
题目大意:
Hugo Heavy要从城市1到城市N运送货物,有M条道路,每条道路都有它的最大载重量,问从城市1到城市N运送最多的重量是多少。
这道题我们可以采用类似于求最短路径的方法,用一种新的“松弛操作”去取代原本的方法。 我们可以记录dis[u]为运送货物到点u时最大可载重量。那么对于一条边(u,v),我们有dis[u]=max(d[v],min(dis[u],w(u,v))).
喜闻乐见的Code
#include<cstdio> #include<queue> #include<cstring> #include<iostream> using namespace std; #define maxn 1010 #define inf 0x3f3f3f3f struct Edge { int u,v,weight,next; Edge(int u=0,int v=0,int weight=0,int next=0): u(u),v(v),weight(weight),next(next) {} }edge[maxn*maxn]; int head[maxn]={0},cnt=0,n,m,Q,dis[maxn]={0}; bool inq[maxn]={0}; void Add_edge(int u,int v,int weight) { edge[++cnt]=Edge(u,v,weight,head[u]); head[u]=cnt; } int SPFA(int s=1,int t=n) { queue<int>q; for(int i=1;i<=n;i++) inq[i]=false,dis[i]=0; q.push(s);inq[s]=true;dis[s]=inf; while(!q.empty()) { int u=q.front();q.pop();inq[u]=false; for(int i=head[u];i;i=edge[i].next) { int v=edge[i].v,Max_w=min(dis[u],edge[i].weight); if(Max_w>dis[v]) { dis[v]=Max_w; if(!inq[v]) { q.push(v); inq[v]=true; } } } } return dis[t]; } int main() { scanf("%d",&Q); for(int o=1;o<=Q;o++) { scanf("%d%d",&n,&m); memset(head,0,sizeof(head)); for(int i=1;i<=m;i++) { int u,v,weight; scanf("%d%d%d",&u,&v,&weight); Add_edge(u,v,weight); Add_edge(v,u,weight); } printf("Scenario #%d:\n%d\n",o,SPFA()); if(o!=Q) printf("\n"); } return 0; }
以上是关于poj 1797 Heavy Transportation的主要内容,如果未能解决你的问题,请参考以下文章
POJ-1797 Heavy Transportation( 最短路 )
POJ 1797 Heavy Transportation (最大生成树)