现在的位置: 首页 > 综合 > 正文

POJ 1797 Heavy Transportation(Dijkstra的稍加变形,三角形3边关系)

2018年04月28日 ⁄ 综合 ⁄ 共 2635字 ⁄ 字号 评论关闭
Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 21632   Accepted: 5754

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

题目大意:

首先输入n个城市,m条街道,然后输入每两个城市之间的最大载容量,判断从1号城市到n号城市的最大载容量是多少的问题。因为要从1号城市往n号城市派一辆汽车过去,当然要满足汽车的载重最大了,但是每条街道又有自己的载容量限制,所以我们只需要对于Dijkstra()稍作一些修改就好。

解题思路:

在Dijkstra()算法中,以前求得的是最短路,判断的条件是:

if ( book[j] == 0 && dis[j] < _min )

{

_min = dis[j];

 u = j;

}

这里需要把_min改成_max。。。而max的值为-1.在init()的过程中,也只需在开始的时候将edge[i][j]改为全部为0的了。。。

还有一处需要改动的地方,就是在进行边的松弛的过程中,我们要做到的是 if ( book[v]==0&&dis[v] < min( dis[u],edge[u][v]) )

代码:

# include<cstdio>
# include<iostream>

using namespace std;

# define MAX 1234

int edge[MAX][MAX];
int book[MAX];
int dis[MAX];
int n,m;
int u,v;


void init()
{
    for ( int i = 1;i <= n;i++ )
    {
        for ( int j = 1;j <= n;j++ )
        {
            edge[i][j] = 0;
        }
    }
}

void input()
{
    for ( int i = 1;i <= m;i++ )
    {
        int t1,t2,t3;
        cin>>t1>>t2>>t3;
        edge[t1][t2] = t3;
        edge[t2][t1] = t3;
    }
}

int Dijkstra()
{
    for ( int i = 1;i <= n;i++ )
    {
        book[i] = 0;
        dis[i] = edge[1][i];
    }

    int _max;
    book[1] = 1;

    for ( int i = 1;i <= n-1;i++ )
    {
         _max = -1;
         for ( int j = 1;j <= n;j++ )
         {
             if ( book[j]==0&&dis[j] > _max )
             {
                 _max = dis[j];
                 u = j;
             }
         }


    book[u] = 1;
    for ( v = 1;v <= n;v++ )
    {
        if ( book[v]==0&&dis[v] < min( dis[u],edge[u][v]) )
        {
            dis[v] = min( dis[u],edge[u][v] );
        }
    }

    }

    return dis[n];



}

int main(void)
{
    int t;cin>>t;
    int icase = 1;
    while ( t-- )
    {
        cin>>n>>m;
        init();
        input();
        printf("Scenario #%d:\n",icase++);
        printf("%d\n\n",Dijkstra());
    }


    return 0;
}

抱歉!评论已关闭.