题目
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.
输入
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 integersC1, 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.
输出
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.
样例输入
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
样例输出
2 4
题意理解
题意:输入n个点m条边,输入起点和终点,输入每个点的救援队人数,也就是抽象成结点权重,接下来有m条边,结点编号1和结点编号2,边权,此题注意的是结点是从0开始的,也就是结点编号从0到n-1,要注意这点。最后输出的是从起点到终点最短路径上面的最短路数目,第二个输出是从起点到终点的最短路上召集的最多救援队人数,即最大权重。
思路:我们用Dijkstra算法跑最短路,dis数组记录最短路,cnt数组记录的是到达该点的最短路数目,res记录从起点到终点最大权重,如果每次迭代的时候最短路径更新,那么我们就更新到达该点的最短路数目,最大权重,如果迭代的时候碰到不同路径的最短路,但是路径长度相同,那么我们就加上此时最短路径的数目,判断这个结点从路径不同但路径长度相同的路径转移过来的权重是否大于其他路径转移过来的权重,如果大于则更新。
最后要注意的就是,这是一个无向图,建两条边。
注:笔者这里用的是堆优化版本的Dijkstra算法
代码
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=510,M=510*510;
typedef pair<int,int> PII;
#define fx first
#define fy second
int n,m;
int h[N],e[M],w[M],ne[M],idx;//链式前向星
void add(int a,int b,int c){
e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
int ww[N];//结点的权重
int dis[N],res[N];//距离数组 到达终点的最大权重
int cnt[N];//路径数目
bool st[N];//判重数组
void dijkstra(int ss,int ee){
memset(dis,0x3f,sizeof dis);
priority_queue<PII,vector<PII>,greater<PII> >heap;
heap.push({0,ss});
dis[ss]=0;cnt[ss]=1;
res[ss]=ww[ss];
while(heap.size()){
PII t=heap.top();
heap.pop();
int ve=t.fy;
if(st[ve])continue;
st[ve]=1;
for(int i=h[ve];~i;i=ne[i]){
int j=e[i],c=w[i];
if(dis[j]>dis[ve]+c){
dis[j]=dis[ve]+c;
heap.push({dis[j],j});
cnt[j]=cnt[ve];
res[j]=res[ve]+ww[j];
}
else if(dis[j]==dis[ve]+c){
cnt[j]=cnt[j]+cnt[ve];
if(res[j]<res[ve]+ww[j])
res[j]=res[ve]+ww[j];
}
}
}
printf("%d %d\n",cnt[ee],res[ee]);
}
int main(){
memset(h,-1,sizeof h);
scanf("%d%d",&n,&m);
int ss,ee;
scanf("%d%d",&ss,&ee);
for(int i=0;i<n;i++)
scanf("%d",&ww[i]);
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dijkstra(ss,ee);
return 0;
}