POJ 1797 Heavy Transportation

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

题目:
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组数据,输入三个值,分别是起点、终点、那条路可以承受的重量。求从结点1到结点n所能运载的最大重量是多少。
比如:
1->2 重量是3
1->3 重量是4
2->3 重量是5
1->3有两种走法,但是1->2只能承受3,所以最大承受量应该是4.

相关知识:
http://baike.baidu.com/link?url=31bD4_dPfvI-GSsliW0kGJ_CH1aFX9S_nWlOmC9BfqvRKNUJgDX4QllHC0c21lphtuFzHfOjzPmwA2WVjTUcK8XS4ABgXwLh4f1Qblx6V6GxRRF5aAoi4eUvgpZ-H1ui5ELZAU8muoDPzJSFAeL9bvFAVv6cptUxPF0WgEMiRES

dijkstra算法的变形。

爆炸啦,爆炸啦,不会做图论的呀,boom。
代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int INF = 1000000+10;//设置最大的长度
const int MIN = -1000000-10;//最小的
const int MAX = 1010;
int maps[MAX][MAX];
int dist[MAX];//保存1->对应点的当前最短距离
int status[MAX];//保存每个点的状态
int n, m, t;
void Finds(int s)
{
    int small = INF;
    //memset(dist, INF, sizeof(dist));
    for(int i = 0; i <= n; i++)
    {
        dist[i] = maps[s][i];
    }
    memset(status, 0, sizeof(status));
    dist[s] = 0;
    status[s] = 1;
    int anser = INF;
    for(int i = 1; i <= n; i++)
    {
        small = MIN;
        for(int j = 1; j <= n; j++)
        {
            if(!status[j] && dist[j] > small)
            {
                small = dist[j];
                t = j;
            }

        }
        anser = min(anser, small);
        if(t == n)
            break;

        status[t] = 1;
        for(int j = 1; j <= n; j++)
        {
            if(!status[j] && dist[j] < maps[t][j])
            {
                dist[j] = maps[t][j];
            }
        }
    }
    printf("%d\n\n", anser);//卧槽,两个换行是要闹哪样嘛
}

int main()
{
    int k;
    scanf("%d", &k);
    for(int t = 1; t <= k; t++)
    {
        scanf("%d%d", &n, &m);
        memset(maps, -1, sizeof(maps));
        for(int i = 0; i < m; i++)
        {
            int begins, ends;
            scanf("%d%d", &begins, &ends);
            scanf("%d", &maps[begins][ends]);
            maps[ends][begins] = maps[begins][ends];
        }
        printf("Scenario #%d:\n", t);
        Finds(1);
    }
    return 0;
}

不喜欢这种题,不喜欢这种题,哥哥我不喜欢这种题

你可能感兴趣的:(poj,图论)