PAT甲级.:1003 Emergency (25point(s))

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 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 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 <iostream>
#include<stdio.h>
using namespace std;
#pragma warning(disable:6031)
#define INF 21474836455

int rt[505];//rescue teams 
int allrt[505];
int pathcnt[505]; //最短路径个数

struct path {
    long long pathlength = INF;
}road[505][505];
int flag[505];

void dijkstra(int begin, int end, int  count)//flag标识该节点不在最短备选内
{   
    pathcnt[begin] = 1; /
    flag[begin] = 1;
    for (int i = 0; i < count; i++)
    {
        if (road[begin][i].pathlength < INF)
        {
            allrt[i] = rt[i];
            pathcnt[i] = pathcnt[begin];
        }
    }
    for (int i = 0; i < count; i++)
    {
        long long minpath = INF;
        int bestcity =0;
        for (int j = 0; j < count; j++)
        {
            if (!flag[j]) {
                if (road[begin][j].pathlength < minpath)//如果起点到j这个城市的距离最小
                {
                    minpath= road[begin][j].pathlength;
                    bestcity = j;            
                }            
            }
        }
        flag[bestcity] = 1;
        if (minpath == INF)
            return;
        for (int j = 0; j < count; j++)
        {
            if (j!=bestcity&&road[bestcity][j].pathlength!=INF) {
                if (road[begin][j].pathlength > road[begin][bestcity].pathlength + road[bestcity][j].pathlength)//如果起点到j这个城市的距离大于bestcity到j的距离+起点到bestcity的距离
                {
                    road[begin][j].pathlength = road[begin][bestcity].pathlength + road[bestcity][j].pathlength;//修改begin到j的距离
                    allrt[j] = rt[j] + allrt[bestcity];//修改j的救援队数量
                    pathcnt[j] = pathcnt[bestcity];
                }
                else if ((road[begin][j].pathlength == road[begin][bestcity].pathlength + road[bestcity][j].pathlength) )
                {  
                    pathcnt[j] += pathcnt[bestcity];
                    if((allrt[bestcity] + rt[j]) > allrt[j])
                        allrt[j] = rt[j] + allrt[bestcity];//修改j的救援队数量
                }
            }
        }
    }
}
int main(){

    int CitysCount;
    int RoadsCount;
    int begin;
    int end;
    cin >> CitysCount >> RoadsCount >> begin >> end;
    int origin;
    int terminal;
    int roadlength;
    for (int i = 0; i < CitysCount; i++)
    {
        cin >> rt[i];
        road[i][i].pathlength = 0;
    }
    for (int i = 0; i < RoadsCount; i++)
    {
        cin >> origin >> terminal >> roadlength;
        road[origin][terminal].pathlength = road[terminal][origin].pathlength = roadlength;
    }
    if (begin == end) {
        cout << 1 << " " << rt[begin];
        return 0;
    }
    dijkstra(begin, end, CitysCount);
    cout <<pathcnt[end] << " " << allrt[end]+rt[begin];
    return 0;
}

假设对于终点,我们已知有N种到达路线(是否最短尚未知,但至少是我们目前最短的路线),如果我们发现终点还连接着一个点A,它到终点时的总长度也和我们已知的N种路线相同,那么对于终点,就有N+M种到达方法,其中M表示M种到达点A的方法,对应pathcnt[j] += pathcnt[bestcity];

如果A到终点比已知的N种路径更短,那么我们可以将N替换成M(因为有更短的方法)。对应pathcnt[j] = pathcnt[bestcity];

于是我们发现,求到达终点的最短路径数已经转换成了求其连接的点最短路径数,也就是说,对于一开始,我们大可以放心地将到达的每个点都视情况标为最短(目前已知),并在后续的计算中通过其他点到达该点的总路径长度是不是最短而更新到该点的最短路径数和最短路径长度

猜你喜欢

转载自www.cnblogs.com/troycoder/p/12357048.html
今日推荐