PAT (Advanced Level) 甲级 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: 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 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.

 这题是个很经典的问题,求出从C1到C2的最短路,并且要保证,每次经过每一个节点的值总和要最大,并且求出最短路径的个数。比较经典的迪杰斯特拉的变形吧。

对于路径个数的话,我采用的是乘法原则,就是求出到达每个节点的最短路径个数,然后将这些进行相乘,也就是每次找到相同最短的,就加上一遍,具体是用droad实现的。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 506;
const int inf = 1e9;
//从一个点到另一个点的 最短路的条数 以及收集的最多医疗队

int res[maxn];
int mp[maxn][maxn];
int N,M,C1,C2;

int d[maxn];//里面表示的是该点到C1的最短距离
int dres[maxn];
int droad[maxn];
int vis[maxn];

void Dij()
{
    for(int i = 0;i<N;i++)
    {
        d[i] = inf;
    }

    d[C1] = 0;//自己到自己距离为0
    dres[C1] = res[C1];
    droad[C1] = 1;
    for(int i = 0;i<N;i++)
    {
        //每次寻找一个最小的 且未访问的
        int u = -1,mn = inf;
        for(int j = 0;j<N;j++)
        {
            if(vis[j]==0 && d[j]<mn)
            {
                u = j;
                mn = d[j];
            }
        }
// cout << u <<"--";
        if(u==-1) return;
        vis[u] = 1;//代表已经访问

        for(int v = 0;v<N;v++)
        {
            if(mp[u][v]!=inf && d[u]+mp[u][v]<d[v] && vis[v]==0)
            {
                d[v] = mp[u][v]+d[u];
                //这个是判断最大医疗 下面就剩条数问题了
                dres[v] = dres[u]+res[v];
                
                droad[v]=droad[u];
                
            }else if(mp[u][v]!=inf && d[u]+mp[u][v]==d[v]&& vis[v]==0)
            {
                if(dres[v]<dres[u]+res[v])
                {
                    dres[v] = dres[u]+res[v];
                    // cout << u <<" "<<v<<"\n";
                }
                
                droad[v]+=droad[u];
            }
        }
        
    }

}

int main()
{
    
    cin >> N >> M >> C1 >> C2;
    for(int i = 0;i<N;i++)
    {
        for(int j = 0;j<N;j++)
            mp[i][j] = inf;
    }
    for(int i = 0;i<N;i++) cin >> res[i];
    for(int i = 0;i<M;i++)
    {
        int a,b,c;
        cin >> a >> b >> c;
        mp[a][b] = c;
        mp[b][a] = c;
    }
    Dij();
    cout << droad[C2]<<" "<<dres[C2];
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值