A1003 Emergency

发布时间 2023-06-03 10:11:57作者: Yohoc

题目:

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 N1), 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 c1c2 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

题目大意:n个城市m条路,所有的边的边权已知,每个城市有已知数量的救援小组。给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和。如果有多条最短路径就输出点权(城市救援小组数目)最大的那个

 

核心代码(Dijkstra算法):

 d[c1] = 0;
    W[c1] = w[c1];
    num[c1] = 1;
    for(int i = 0; i < n; i++){
        int u = -1, minn = inf;
        for(int j = 0; j < n; j++){
            if(visited[j] == false && d[j] < minn){
                u = j;
                minn = d[j];
            }
        }
        if(u == -1) break;
        visited[u] = true;
        for(int v = 0; v < n; v++){
            if(e[u][v] != inf && visited[v] == false){
                if(d[u] + e[u][v] < d[v]){
                    d[v] = d[u] + e[u][v]; // 最短路径的长度
                    num[v] = num[u]; // 最短路径的数目
                    W[v] = W[u] + w[v]; // 点权
                }else if(d[u] + e[u][v] == d[v]){ //当有多条最短路径时
                    num[v] += num[u]; // 最短路径的数目
                    if(W[u] + w[v] > W[v]){ // 更新点权(求多条对短路径中的最大点权)
                        W[v] = W[u] + w[v]; 
                    }
                }
            }
        }
    }

 

 

代码:(满分)

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf = 99999999;
int main(){
    int n, m, c1, c2;
    int w[505], e[505][505], d[505], W[505], num[505]={0};
    bool visited[505] = {0};
    fill(d, d + 505, inf);
    fill(e[0], e[0] + 505 * 505, inf);
    scanf("%d%d%d%d",&n, &m, &c1, &c2);
    for(int i = 0; i < n; i++){
        scanf("%d", &w[i]);
    }
    for(int i = 0; i < m; i++){
        int a, b, l;
        scanf("%d%d%d", &a, &b, &l);
        e[a][b] = e[b][a] = l;
    }
    d[c1] = 0;
    W[c1] = w[c1];
    num[c1] = 1;
    for(int i = 0; i < n; i++){
        int u = -1, minn = inf;
        for(int j = 0; j < n; j++){
            if(visited[j] == false && d[j] < minn){
                u = j;
                minn = d[j];
            }
        }
        if(u == -1) break;
        visited[u] = true;
        for(int v = 0; v < n; v++){
            if(e[u][v] != inf && visited[v] == false){
                if(d[u] + e[u][v] < d[v]){
                    d[v] = d[u] + e[u][v];
                    num[v] = num[u];
                    W[v] = W[u] + w[v];
                }else if(d[u] + e[u][v] == d[v]){
                    num[v] += num[u];
                    if(W[u] + w[v] > W[v]){
                        W[v] = W[u] + w[v];
                    }
                }
            }
        }
    }
    printf("%d %d", num[c2], W[c2]);
    return 0;
}