PAT甲级 1003
2017-06-15 20:01
351 查看
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
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
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.
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求最短路径时,记录并更新最短路径的数目,以及相应最多的救援队伍数目。
此代码需要特别考虑当起点和终点一样时,dist,pathNum,team三个值的初始值。
#include<iostream> #include<stdio.h> #include<limits.h> #include<algorithm> using namespace std; const int maxn=500+10; int teamsInCity[maxn]; int path[maxn][maxn]; int dist[maxn],team[maxn],pathNum[maxn]; bool visit[maxn]; void init(int n){ int i; for(i=0;i<n;i++){ fill(path[i],path[i]+n,INT_MAX); } fill(visit,visit+n,false); } void dijkstra(int s,int n){ int i,j; for(i=0;i<n;i++){ dist[i]=path[s][i]; if(path[s][i]!=INT_MAX){ team[i]=teamsInCity[s]+teamsInCity[i]; pathNum[i]=1; } } //Condition:source==end dist[s]=0; //new pathNum[s]=1; //new team[s]=teamsInCity[s]; //new visit[s]=true; int cur,minLength; for(i=1;i<n;i++){ minLength=INT_MAX; for(j=0;j<n;j++){ if(!visit[j] && dist[j]<minLength){ minLength=dist[j]; cur=j; } } visit[cur]=true; if(minLength==INT_MAX){ break; } for(j=0;j<n;j++){ if(!visit[j] && path[cur][j]!=INT_MAX){ if(path[cur][j]+dist[cur]==dist[j]){ pathNum[j]+=pathNum[cur]; team[j]=max(team[j],team[cur]+teamsInCity[j]); }else if(path[cur][j]+dist[cur]<dist[j]){ dist[j]=path[cur][j]+dist[cur]; pathNum[j]=pathNum[cur]; team[j]=team[cur]+teamsInCity[j]; } } } } } int main(){ freopen("./in","r",stdin); int n,m,c1,c2; scanf("%d %d %d %d",&n,&m,&c1,&c2); init(n); int i; for(i=0;i<n;i++){ scanf("%d",teamsInCity+i); } int tc1,tc2,l; for(i=0;i<m;i++){ scanf("%d %d %d",&tc1,&tc2,&l); path[tc1][tc2]=l; path[tc2][tc1]=l; } dijkstra(c1,n); printf("%d %d\n",pathNum[c2],team[c2]); return 0; }
相关文章推荐
- PAT甲级1003---迷宫问题(DFS或Dijkstra)
- PAT 甲级 1003
- PAT甲级练习题1003
- Pat 1003 甲级
- PAT程序设计考题——甲级1003(Emergency ) C++实现
- PAT甲级1003(深搜)
- PAT甲级C语言.1003. 我要通过!
- pat 甲级 1003
- 浙大Pat | 浙大pat 牛客网甲级 1003 Highest Price in Supply Chain (25)DFS
- [PAT-甲级]1003.Emergency
- PAT甲级 1003
- PAT(甲级)1003
- pat 甲级 1003
- PAT 甲级练习 1003
- 浙大PAT甲级-1003
- PAT-甲级-1003
- pat甲级_路径问题(例题:pat 1003 Emergency (25))
- 【PAT】甲级1003 - Emergency(最短路)
- pat 甲级 1003 Emergency
- PAT程序设计练习——甲级1003(任意两个城市最短距离、Floyd最短路径算法)