码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1797 Heavy Transportation

时间:2015-07-26 18:49:53      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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
题意:货物运行时两个地点可以有多条街道连接,每条街道对车辆的最大承载量是不同的,那么现在需要求出从地点1运送货物到地点n所能承载的最大货物量,假如中间有很多条街道相通,能承受的只能是最少的量,那么只需要求出这些最少的中间最大的那个就行
#include<stdio.h>
#define INF 0x3f3f3f3f
#define N 1100
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
int G[N][N], dist[N], visit[N], n; //dist数组存放的是所有从1到i路径之间的每一小段的最小值的最大值
void Init()
{
    int i, j;
    for (i = 0; i <= n; i++)
    {
        visit[i] = 0;
        dist[i] = -INF;
        for (j = 0; j <= n; j++)
            G[i][j] = -INF; 
    }
}
void Dist()
{
    int i, j, idex, Min;
    dist[1] = 0;
    for (i = 1; i <= n; i++)
    {
        Min = -INF;
        for (j = 1; j <= n; j++)
        {
            if (dist[j] > Min && !visit[j])
            {
                Min = dist[j];
                idex = j;
            }
        }
        visit[idex] = 1;
        for (j = 1; j <= n; j++)
        {
            if (idex == 1)
            {
                if (!visit[j] && dist[j] < max(dist[idex], G[idex][j]))
                    dist[j] = max(dist[idex], G[idex][j]);
            } //如果idex是1时仍然用min,则dist数组中都是0,为了不影响结果要max,这样dist数组就不会全是0了
            else
            {
                if (!visit[j] && dist[j] < min(dist[idex], G[idex][j]))
                    dist[j] = min(dist[idex], G[idex][j]);
            }
        }
    }
}
int main ()
{
    int T, m, a, b, c, k = 0;
    scanf("%d", &T);
    while (T--)
    {
        Init();
        k++;
        scanf("%d %d", &n, &m);
        while (m--)
        {
            scanf("%d %d %d", &a, &b, &c);
            G[a][b] = max(G[a][b], c);
            G[b][a] = G[a][b];
        }
        Dist();
        printf("Scenario #%d:\n", k);
        printf("%d\n", dist[n]);
        printf("\n");
    }
    return 0;
}

 

POJ 1797 Heavy Transportation

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4678244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!