PAT A 1003 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 Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: () - the number of cities (and the cities are numbered from 0 to ), - the number of roads, and - the cities that you are currently in and that you must save, respectively. The next line contains in‐tegers, where the -th integer is the number of rescue teams in the -th city. Then lines follow, each describes a road with three integers , and , which are the pair of cities connected by a road and the length of that road, respectively. It is guaran‐teed that there exists at least one path from to .
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between and , and the maximum amount of rescue teams you can possibly gather. All the num‐bers 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

题目大意大意就是给出了每个城市的team数量 给出了哪两个城市两两连接 以及两两连接之间的路径长度

问从C1到C2这两个城市之间最短路径的条数(注意!!!不是问最短路径的长度 是问有几条最短路径)
问最短路径中的最大的team数的和是多少

解题思路:因为数据量不大 就用邻接矩阵存储 dijkstra思想 另外开一个数组dcnt存路径条数 开一个数组teams存team总和

代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int n,m;
int c1,c2;
const int M = 510;
int g[M][M];
int d[M];//到c1的距离
int team[M];//每个城市的team
int teams[M];//从c1出发可以获得的最大team数量
const int INF = 0x3f3f3f3f;
bool st[M];
int dcnt[M];//最短路径条数

void dijkstra(int u)
{
    memset(d,0x3f,sizeof d);
    memset(teams,0,sizeof teams);
    memset(dcnt,0,sizeof dcnt);
    teams[u] = team[u];
    d[u] = 0;
    dcnt[u] = 1;
    for(int i=0;i<n;i++)
    {
        int t = -1;
        int minn = INF;
        for(int j=0;j<n;j++)
        {
            if(!st[j] && d[j] < minn)
            {
                t = j;
                minn = d[j];
            }
        }
        
        if(t == -1) return;
        
        st[t] = true;
        for(int j=0;j<n;j++)
        {
            if(!st[j] && g[t][j] != 0)
            {
                if(d[t]+g[t][j] < d[j])
                {
                    d[j] = d[t] + g[t][j];
                    dcnt[j] = dcnt[t];
                    teams[j] = teams[t] + team[j];
                }
                else if(d[t]+g[t][j] == d[j])
                {
                    dcnt[j] += dcnt[t];
                    if(teams[t]+team[j] > teams[j])
                    {
                        teams[j] = teams[t] + team[j];
                    }
                }
            }
        }
    }
}

int main()
{
    cin>>n>>m;
    cin>>c1>>c2;
    for(int i=0;i<n;i++) cin>>team[i];
    while(m--)
    {
        int a,b,x;
        cin>>a>>b>>x;
        g[a][b] = g[b][a] = x;
    }
    
    dijkstra(c1);

    cout<<dcnt[c2]<<' '<<teams[c2]<<endl;
    
    return 0;
}

写在结尾:推荐歌曲:网易云《温暖的房子》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cwn_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值