【PAT】【Advanced Level】1003. Emergency (25)
2017-07-11 19:30
344 查看
1003. Emergency (25)
时间限制400 ms
内存限制
65536 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
原题链接:
https://www.patest.cn/contests/pat-a-practise/1003
思路:
数据量很小,DFS可通过
坑点:
让输出最短路的个数,而不是长度
教训:
最短路算法还应熟悉
认真读题
针对小数据量,直接暴力
CODE:
#include<iostream> #include<vector> #include<cstring> #include<queue> #define N 500 #define inf 10000000 using namespace std; bool visited ; int dist ; int ps ; int m,n; int sta,des; int val=0; int vc=0; int minr=inf; int maxx=-1; int numm=0; void dfs(int s) { if (s==des) { if (vc<minr) { numm=1; minr=vc; maxx=val; } else if(vc==minr) { numm++; maxx=max(maxx,val); } return; } for (int i=0;i<n;i++) if (dist[s][i]!=inf && visited[i]!=1 && i!=s) { val+=ps[i]; vc+=dist[s][i]; visited[i]=1; if (vc<=minr) dfs(i); visited[i]=0; val-=ps[i]; vc-=dist[s][i]; } return; } int main() { memset(visited,0,sizeof(visited)); cin>>n>>m; cin>>sta>>des; for (int i=0;i<n;i++) { cin>>ps[i]; for (int j=0;j<n;j++) dist[i][j]=inf; } for (int i=0;i<m;i++) { int a,b,c; cin>>a>>b>>c; dist[a][b]=min(c,dist[a][b]); dist[b][a]=min(c,dist[b][a]); } val+=ps[sta]; dfs(sta); cout<<numm<<" "<<maxx; return 0; }
相关文章推荐
- 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)
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1003. Emergency (25)
- 编程题目:PAT(Advanced Level) Practice 1003. Emergency (25)
- PAT Advanced Level 1003. Emergency (25)(Java and C++)
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
- PAT(Advanced Level) 1003. Emergency(25) 最短路 + DFS
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- PAT (Advanced Level) Practise - 1003. Emergency (25)
- 【PAT】【Advanced Level】1117. Eddington Number(25)
- 【PAT】【Advanced Level】1025. PAT Ranking (25)
- PAT (Advanced Level) Practise 1017 Queueing at Bank (25)
- PAT (Advanced Level) Practise 1029 Median (25)
- 【PAT】【Advanced Level】1037. Magic Coupon (25)
- 【PAT】【Advanced Level】1059. Prime Factors (25)