zoukankan      html  css  js  c++  java
  • 1003 Emergency

    题目:

    As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

    Input Specification:

    Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1c2and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

    Output Specification:

    For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

    Sample Input:

    5 6 0 2

    1 2 1 5 3

    0 1 1

    0 2 2

    0 3 1

    1 2 1

    2 4 1

    3 4 1

    Sample Output:

    2 4

    题目大意:

    给一个图,求起点终点之间的最短路径有几条,并且找出最短路径中能找到的最多的人。

    解题思路:

    采用迪杰克斯拉算法,需要注意的是最小值的初值不能给的太大,比如0x7fffffff(int所能表示的最大的数),当做加法运算时可能导致越界,从而取到负值,导致程序错误。一开始用dfs的方法,但题意没对。

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #define MaxSize 600
    #define INF 9999999
    using namespace std;
    int n, m, c1, c2;
    int graph[MaxSize][MaxSize];
    int rescue[MaxSize];
    int dist[MaxSize], team[MaxSize];
    int path[MaxSize];
    void Dijkstra()
    {
        dist[c1] = 0;
        team[c1] = rescue[c1];
        int set1[MaxSize];
        int min, i, j, u = 0;
        for (i = 0; i < n; i++)
        {
            dist[i] = graph[c1][i];
            if (dist[i] != INF && i != c1)
                team[i] = team[c1] + rescue[i];
            set1[i] = 0;
        }
        set1[c1] = 1;
        for (i = 0; i < n; ++i)
        {
            min = INF;
            for (j = 0; j < n; ++j)
            {
                if (set1[j] == 0 && dist[j] < min)
                {
                    u = j;
                    min = dist[j];
                }
            }
            set1[u] = 1;
            //以u为中间节点考虑未加入的结点
            for (j = 0; j < n; ++j)
            {
                if (set1[j] == 0 && dist[u] + graph[u][j] < dist[j])
                {
                    dist[j] = dist[u] + graph[u][j];
                    team[j] = team[u] + rescue[j];
                    path[j] = path[u];
                }
                else if (set1[j] == 0 && dist[u] + graph[u][j] == dist[j])
                {
                    path[j] += path[u];
                    if (team[j] < team[u] + rescue[j])
                        team[j] = team[u] + rescue[j];
                }
            }
        }
    }
    int main()
    {
        cin >> n >> m >> c1 >> c2;
        int i;
        for (i = 0; i < n; i++)
        {
            cin >> rescue[i];
        }
        int a, b;
        for (i = 0; i < n; i++)
        {
            dist[i] = INF;
            path[i] = 1;
            team[i] = rescue[i];
            for (int j = 0; j < n; j++)
                graph[i][j] = INF;
        }
        for (i = 0; i < m; i++)
        {
            cin >> a >> b;
            cin >> graph[a][b];
            graph[b][a] = graph[a][b];
        }
        Dijkstra();
        cout << path[c2] << " " << team[c2] << endl;
        return 0;
    }

    感想:

    第一次把学得图论用作解题方法,还是看着别人的博客写出来的,现在还是太菜了,但不管怎么说,做了总比没做强。算法在我心中一直是道坎,希望通过刷pat,虽然达不到玩ACM的大佬级别,但至少学过的算法起码能实际应用。遇到困难不要放弃啊,多看看别人怎么做的,毕竟玩了1000多场劫也上不去王者,不也还在玩吗。

  • 相关阅读:
    php打印出10*10表格
    php打印出1到2000年之间所有的闰年
    借鉴一篇好文章
    女程序员的预备篇
    SQL存储过程删除数据库日志文件的方法
    Mongodb无法访问28107的问题
    使用 xsd.exe 命令工具将 xsd 架构生成 类(CS) 文件
    C# 用POST提交json数据
    WinForm 使用 HttpUtility
    Sql Server 分区之后增加新的分区
  • 原文地址:https://www.cnblogs.com/stormax/p/10914666.html
Copyright © 2011-2022 走看看