poj 1797 Heavy Transportation 用的dp的思想

Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 15948   Accepted: 4162

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

这道题想了好久。好久~~ 看了网上的各种方法, 有用dijstral变型来做的,有用最大生成树来做的~~直接被搞晕了~~~晚上和队长探讨了一番,貌似是dp的思想,对1-n的距离进行不断的更新,得到最后的结果,同是也能得到源点到任意点的 答案 距离;


poj 1797 Heavy Transportation 用的dp的思想_第1张图片


                                1  2   3  4    5      1-5的距离

从1-5输出的结果是:  0 30 30 50 80    

post code:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;

int mina(int a,int b)   //选择最小值
{
    if(a>b)return b;
    else return a;
}
int maxa(int a,int b)   //选择最大值
{
    if(a>b)return a;
    else return b;    
}

int a[1010][1010];
int ans[1010];
int used[1010];
int main()
{
    int t,m,n,ji=0;
    scanf("%d",&t);
    while(t--)
    {
           ji++;
           scanf("%d %d",&n,&m);
           int i,x,y,num;
           memset( a, 0, sizeof(a) );
           for( i=1; i<=m; i++ )        //建立邻接矩阵 无向图
           {
               scanf("%d %d %d",&x,&y,&num); 
               a[x][y]=num;
               a[y][x]=num;
           } 
           ans[1]=0;
           memset( used, 0, sizeof(used) );  
       
           used[1]=1;   // 1位置不用再更新了

           queue <int> my;
           while(!my.empty())my.pop();
           for( i=2; i<=n; i++ )    //开始 用以1为起始点 到其他点的距离(作为开始的结果 后面将不断的对其进行更新)
              {
                 ans[i]=a[1][i];
                 if(a[1][i]!=0){my.push(i);used[i]=1;}   //将和起始点直接相连的点进队列,有点类似广搜。
              }
              int min;
           while( !my.empty() )    //当队列为空的时候 结束
           {
                 m=my.front();
                 my.pop();
                 for( i=2; i<=n; i++ )
                 {
                      int original=ans[i];        //这步是重要的更新 如果你这个点更新了,还要再重新进队一次再进行更新。
                      min=mina( ans[m], a[m][i] );
                      ans[i]=maxa( ans[i], min);
                      if( ans[i] != original ) used[i]=0;  
                      if(used[i]!=1 && a[m][i]!=0 ) {my.push(i);used[i]=1;} //没有更新过 进队再进行更新
                 }
           }
           printf("Scenario #%d:\n",ji);
              printf("%d\n\n",ans[n]);  // 输出最后结果
        
    }
    
}










你可能感兴趣的:(poj 1797 Heavy Transportation 用的dp的思想)