admin管理员组

文章数量:1577560

Emergency

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

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

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算法,具体可参考 《Dijkstra算法》。在最短路径基础上增加了对点权和及路径条数的判断。


代码实现

#include <cstdio>

const int maxn = 501;
const int INF = 1000000000;

int g[maxn][maxn], n;       // 邻接矩阵,结点个数
int d[maxn];                // 起始点到当前结点的距离
int num[maxn];              // 到达结点i的最短路径条数
int weight[maxn];           // 点权数组
int w[maxn];                // 到达结点i的点权和
bool visit[maxn];           // 判断是否已访问

void Dijkstra(int s)
{
    for (int i = 0; i < n; i++)     // 初始化
    {
        d[i] = INF;
        num[i] = 0;
        w[i] = 0;
        visit[i] = false;
    }
    d[s] = 0;               // 到自身距离为0    
    num[s] = 1;             // 当自身路径条数为1
    w[s] = weight[s];       // 路径点权和即为自身点权

    for (int i = 0; i < n; i++)     // n个结点最多循环n次
    {
        int index = -1, minD = INF;
        for (int j = 0; j < n; j++)     // 找到距离最短的未访问结点
        {
            if (visit[j] == false && d[j] < minD)
            {
                minD = d[j];
                index = j;
            }
        }

        if (index == -1)        // 如果没找到,所有未访问结点都与起始结点不连通,返回
            return;
        visit[index] = true;    // 找到结点,标记为已访问

        for (int j = 0; j < n; j++)     // 以index结点为新的接入点,遍历未访问结点,更新各项信息
        {
            if (visit[j] == false && g[index][j] < INF)     // index与j连通且j未访问
            {
                if (g[index][j] + d[index] < d[j])      
                {
                    d[j] = g[index][j] + d[index];      // 更新最短路径
                    num[j] = num[index];                // 更新最短路径条数
                    w[j] = w[index] + weight[j];        // 更新点权和
                }
                else if (g[index][j] + d[index] == d[j])
                {
                    num[j] += num[index];       // 最短路径条数增加
                    if (w[index] + weight[j] > w[j])        // 距离相等,优先选点权和更大的
                        w[j] = weight[j] + w[index];
                }
            }
        }
    }
}

int main()
{
    int m, c1, c2;
    int x, y, len;

    scanf("%d%d%d%d", &n, &m, &c1, &c2);

    for (int i = 0; i < n; i++)         // 初始化邻接矩阵
        for (int j = 0; j < n; j++)
            g[i][j] = INF;

    for (int i = 0; i < n; i++)
        scanf("%d", &weight[i]);
    for (int i = 0; i < m; i++)
    {
        scanf("%d%d%d", &x, &y, &len);
        g[x][y] = g[y][x] = len;
    }

    Dijkstra(c1);
    printf("%d %d", num[c2], w[c2]);

    return 0;
}

本文标签: Emergency