1003. Emergency (25)

来源:互联网 发布:office苹果mac版下载 编辑:程序博客网 时间:2024/05/08 16:36

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

题目要求:存在多条最短路径,输出其中号召的消防员数量最多的路径,需要注意起点和终点的消防员都需要累加进消防队伍中。

#include <stdio.h>#include <stdlib.h>#define inf 99999999int g[510][510]; //两个顶点之间边的权重int numTeam[510], dist[510] = {0}, collected[510] = {0}, hands[510] = {0}, route[510] ={0};//numTeam是每个顶点的权值(每个城市消防员的数量),dist是每个起点到i的最短距离,collected表示当前//顶点是否已经输入。hands表示起点到i的过程中找到的消防员的总和(包括起点和i),route表示从起点到//i的路线数量。int findMinDist(int n, int e){    int minV, v, minDist = inf;    for(v = 0; v < n; v++)    {        if(collected[v] == 0 && dist[v] < minDist)        {            minDist = dist[v];            minV = v;        }    }    if(minDist < inf)        return minV;    else        return -1;}void solve(int n, int s, int end){    int v, w, i;    for(i = 0; i < n; i++)        dist[i] = g[s][i];    hands[s] = numTeam[s];    dist[s] = 0;    route[s] = 1; //以上是各个数组的初始化    //由于要显示路线的数量需要v从起点开始,这与陈姥姥的Dijkstra算法略有区别    while(1)    {        v = findMinDist(n, end);        if(v == -1)            break;        collected[v] = 1;        for(w = 0; w < n; w++)        {            if(collected[w] == 0 && g[v][w] < inf)            {                if(dist[v] + g[v][w] < dist[w])                {                    dist[w] = dist[v] + g[v][w];                    route[w] = route[v];                    hands[w] = hands[v] + numTeam[w];                }                else if(dist[v] + g[v][w] == dist[w])                {                    route[w] += route[v];                    if(hands[v] + numTeam[w] > hands[w])                        hands[w] = hands[v] + numTeam[w];                }            }        }    }}int main(){    int n, m, v1, v2, i, j;    scanf("%d %d %d %d", &n, &m, &v1, &v2);    for(i = 0; i < n; i++)        scanf("%d", &numTeam[i]);    for(i = 0; i < n; i++)        for(j = 0; j < n; j++)            g[i][j] = inf;    for(i = 0; i < m; i++)    {        int c1, c2, l;        scanf("%d %d %d", &c1, &c2, &l);        g[c1][c2] = l;        g[c2][c1] = l;    }    solve(n, v1, v2);    printf("%d %d", route[v2], hands[v2]);    return 0;}
0 0
原创粉丝点击