PAT_1003: Emergency
2013-07-14 20:58
281 查看
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最短路径算法,注意多条最短路径的计算方式。该算法其它的技术细节请参考文章:http://blog.csdn.net/v_JULY_v/article/details/6182419
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最短路径算法,注意多条最短路径的计算方式。该算法其它的技术细节请参考文章:http://blog.csdn.net/v_JULY_v/article/details/6182419
#include<stdio.h> #define MAXNUMN 500 #define UNDEFINED -1 #define INFINITY 99999999 #define UNKNOWN 0 #define KNOWN 1 int n; /*number of city*/ int road[MAXNUMN][MAXNUMN]; int dist[MAXNUMN]; /*数组q存储图中的每个节点到源的最短路径长度*/ int s[MAXNUMN]; /*数组s记录节点v是否已经known*/ int previous[MAXNUMN]; /*记录最短路径的前驱节点*/ int rescue[MAXNUMN]; /*记录每个city的rescue人数*/ int count[MAXNUMN]; /*记录从源节点到该节点的最短路径条数*/ int maxpeople[MAXNUMN]; /*记录从源节点到该节点能找到的最多救援人数*/ /*初始化每个节点*/ void InitializeQ(int source) { int i; for(i=0;i<n;i++) { dist[i] = INFINITY; previous[i] = UNDEFINED; s[i] = UNKNOWN; count[i] = 0; maxpeople[i] = rescue[i]; } dist[source]=0; count[source]=1; } void FindShortest(int source, int dest) { int i,j; for(i = 0; i<n; i++) { int cu = 0; /*当前dist最短节点的index*/ int tempdist = INFINITY; /*找到unknown节点中dist最小的点*/ for(j = 0; j<n; j++) { if(s[j]==UNKNOWN) { if(dist[j]<tempdist) { cu = j; tempdist = dist[j]; } } } s[cu]= KNOWN; /* if(cu == dest) break; */ /*更新dist*/ for(j = 0; j < n; j++) { if(s[j]==UNKNOWN && road[cu][j]>0) { if(dist[j]>dist[cu]+road[cu][j]) { dist[j] = dist[cu]+road[cu][j]; previous[j] = cu; count[j] = count[cu]; maxpeople[j] = maxpeople[cu]+rescue[j]; } else if(dist[j]==dist[cu]+road[cu][j]) { count[j] += count[cu]; if(maxpeople[cu]+rescue[j]>maxpeople[j]) maxpeople[j] = maxpeople[cu]+rescue[j]; } } } } } int main() { int m,c1,c2; int i,j; int cc1,cc2,length; scanf("%d %d %d %d",&n,&m,&c1,&c2); for(i=0;i<n;i++) scanf("%d",&rescue[i]); for(i=0;i<n;i++) for(j=0;j<n;j++) { road[i][j] = -1; /*初始化,不连通节点权值为-1*/ } for(i=0;i<m;i++) { scanf("%d %d %d",&cc1,&cc2,&length); road[cc1][cc2]=length; road[cc2][cc1]=length; } InitializeQ(c1); FindShortest(c1,c2); printf("%d %d",count[c2],maxpeople[c2]); return 0; }
相关文章推荐
- PAT (Advanced Level) Practise 1003 Emergency (25)
- PAT (Advanced Level) 1003 - Emergency(单元最短路)
- PAT (Advanced Level) Practise 1003 Emergency
- 【Pat 1003】Emergency
- pat甲级_路径问题(例题:pat 1003 Emergency (25))
- PAT1003. Emergency (25)
- Pat 1003 Emergency (25)
- PAT程序设计考题——甲级1003(Emergency ) C++实现
- PAT 甲级试题1003 Emergency (25) dijkstra变形应用
- 浙大PAT advanced level 1003 emergency
- PAT 1003 Emergency
- PAT1003. Emergency (25) Dijkstra
- pat1003 Emergency
- PAT 1003 Emergency
- PAT 1003 Emergency
- [PAT-甲级]1003.Emergency
- PAT-A | 1003 | Emergency
- PAT 1003 Emergency 深度优先搜索 (剪枝方案一,未优化)
- pat1003. Emergency
- PAT 1003 Emergency (25)