poj 1797 Heavy Transportation

                                                                             Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 5824 Accepted: 1539

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

Source

TUD Programming Contest 2004, Darmstadt, Germany

[Submit]   [Go Back]   [Status]   [Discuss]

//Dijkatra做最大网络流.注意这里我用优先队列+Dijkstra,另外一个需要注意的在做做大网络流的时候和做
最短路径的时候区别是在做最大网络流的时候每次要先将大的出队,即要用大根堆实现
//5005981 11410 1797 Accepted 5624K 407MS C++ 1379B 2009-04-18 21:17:12 
#include <iostream>
#define MAX 1005
#include 
<queue>
using namespace std;
typedef 
struct node
{
    
int dis;
    
int heap;
    friend 
bool operator <(node a,node b)
    {
        
return a.dis<b.dis;
    }
}Point;
priority_queue
<Point>Q;
Point temp;
int edges[MAX][MAX],n,m,t,dis[MAX];
bool mark[MAX];
void Init()
{
    
int i,j,a,b,s;
    scanf(
"%d%d",&n,&m);
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
            edges[i][j]
=-1;
    
while(m--)
    {
        scanf(
"%d%d%d",&a,&b,&s);
        edges[a][b]
=edges[b][a]=s;
    }
}
int GetMin(int a,int b)
{
    
if(a>b)
        
return b;
    
else
        
return a;
}
void Dijkstra(int start,int end)
{
    
int i,j,k;
    
while(!Q.empty())
        Q.pop();
    
for(i=1;i<=n;i++)
    {
        dis[i]
=edges[start][i];
        
if(dis[i]!=-1)
        {
            temp.dis
=dis[i];
            temp.heap
=i;
            Q.push(temp);
        }
        mark[i]
=false;
    }
    mark[start]
=true;
    dis[start]
=0;
    
for(i=1;i<n;i++)
    {
        
while(!Q.empty())
        {
            temp
=Q.top();
            Q.pop();
            
if(!mark[temp.heap])
            {
                k
=temp.heap;
                
break;
            }
        }
        mark[k]
=true;
        
for(j=1;j<=n;j++)
        {
            
if(mark[j])   //5006249 11410 1797 Accepted 4272K 329MS C++ 1528B 2009-04-18 21:44:32 
                continue;//正确的,已经并入的就不需要并了
            if(edges[k][j]==-1)  //5006212 11410 1797 Accepted 4272K 297MS C++ 1483B 2009-04-18 21:42:01 时间上优化了
                continue;
            
if(dis[j]==-1||(dis[k]>dis[j]&&edges[k][j]>dis[j]))
            {
                dis[j]
=GetMin(dis[k],edges[k][j]);
                temp.dis
=dis[j];
                temp.heap
=j;
                Q.push(temp);
            }
        }
    }
}
int main()
{
    
int zz=1;
    scanf(
"%d",&t);
    
while(t--)
    {
        Init();
        Dijkstra(
1,n);
        printf(
"Scenario #%d:\n%d\n\n",zz++,dis[n]);
    }
    
return 0;
}
原文地址:https://www.cnblogs.com/forever4444/p/1485937.html