1003 Emergency (25 分)

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, c​2 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 C1and​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

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 1000000
using namespace std;
int a[1005][1005];
int savtem[1005];//每个村庄有多少个救援队
int dis[1005];//起点S到村庄j的最短距离
int get[1005];//起点S到村庄j可以聚集救援队的数量
int flag[1005];
int cnt[1005];//起点S到村庄j的最短路径有多少条
void dijkstra(int start, int n)
{
    int i, j, k, min;
    for (i = 0; i < n; i++)//(初始化)存放起点到其余顶点的距离
        dis[i] = a[start][i];
dis[start] = 0;
get[start] = savtem[start];
cnt[start] = 1;

for (i = 0; i < n; i++)
{
    min = MAX;
    k = -1;
    for (j = 0; j < n; j++) //求出初始起点s直接到j点距离最短的点的下标值
    {
        if (flag[j]==0 && min > dis[j])
        {
            min = dis[j];
            k = j;
        }
    }
    flag[k] = 1;
    if (k == -1)
        return;
    for (j = 1; j <= n; j++)
    {
        if (dis[j] > dis[k] + a[k][j])//若找到其他途径比从1号顶点直接到目的顶点的距离短,则替换掉
        {
            cnt[j]=cnt[k];
            dis[j] = dis[k] + a[k][j];
            get[j]=savtem[j]+get[k];
        }

        else if (dis[j] == dis[k] + a[k][j])//如果距离相同
        {
            cnt[j]=cnt[j]+cnt[k];
            get[j] = max(get[j],get[k] + savtem[j]);//更新救援队数量
        }
    }
}
}

int main()
{
    int n, m;
    int i;
    int s, e;
    cin>>n>>m>>s>>e;
    for(int i=0;i<n;i++)
        cin>>savtem[i];
    int t1,t2,t3;
    memset(flag, 0, sizeof(flag));
    memset(cnt, 0, sizeof(cnt));
    memset(cnt, 0, sizeof(get));
    memset(a, MAX, sizeof(a));//初始化所有点的距离/花费为无穷大
    for (i = 0; i < m; i++)
    {
        scanf("%d%d%d", &t1, &t2, &t3);
        if (a[t1][t2] > t3)//去重
            a[t1][t2] = a[t2][t1] = t3;
    }
    dijkstra(s, n);
    printf("%d %d\n", cnt[e], get[e]);
    return 0;
}

详细分析周末再整理:)

上一篇:正则表达式


下一篇:正则表达式