PTA A1003&A1004

the next day

A1003 Emergency (25 分)

Title Contents

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 c1, c2 and 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

word

emergency

英 / ɪmɜdʒ (g) nsɪ / 美 / ɪmɝdʒənsi /

n emergency;. emergencies; very moment
adj emergency;. standby

rescue

English / 'reskjuː / US /' rɛskju /

. N rescue, rescue, rescue; rescue operation

. V rescue, rescue; (informal) to prevent loss ......

scattered

English / 'skætəd / US /' skætɚd /
. ADJ dispersed; scattered

at the mean time

Simultaneously

call up

Call; convened; so think; proposed

as many hands

As much manpower

guarantee

英 / Gaer (a) nti / 美 / ɡærənti /

. n guarantee; guarantees; surety; guarantee; collateral
. vt assurance; guarantee

Topic analysis

This title is a normal single-source shortest path problem, using Dijkstra's algorithm can be solved, but the refresh dist (reach every point of the shortest path) of the way when you need to refresh num (the number of the most shorted) and rescue (to a point the number of rescue teams) number, the number of the shortest on the num, I began to think is relatively simple, using only a minimum number of integer variable to monitor the shorting bar when reach the end, in fact, the number of pieces on the shortest Path and the number of shortest length and rescue teams, like the need to constantly refresh to get the final result.

Specific code

#include&ltstdio.h>
#include&ltstdlib.h>
#include&ltlimits.h>
#define MAXSIZE 500

int road[MAXSIZE][MAXSIZE];
int team[MAXSIZE];
int rescue[MAXSIZE];
int is_collect[MAXSIZE];
int is_visited[MAXSIZE];
int dist[MAXSIZE];
int num[MAXSIZE];
int N, M, C1, C2;

void Dijkstra();

int main(void)
{
    scanf("%d %d %d %d", &N, &M, &C1, &C2);
    for (int i = 0; i < N; i++)
        scanf("%d", &team[i]);
    for (int i = 0; i < N; i++)
    {
        dist[i] = INT_MAX;
    }
    for (int i = 0; i < M; i++)
    {
        int c1, c2, l;
        scanf("%d %d %d", &c1, &c2, &l);
        road[c1][c2] = l;
        road[c2][c1] = l;
    }
    Dijkstra();
    printf("%d %d", num[C2], rescue[C2]);
    system("pause");
}

int is_trave()
{
    int flag = 1;
    for (int i = 0; i < N; i++)
    {
        if (is_visited[i] != 0 && is_collect[i] == 0)
        {
            flag = 0;
            break;
        }
    }
    return flag;
}

int find_min()
{
    int temp = 0;
    int min = INT_MAX;
    for (int i = 0; i < N; i++)
    {
        if (is_visited[i] == 1 && is_collect[i] == 0)
        {
            if (dist[i] < min)
            {
                min = dist[i];
                temp = i;
            }
        }
    }
    return temp;
}

void Dijkstra()
{
    is_collect[C1] = 1;
    rescue[C1] = team[C1];
    num[C1] = 1;
    for (int i = 0; i < N; i++)
    {
        if (road[C1][i] != 0)
        {
            is_visited[i] = 1;
            dist[i] = road[C1][i];
            num[i] = num[C1];
            rescue[i] = rescue[C1] + team[i];
        }
    }
    while (!is_trave())
    {
        int m = find_min();
        is_collect[m] = 1;
        for (int i = 0; i < N; i++)
        {
            if (road[m][i] != 0&&is_collect[i]==0)
            {
                if (dist[m] + road[m][i] < dist[i])
                {
                    num[i] = num[m];
                    dist[i] = dist[m] + road[m][i];
                    rescue[i] = rescue[m] + team[i];
                }
                else if (dist[m] + road[m][i] == dist[i])
                {
                    num[i] = num[i] + num[m];
                    if (rescue[m] + team[i] > rescue[i])
                        rescue[i] = rescue[m] + team[i];
                }
                is_visited[i] = 1;
            }
        }
    }
}

Reference blog

1003. Emergency (25) -PAT Grade A true problem (Dijkstra algorithm)

Guess you like

Origin www.cnblogs.com/z-y-k/p/11519353.html