[PAT甲级]1003 Emergency

求最短路径的问题,用dj算法来解决,注意添加了条件,当路径长度相等时,优先选择救援队较多的路径;其次,要算出最短路径的条数,可以设置num_t[100]数组,记录从开始结点到当前结点 i 的最短路径的条数,而后面的相邻结点 j 若可以通过 i 以最短路径长度到达则 += num_s[i]即可,每次获得一个newP即是知道它的所有最短路径,找出所有能直连它的已经加入到newP过的即可。

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 C​2​​ - 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 c​1​​, 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 C​1​​ to C​2​​.

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

输出的是最短路径的条数,以及最短路径的最多救援队数,代码为:

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;

struct E{
    int next;
    int c;
    int num;//走这条路可以获得的援救队伍数量,只要加上next的数量即可,开始时记录开始结点的队伍的数量
};

vector<E> edge[100000];
bool mark[501];//是否已经访问过
int Dis[501];//由开始结点到相应结点的当前已知最短距离
int num_t[501];//第i座城市的救援队数量
int Count[501];//从开始结点到当前城市的总救援队数量
int num_s[501] = {0};//从开始结点到当前结点最短的路线共有几条

int main()
{
    int N, M, C1, C2;
    scanf("%d%d%d%d", &N, &M, &C1, &C2);
    for(int i = 0; i < N; i++)
    {
        scanf("%d", &num_t[i]);
    }
    for(int i = 0; i < M; i++)
    {
        int s, e, cost;
        scanf("%d%d%d", &s, &e, &cost);
        E temp;
        temp.next = e;
        temp.c = cost;
        temp.num = num_t[e];
        edge[s].push_back(temp);
        temp.next = s;
        temp.num = num_t[s];
        edge[e].push_back(temp);
    }
    for(int i = 0; i < N; i++)
    {
        Dis[i] = -1;
        mark[i] = false;
    }
    mark[C1] = true;
    Dis[C1] = 0;
    Count[C1] = num_t[C1];
    int newP = C1;
    num_s[C1] = 1;
    for(int i = 1; i < N; i++)
    {
        for(int j = 0; j < edge[newP].size(); j++)
        {
            int t = edge[newP][j].next;
            int co = edge[newP][j].c;
            int n = edge[newP][j].num;
            if(mark[t] == true) continue;
            if(Dis[t] == -1 || Dis[t] > Dis[newP] + co || (Dis[t] == Dis[newP] + co && Count[newP] + n > Count[t]))
            {
                Count[t] = Count[newP] + n;
                Dis[t] = Dis[newP] + co;
            }
        }
        int minV = 0x7fffffff;
        for(int k = 0; k < N; k++)
        {
            if(mark[k] == true) continue;
            if(Dis[k] == -1) continue;
            if(minV > Dis[k])
            {
                newP = k;
                minV = Dis[k];
            }
        }
        //新加入一个已知最短路径,则从开始结点到 newP 的结点都是已经知道的
        for(int j = 0; j < N; j++)
        {
            if(mark[j] == true && j != newP)
            {
                for(int k = 0; k < edge[j].size(); k++)
                {
                    if(newP == edge[j][k].next && Dis[newP] == Dis[j] + edge[j][k].c)
                    {
                        num_s[newP] += num_s[j];
                    }
                }
            }
        }
        mark[newP] = true;
    }
    printf("%d %d\n", num_s[C2], Count[C2]);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值