poj 1797 Heavy Transportation 【变形dijk】

版权声明:转载的时候记着点个赞再评论一下! https://blog.csdn.net/LOOKQAQ/article/details/82786625

题目链接:http://poj.org/problem?id=1797

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 49563   Accepted: 12832

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。接下来m行每行输入3个整数(假设为a,b,c)代表a,b之间只允许最大重量为c的通过,让求,从起点能运到终点的最大重量。(注意,不能照搬dijk算法,下面对样例进行解释,输入的n=3,m=3,接下来有三行,1 2 3 代表1,2之间允许最大重量为3的通过,1 3 4代表1,3之间允许最大重量为4的通过,2 3 5 代表2,3,之间允许最大重量为5的通过,所以现在有2种方式,一种是从1直接到3,能运载的最大重量为4,另一种是从1到2,然后从2到3,因为1,2之间的最大运载量为3,即使2,3之间的运载量为5,但是从2来的只能为3,所以这种方式的最大运载量为3 小于上种运输方式,答案输出4)

思路:这是个变形版的dij(),挺好的一道题。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1e3+10;
const int inf = -0x3f3f3f3f; //初始化为负无穷
int vis[maxn],dis[maxn];
int map1[maxn][maxn],n,m,u,v,w;
void dij()
{
    memset(vis,0,sizeof vis);
    for(int i=1;i<=n;i++)
        dis[i] = map1[1][i];
    vis[1] = 1;
    dis[1] = 0;
    int ans,next;
    for(int i=1;i<n;i++)
    {
        ans = inf;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j] > ans) //找出距离源点最远的点(除本身之外)
            {
                ans = dis[j];
                next = j;
            }
        }
        vis[next] = 1;
        for(int j=1;j<=n;j++)
        {
            //下面是核心的比较,也是与dij不同之处的一个地方
            if(!vis[j] && dis[j] < min(dis[next],map1[next][j]))
                dis[j] = min(dis[next],map1[next][j]);
        }
    }
    cout<<dis[n]<<endl<<endl; //注意换行+换行,不然格式卡死
}
int main()
{
    ios::sync_with_stdio(false);
    int t,case1=1;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j) map1[i][j] = 0;
                else map1[i][j] = inf;
            }
        }
        for(int i=1;i<=m;i++)
        {
            cin>>u>>v>>w;
            map1[u][v] = map1[v][u] =w; //双向边的处理,注意
        }
        cout<<"Scenario #"<<case1++<<":"<<endl;
        dij();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LOOKQAQ/article/details/82786625