PAT 1003 Emergency(25分)

PAT 1003 Emergency(25分)

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 toN−1),M- the number of roads,C​1​​andC​2​​- the cities that you are currently in and that you must save, respectively. The next line containsNintegers, where thei-th integer is the number of rescue teams in thei-th city. ThenMlines follow, each describes a road with three integersc​1​​,c​2​​andL, 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 fromC​1​​toC​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths betweenC​1​​andC​2​​, 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

思路

  • 本题是常规的最短路径题目,可以使用邻接矩阵存储图。
  • 注意:

    • 每个顶点最大人数的统计。
    • 从v到u最短路径条数的统计。

代码

#pragma warning (disable:4996)
#include 
#include 

using namespace std;

const int maxn = 510;
const int INF = 0x3f3f3f3f;

int G[maxn][maxn], d[maxn], num[maxn];
bool vis[maxn];
int weight[maxn];    //人数
int w[maxn];    //最大人数
int n, m, c1, c2;
//int ans = 1;
void Dijkstra(int s)
{
    fill(d, d + maxn, INF);
    d[s] = 0;
    num[s] = 1;
    w[s] = weight[s];
    for (int i = 0; i < n; i++)
    {
        int u = -1, MIN = INF;
        for (int j = 0; j < n; j++)
        {
            if (vis[j] == false && d[j] < MIN)
            {
                u = j;
                MIN = d[j];
            }
        }
        if (u == -1)
        {
            return;
        }
        vis[u] = true;

        for (int v = 0; v < n; v++)
        {
            if (vis[v] == false && G[u][v] != INF)
            {
                if (d[u] + G[u][v] < d[v])
                {
                    d[v] = d[u] + G[u][v];
                    w[v] = weight[v] + w[u];
                    num[v] = num[u];
                }
                else if (d[u] + G[u][v] == d[v])
                {
                    if (w[u] + weight[v] > w[v])
                        w[v] = w[u] + weight[v];
                    num[v] += num[u];
                }
            }
        }
        
    }
}

int main()
{
    //freopen("test.txt", "r", stdin);

    fill(G[0], G[0] + maxn * maxn, INF);
    scanf("%d %d %d %d", &n, &m, &c1, &c2);

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &weight[i]);
    }
    int n1, n2, l;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d %d", &n1, &n2, &l);
        G[n1][n2] = l;
        G[n2][n1] = l;
    }

    Dijkstra(c1);
    printf("%d %d", num[c2], w[c2]);

    return 0;
}

你可能感兴趣的:(算法c++最短路径)