Pat 1003 emergency (25 points)

Time:2021-8-27

Pat 1003 emergency (25 points)

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

thinking

  • This problem is a conventional shortest path problem. You can use the adjacency matrix to store the graph.
  • be careful:

    • Statistics of the maximum number of people per vertex.
    • Statistics of the number of shortest paths from V to u.

code

#pragma warning (disable:4996)
#include <cstdio>
#include <algorithm>

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];    // Number of people
int w[maxn];    // Maximum number of people
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;
}

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]