PAT (Advanced Level) Practise 1003 解题报告
2017-01-28 05:54
309 查看
GitHub
markdownPDF
问题描述
解题思路
代码
提交记录
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
大意是:
n个城市m条路,每个城市都有救援小组。已知各边权值,给定起点和重点,求从起点到重点的最短路径条数以及最短路径上的救援小组数目之和。
n<=500.
DFS
DFS
markdownPDF
问题描述
解题思路
代码
提交记录
问题描述
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
大意是:
n个城市m条路,每个城市都有救援小组。已知各边权值,给定起点和重点,求从起点到重点的最短路径条数以及最短路径上的救援小组数目之和。
n<=500.
解题思路
最短路径,可以用dijsktra或深搜(数据不大)。dijsktra多记录一下路径数和小组总数。代码
dijsktra#include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() { //freopen("in.txt","r",stdin); int i,j,k,n,m,s,t,c1,c2,a[501][501],b[501],c[501],d[501],e[501],f[501]={0}; memset(a,0x3f,sizeof(a)); memset(d,0x3f,sizeof(d)); cin>>n>>m>>c1>>c2; for (i=0;i<n;i++) cin>>b[i]; for (i=0;i<m;i++) { cin>>j>>k>>t; a[j][k]=a[k][j]=t; } c[c1]=b[c1]; d[c1]=0; e[c1]=1; for (i=0;i<n;i++) { s=0x3f3f3f3f; t=-1; for (j=0;j<n;j++) if ((f[j]==0)&&(d[j]<s)) { t=j; s=d[j]; } if (t==-1) break; f[t]=1; for (j=0;j<n;j++) if (f[j]==0) if (d[j]>d[t]+a[t][j]) { d[j]=d[t]+a[t][j]; c[j]=c[t]+b[j]; e[j]=e[t]; } else if (d[j]==d[t]+a[t][j]) { e[j]+=e[t]; if (c[j]<c[t]+b[j]) c[j]=c[t]+b[j]; } } cout<<e[c2]<<' '<<c[c2]; return 0; }
DFS
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,s,t,maxt,mind,c1,c2,d,a[501][501],b[501]={0},c[501]={0}; void dfs(int k) { int i,j; if (k==c2) { if (d<mind) { mind=d; maxt=t; s=1; } else if (d==mind) { s++; if (t>maxt) maxt=t; } } c[k]=1; for (i=0;i<n;i++) if ((c[i]==0)&&(a[k][i]>0)) { d+=a[k][i]; t+=b[i]; dfs(i); d-=a[k][i]; t-=b[i]; } c[k]=0; } int main() { //freopen("in.txt","r",stdin); int i,j,k; memset(a,-1,sizeof(a)); cin>>n>>m>>c1>>c2; for (i=0;i<n;i++) cin>>b[i]; for (i=0;i<m;i++) { cin>>j>>k>>t; a[j][k]=a[k][j]=t; } maxt=0; mind=0x7FFFFF; t=b[c1]; d=0; dfs(c1); cout<<s<<' '<<maxt; return 0; }
提交记录
dijsktraDFS
相关文章推荐
- PAT (Advanced Level) Practise 1002 解题报告
- PAT (Advanced Level) Practise 1001 解题报告
- PAT (Advanced Level) Practise 1004 解题报告
- PAT (Advanced Level) 1102. Invert a Binary Tree (25) 解题报告
- PAT(Advanced Level) 1004 Counting Leaves 解题报告
- PAT (Advanced Level) 1103. Integer Factorization (30) 解题报告
- PAT(Advanced Level) 1020 Tree Traversals 解题报告
- PAT (Basic Level) Practise (中文)1067. 试密码(20) 解题报告
- pat(basic level) practise 解题报告
- PAT (Advanced Level) 1035. Password (20) 解题报告
- PAT (Advanced Level) 1005. Spell It Right (20) 解题报告
- PAT (Advanced Level) 1036. Boys vs Girls (25) 解题报告
- PAT (Advanced Level) 1006. Sign In and Sign Out (25) 解题报告
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 解题报告
- PAT (Advanced Level) 1037. Magic Coupon (25) 解题报告
- PAT (Basic Level) Practise(中文)----30题解题报告
- PAT (Advanced Level) 1008. Elevator (20) 解题报告
- PAT (Advanced Level) 1038. Recover the Smallest Number (30) 解题报告
- PAT (Advanced Level) 1124. Raffle for Weibo Followers (20) 解题报告
- PAT (Advanced Level) 1125. Chain the Ropes (25) 解题报告