编程题目:PAT(Advanced Level) Practice 1003. Emergency (25)
2014-07-13 22:04
489 查看
1003. Emergency (25)
时间限制400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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)加以改进,注意做好多条相等的最短路径的处理。另外明确题目要求的是最短路径的数目,以及沿着某一条路径可能获得的rescue team的最大数,这一点上我起初理解错误。参考代码如下,写的有些冗余,但是好歹AC过了。
/* http://pat.zju.edu.cn/contests/pat-a-practise/1003 1003. Emergency (25),找到最短路径,计算其条数,并得到沿途人手最多的一条路径 */ #include<iostream> #include<cstring> #include<vector> #include<set> using namespace std; #define Inf 10000000 int N ,M ,C1,C2; int RT[501]={0}; //rescue team int LENGTH[501][501]; //length of two cities int visited[501] = {0}; //是否访问过的标记 int dist[501]; //存最短距离 int pathNum = 0; //最短路径的数目 int maxRTs = 0; //最短路径中rescue team 的总数 int tempRTs= 0; //暂存一条路径的人手总数 struct prenode //定义结构体,用于存指定点到源点路径的前一个点,若有多条路径,则存在vector<int> next中 { int number; vector<int> next; }; prenode pre[501]; void improved_dijkstra()//获得最短路径 { int i; for(i = 0; i<N;i++) { pre[i].number = -1; } for(i =0;i<N;i++)//初始化距离 { dist[i] = LENGTH[C1][i]; if(dist[i]!=Inf) pre[i].number=C1; } visited[C1] = 1; for(i = 0;i<N-1;i++)//N个点,需要N-1次,每次找到一条最小的边 { int min = Inf; int num = -1; for(int j = 0;j<N;j++) { if(visited[j]==0 && dist[j]<min ) { min = dist[j]; num = j; } } if(num != -1) { visited[num] = 1; for(int k =0;k<N;k++)//更新dist 信息 { if(visited[k]== 0)//针对未确定的点更新最短路径信息 { if( dist[k]>dist[num]+LENGTH[num][k]) { dist[k] = dist[num]+LENGTH[num][k]; pre[k].number = num; pre[k].next.clear(); } else if(dist[k]==dist[num]+ LENGTH[num][k])//多个相等的距离 { pre[k].next.push_back(num); } } } } } } void getpathNum(int nodenow,int nodetarget) //得到path数目 { if(nodenow == nodetarget) { pathNum++; //tempRTs += RT[nodenow]; if(tempRTs+RT[nodenow]>maxRTs) maxRTs = tempRTs+RT[nodenow]; return; } else { int temp = RT[nodenow]; tempRTs += RT[nodenow]; getpathNum(pre[nodenow].number,nodetarget); int i =0; while(i<pre[nodenow].next.size()) { getpathNum(pre[nodenow].next[i],nodetarget); i++; } tempRTs -= temp;//从上一条路径撤回要消掉该点的人手 } } int main() { cin>> N >>M >>C1>>C2; int i; for( i = 0;i<N;i++)//输入每个城市救援队数目 cin>>RT[i]; int a, b, l; for(int i = 0;i<N;i++) { for(int j = 0;j<N ;j++) { if(i==j) LENGTH[i][j] = 0; else LENGTH[i][j] = Inf; } } for(i =0;i<M;i++)//输入城市间距离 { cin >> a >> b>> l; LENGTH[a][b] = l; LENGTH[b][a] = l; } improved_dijkstra(); getpathNum(C2,C1); cout<<pathNum<<" "<<maxRTs; system("pause"); return 0; }
另外看到一篇写得比较简洁的代码,供参考:http://www.cnblogs.com/echobfy/p/3545917.html
相关文章推荐
- 编程题目:PAT(Advanced Level) Practice 1002. A+B for Polynomials (25)
- 编程题目:PAT(Advanced Level) Practice 1001. A+B Format (20)
- PAT (Advanced Level) 1003. Emergency (25)
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- 【PAT Advanced Level】1003. Emergency (25)
- PAT Advanced Level 1003. Emergency (25)(Java and C++)
- PAT(Advanced Level) 1003. Emergency(25) 最短路 + DFS
- PAT (Advanced Level) Practise - 1003. Emergency (25)
- PAT (Advanced Level) Practise 1003. Emergency (25)
- 【PAT】【Advanced Level】1003. Emergency (25)
- PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
- PAT (Advanced Level) 1003. Emergency (25)
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- 浙大PAT (Advanced Level) Practise 1003. Emergency (25)
- 【PAT Advanced Level】1029. Median (25)
- 【PAT Advanced Level】1013. Battle Over Cities (25)
- 1017. Queueing at Bank (25) @ PAT (Advanced Level) Practise
- 1021. Deepest Root (25) @ PAT (Advanced Level) Practise