【PAT】1003. Emergency (25)【单源最短路问题/dijkstra算法】

题目描述:

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 FORMAT

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.

翻译:每个输入文件包括一组测试数据。对于每组测试数据,第一行包括4个可能的整数:N(<=500)——城市数量(并且城市编号从0到n-1),M——道路数量,C1,C2——分别为你所在的城市和你必须去营救的城市。接下来的一行包括N个整数,第i个整数是第i个城市中救援小组的数量。接下来的M行每一行都描述一条道路,每行包括三个数字c1,c2和L,分别为道路连接的两个城市和道路长度。数据保证至少存在一条从C1到C2的路。

OUTPUT FORMAT

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之间不同的最短路的数量,和不解与你可能召集的救援小组的最大数量。一行内的所有数字必须用一个空格隔开,并且末尾没有多余的空格。


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


解题思路:

dijkstra模板算法,有兴趣的可以看一下我写的【模板算法】单源最短路问题-dijkstra算法(邻接表+优先队列优化),在这里需要改变的就是计算最短路个数,如果长度相同就加一,如果更新就变为1。
注意:有可能C1、C2为同一城市!!!巨坑,明明题目说的是some other,就是某个其他城市。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,M,C1,C2,ccount=0; 
struct Team{
    int length,hands,to;
    Team(int a,int b,int c):length(a),hands(b),to(c){}
    bool operator<(const Team &a)const{
        return length==a.length?hands>a.hands:length<a.length;  
    } 
};
struct Edge{
    int length,to;
    Edge(int a,int b):length(a),to(b){}
};
int city[510],d[510][2];
vector<Edge>G[510];
priority_queue<Team> q;
void dijkstra(){
    for(int i=0;i<N;i++)d[i][0]=INF;
    d[C1][0]=0;
    d[C1][1]=city[C1];
    q.push(Team(0,city[C1],C1));
    while(!q.empty()){
        Team temp=q.top();q.pop();
        if(d[temp.to][0]<temp.length)continue;
        for(int i=0;i<G[temp.to].size();i++){
            Edge e=G[temp.to][i];
            if(d[e.to][0]>d[temp.to][0]+e.length){
                if(e.to==C2)ccount=1;
                d[e.to][0]=d[temp.to][0]+e.length;
                d[e.to][1]=d[temp.to][1]+city[e.to];
                q.push(Team(d[e.to][0],d[e.to][1],e.to));
            }
            else if(d[e.to][0]==d[temp.to][0]+e.length){
                if(e.to==C2){
                    ccount++;
                }
                if(d[e.to][1]<d[temp.to][1]+city[e.to])
                d[e.to][1]=d[temp.to][1]+city[e.to];
                q.push(Team(d[e.to][0],d[e.to][1],e.to));
            }   
        } 
    }
}
int main(){
    scanf("%d%d%d%d",&N,&M,&C1,&C2);
    for(int i=0;i<N;i++)
    scanf("%d",&city[i]);
    int a,b,c;
    for(int i=0;i<M;i++){
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back(Edge(c,b));
        G[b].push_back(Edge(c,a));
    }
    if(C1!=C2)
    dijkstra();
    else ccount=1,d[C2][1]=city[C1];
    printf("%d %d\n",ccount,d[C2][1]);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值