admin管理员组

文章数量:1577552

1003 Emergency (25分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
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, C​1 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 C​1 and C​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

题意:

给出城市数量N,道路数量M,起点C1,终点C2,每个城市拥有若干救援队,以及给出哪那几个城市想通(c1,c2)相距(l),经过城市救援队汇合,求从城市C1到城市C2的最短路径的个数,并求出最短路径上能拥有最多的救援队个数。

思路:

狄杰斯特拉最短路径问题。套用模板,由于题目要求计算最短路径的个数,需要引入变量num[],初始化为num[st]=1,其余为0,放在最短路径中更新。

参考代码:

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=501;
const int INF=1e9;
int G[maxn][maxn],a[maxn];
int d[maxn],team[maxn]={0},num[maxn]={0};
bool flag[maxn]={false};
int n,m,C1,C2;
void dij(int st){
    fill(d,d+maxn,INF);
    d[st]=0;
    team[st]=a[st];
    num[st]=1;
    for(int i=0;i<n;i++){
        int u=-1,MIN=INF;
        for(int j=0;j<n;j++){
            if(!flag[j]&&d[j]<MIN){
                u=j;
                MIN=d[j];
            }
        }
        if(u==-1) return;
        flag[u]=true;
        for(int v=0;v<n;v++){
            if(!flag[v]&&G[u][v]!=INF){
                if(d[u]+G[u][v]<d[v]){
                    d[v]=d[u]+G[u][v];
                    team[v]=team[u]+a[v];
                    num[v]=num[u];
                }
                else if(d[u]+G[u][v]==d[v]){
                    if(team[u]+a[v]>team[v])
                        team[v]=team[u]+a[v];
                    num[v]+=num[u];
                }
            }
        }
    }
}
int main() {
    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",&a[i]);
    int c1,c2,l;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&c1,&c2,&l);
        G[c1][c2]=l;
        G[c2][c1]=l;
    }
    dij(C1);
    printf("%d %d\n",num[C2],team[C2]);
    return 0;
}

如有错误,欢迎指正

本文标签: PATEmergency