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 (≤) - 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
这。。。前一题还输入输出呢怎么突然就最短路了。。。完全不按套路出牌啊喂(在最短路基础上额外维护一下题目要求的最短路条数和最大人数就行了
……话是这么说交上去一直WA。。。后来翻网上题解找到了一组把我的程序卡掉的数据之前的WA版本是【如果松弛后的dis和目前的dis一样,也要再入队一次】因为觉得虽然距离没变但是路径数和最大人数都需要更新然而事实上这个点之前已经有过一次入队且未处理的记录了也就是说,假设1、2点到3点的距离一样,先通过1更新3,再通过2更新3的时候,用1更新那时入队的节点还没被处理如果这时候把3再入队一次,3后边的节点就会受到两次3的影响就WA了。
改AC的代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> const int N=509; using namespace std; int n,m,c1,c2; int rt[N]; int p[N],cnt; struct edge{int to,nex,val;}e[5000000]; void add(int u,int v,int val){ e[++cnt]=(edge){v,p[u],val}; p[u]=cnt; } int path_num[N],maxrt[N]; long long dis[N]; struct node{int id;long long dis;}; bool operator <(node x,node y){return x.dis>y.dis;} bool vis[N]; priority_queue<node>q; inline void dijkstra(int s) { dis[s]=0; while(!q.empty())q.pop(); q.push((node){s,0}); while(!q.empty()) { node cur=q.top();q.pop(); if(cur.dis!=dis[cur.id])continue; int u=cur.id; for(int k=p[u];k;k=e[k].nex) { int v=e[k].to; if(dis[v]>=dis[u]+e[k].val) { if(dis[v]==dis[u]+e[k].val){ path_num[v]+=path_num[u]; maxrt[v]=max(maxrt[v],maxrt[u]+rt[v]); } else{ path_num[v]=path_num[u]; dis[v]=dis[u]+e[k].val; maxrt[v]=maxrt[u]+rt[v]; q.push((node){v,dis[v]}); } } } } } int main(){ scanf("%d%d%d%d",&n,&m,&c1,&c2); for(int i=0;i<n;++i)scanf("%d",&rt[i]); for(int i=1;i<=m;++i){ int u,v,w;scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } memset(dis,0x3f,sizeof(dis)); path_num[c1]=1; maxrt[c1]=rt[c1]; dijkstra(c1); printf("%d %d",path_num[c2],maxrt[c2]); return 0; }
原文地址:https://www.cnblogs.com/wypx/p/12188336.html