• 周一. 8月 15th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

1003 Emergency (25 分)

admin

11月 28, 2021

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

Submit(C++):

#include <iostream>
#include <algorithm>
#define MaxSize 510
using namespace std;
//题目意思:求最短路径的的个数和最短路径下救援数量
//第一行: N个城市,M条道路,C1和C2起始城市和终点城市
//第二行: 救援队的数量及所在的城市(下标) 对应N
//下边M行:表示城市1 城市2 两城市的路径距离 对应M

//初始化顶点数和边数;初始化顶点;初始化边
//获取权重 //Dijkstra最短路径算法 最短路径不止一条,需要对算法稍稍改进 //
int n,m,c1,c2;
int e[MaxSize][MaxSize],weight[MaxSize],dis[MaxSize],num[MaxSize],w[MaxSize];
const int inf = 99999999;
bool visit[MaxSize];//存放访问标记
int main() {
    int i,j;
    scanf("%d %d %d %d",&n,&m,&c1,&c2);//输入第一行
    for (i=0;i<n;i++) {//输入第二行
        scanf("%d ",&weight[i]);
    }
    fill(e[0],e[0]+MaxSize*MaxSize,inf);//填充最大值,默认无穷大
    fill(dis,dis+MaxSize,inf);//填充最大值
    int a,b,c;
    for (j=0;j<m;j++) {//输入M列
        scanf("%d %d %d",&a,&b,&c);
        e[a][b] = e[b][a] = c;//a->b的距离 = b->a的距离 = L,双向
    }
    dis[c1] = 0;//dis[i]表示从出发点c1到i节点最短路径的路径长度
    w[c1] = weight[c1];//w[i]表示从出发点c1到i点救援队的数目之和
    num[c1] = 1;//num[i]表示从出发点c1到i节点最短路径的条数
    for (i=0; i<n; i++) {
        int u = -1,min = inf;
        for (j=0;j<n;j++) {
            if (visit[j] == false && dis[j] < min) {
                u = j;
                min = dis[j];
            }
        }
        if (u == -1) break;
        visit[u] = true;
        for (int v = 0; v<n; v++) {
            if ( dis[v] > dis[u] + e[u][v]) {
                dis[v] = dis[u] + e[u][v];//取路径长度最小值
                num[v] = num[u];
                w[v] = w[u] + weight[v];
            } else if (dis[v] == dis[u] + e[u][v]) {
                num[v] = num[v] + num[u];
                if (w[v] < w[u] + weight[v]) w[v] = w[u] + weight[v];//取救援最大值
            }
        }
    }
    printf("%d %d",num[c2],w[c2]);
    return 0;
} 

参考:

柳婼-https://blog.csdn.net/liuchuo/article/details/54561626

昵称五个字-https://blog.csdn.net/a617976080/article/details/89676670

发表回复

您的电子邮箱地址不会被公开。