POJ1797 Heavy Transportation (stack optimized modified Dijkstra)

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

# 1 Scenario: 
. 4 
to the effect that all paths in seeking the maximum point of the shortest side weight of 1 to n. It can be run through with a stack optimized Dijkstra. Where d is the meaning of the different arrays and relaxation operation are different. Where d [i] for all i from 1 to the representative of the minimum path side the biggest edge weights. Relaxation conditions were changed if (d [y] <min (d [x], z)) d [y] = min (d [x], z).
It is noted that:
1.d array is initialized to -INF because of the requirement that the d [n] allowed as large as possible.
2.d [1] to be initialized INF. Because if d [1] is 0 according to the initialization dij template, the first point 1 is taken out, this time d [y] is -INF, must be smaller than min (d [x], z ), as d [x] in the first equal to d [. 1] is equal to 0, so that eventually the entire array is d 0, not the answer.
2.pair first dimension without a minus sign because the priority queue to be let out big, so do not let it become as small root stack according to the blue book.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector> 
#include <cstring>
#include <queue>
using namespace std;
const int N=10005,M=200010;//两倍存双向边
int head[N],ver[M],edge[M],Next[M],d[N];
bool v[N];
int n,m,tot=0;
priority_queue<pair<int,int> >q;
 void add(int x,int y,int z)
 {
     ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot;
 }
 void dijkstra()
 {
     memset(d,-0x3f,sizeof(d));
     memset(v,0,sizeof(v));
     d[1]=2000000000;
     q.push(make_pair(20000000,1));
     while(q.size())
     {
         int x=q.top().second;
         q.pop();
         if(v[x])continue;
         v[x]=1;
         int i;
         for(i=head[x];i;i=Next[i])
         {
             int y=ver[i];
             int z=edge[i];
             if(d[y]<min(d[x],z))
             {
                 d[y]=min(d[x],z);
                 q.push(make_pair(d[y],y));
             }
         }
     }
 }
int main()
{
    int t;
    cin>>t;
    int i,j,k;
    for(i=1;i<=t;i++)
    {
        tot=0;
        while(q.size())q.pop();
        memset(head,0,sizeof(head));
        memset(Next,0,sizeof(Next));
        scanf("%d%d",&n,&m);
        for(j=1;j<=m;j++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        dijkstra();
        printf("Scenario #%d:\n",i);
        cout<<d[n]<<endl;
        cout<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12319665.html