[PAT (Advanced Level) ]1003. Emergency 解题文档
来源:互联网 发布:鹰眼图js 编辑:程序博客网 时间:2024/05/17 04:52
题目链接:http://www.patest.cn/contests/pat-a-practise/1003
1003. Emergency (25)
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.
5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output2 4
分析:
本题其实就是一个图论方面的问题。求解最短路径的路数以及选择什么样的最短路径可以获得的最大救援数是多少。
采用方法:
(1)求解最短路径值,采用Prim算法(关于Prim算法可以参考笔者的这篇文章:http://blog.csdn.net/u010536377/article/details/42083451 当前也可以采用Dijstra算法,但是笔者认为Prim算法更易实现);
(2)根据最短路径值以及DFS深度优先搜索算法(实现的是find()函数),求解出最短路径的的条数以及救援数。具体代码如下。
<span style="font-size:12px;">#include <stdio.h>#define MAXNUM 600int N,M,C1,C2;int ArrayN[MAXNUM];int ArrayM[MAXNUM][MAXNUM];int Matrix[MAXNUM][MAXNUM];int path;int numPath;int maxTeamNum = 0;int i,j;void input() { scanf("%d%d%d%d",&N,&M,&C1,&C2); for (i=0; i<N; i++) { scanf("%d",&ArrayN[i]); } for(i=0;i<M;i++){ scanf("%d%d%d",&ArrayM[i][0],&ArrayM[i][1],&ArrayM[i][2]); }}void matrix(){ for(int i=0;i<M;i++){ Matrix[ArrayM[i][0]][ArrayM[i][1]] = ArrayM[i][2]; Matrix[ArrayM[i][1]][ArrayM[i][0]] = ArrayM[i][2]; }}int Minlength(){ int flag=1; int Node[MAXNUM]; for(i=0;i<N;i++){ Node[i]=0; } Node[C1]=1; while(flag){ int min=100000000; int newNode=-1; int selectNode=-1; for(i=0;i<N;i++){ if(Node[i]!=0){ for(j=0;j<N;j++){ if (Node[j] == 0 && Matrix[i][j] != 0 && min > (Matrix[i][j]+Node[i])) { min = Matrix[i][j]+Node[i]; selectNode = i; newNode = j; } } } } Node[newNode] = Node[selectNode] + Matrix[selectNode][newNode]; if (Node[C2] != 0) { flag = 0; } } return Node[C2] - 1;}void find(int Node,int min,int curTeamNum,int SearchFlag[]){ int xxx[MAXNUM]; for(int i=0;i<N;i++){ xxx[i]=SearchFlag[i]; } xxx[Node]=1; if (min == path && Node == C2) { numPath++; if (maxTeamNum < curTeamNum) maxTeamNum = curTeamNum; return; } else if (min < path) { for (int i = 0; i < N; i++){ if (Matrix[Node][i] > 0&&xxx[i]==0) { { find(i, min + Matrix[Node][i], curTeamNum+ArrayN[i],xxx); }} } } else return; }int main() { input(); matrix(); path = Minlength(); int flagS[MAXNUM]; for(i=0;i<N;i++) flagS[i]=0; find(C1, 0, ArrayN[C1],flagS); printf("%d %d",numPath,maxTeamNum); return 0;}</span>
另外提供一组测试用例:
Input:
10 8 0 2
1 2 3 4 5 6 7 8 9 10
0 1 6
0 2 7
0 4 3
1 2 1
2 3 1
2 4 4
3 4 6
1 10 13
Output:
3 9
上面代码所有测试点均已经通过。
- [PAT (Advanced Level) ]1003. Emergency 解题文档
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- 【PAT Advanced Level】1003. Emergency (25)
- PAT (Advanced Level) Practise 1003. Emergency (25)
- PAT (Advanced Level) Practise 1003. Emergency (25)
- PAT (Advanced Level) 1003. Emergency (25)
- 【PAT】【Advanced Level】1003. Emergency (25)
- PAT (Advanced Level) Practise 1003. Emergency (25)
- PAT Advanced Level 1003:Emergency
- [PAT (Advanced Level) ]1035. Password 解题文档
- [PAT (Advanced Level) ]1008. Elevator 解题文档
- 浙大PAT (Advanced Level) Practise 1003. Emergency (25)
- 编程题目:PAT(Advanced Level) Practice 1003. Emergency (25)
- PAT Advanced Level 1003. Emergency (25)(Java and C++)
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
- 浙大PAT advanced level 1003 emergency
- UVA - 11624 Fire!(15.10.10 搜索专题)bfs
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记47 文件系统Demo
- String.Format格式说明
- 《深入理解Linux网络技术内幕》阅读笔记(二十四)
- 关于责任、钱和技术
- [PAT (Advanced Level) ]1003. Emergency 解题文档
- 图片作为背景,登陆页面相对定位
- svn版本库的拆分及迁移子目录等
- HDU 5113 Black And White (dfs神剪枝)
- explicit
- .htaccess 分布式配置文件
- 位图的重新采样以及对大图的重新处理
- 手机相关的内容(权限自行添加)
- AccessDiver