PAT(Advanced Level) 1003. Emergency(25) 最短路 + DFS
2018-03-07 23:31
483 查看
题目链接
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.
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Emergency
Time limit:1 seconds Memory limit:256 megabytesProblem Description
IAs 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.
Input
5 6 0 21 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Output
2 4题意:
求最短路的条数,以及最短路中的最大权解题思路:
直接SPFA或Dijistra得出最短路,然后深搜一遍找最大权Code:
#include <cstdio> #include <iostream> #include <cmath> #include <algorithm> #include <cstring> #include <queue> using namespace std; const int maxn=500+5,maxm=500000; const int INF = 0x3f3f3f3f; struct EdgeNode { int to,w,next; }; EdgeNode edges[maxm]; int N,M; int head[maxn],edge; bool vis[maxn]; queue<int> que; int dis[maxn]; int C1, C2; int team[maxn]; int ans_len=0, ans_team=0,cnt=0; void addedge(int u,int v,int c) { edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++; } void init() { memset(head,-1,sizeof(head)); edge=0; } void spfa(int s,int n) { int u; for(int i=0;i<n;i++) dis[i]=INF; while(!que.empty())que.pop(); que.push(s); vis[s]=true; dis[s]=0; while(!que.empty()) { u = que.front(); que.pop(); vis[u] = false; for (int i = head[u]; i !=-1; i=edges[i].next) { int v = edges[i].to; int w = edges[i].w; if (dis[v]>dis[u]+w) { dis[v] = dis[u] + w; if(!vis[v]) { vis[v] = true; que.push(v); } } } } } void DFS(int u,int teams,int len) { //printf("%d %d %d\n", u, teams, len); if (u == C2 && ans_team < teams && len == ans_len) { ans_team = teams; } if (u == C2 &&len == ans_len) { cnt++; } if(len>=ans_len) { return; } vis[u] = true; for (int i = head[u]; i !=-1; i=edges[i].next) { int v = edges[i].to; int w = edges[i].w; if (!vis[v]&&len+w<=ans_len) { vis[v] = true; DFS(v, teams + team[v],len+w); vis[v] = false; } } } int main() { init(); cin >> N >> M >> C1 >> C2; for (int i = 0; i < N; i++) { cin >> team[i]; } for (int i = 0; i < M;i++) { int u, v, c; cin >> u >> v >> c; addedge(u, v, c); addedge(v, u, c); } spfa(C1, N); ans_len = dis[C2]; memset(vis, false, sizeof(vis)); DFS(C1, team[C1], 0); cout << cnt << ' ' << ans_team << endl; return 0; }
相关文章推荐
- PAT Advanced Level 1003. Emergency (25)(Java and C++)
- PAT (Advanced Level) 1003. Emergency (25)
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
- 浙大PAT (Advanced Level) Practise 1003. Emergency (25)
- 编程题目:PAT(Advanced Level) Practice 1003. Emergency (25)
- PAT (Advanced Level) 1003. Emergency (25)
- PAT (Advanced Level) Practise - 1003. Emergency (25)
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- PAT (Advanced Level) Practise 1003. Emergency (25)
- 【PAT Advanced Level】1003. Emergency (25)
- 【PAT】【Advanced Level】1003. Emergency (25)
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- 【PAT】【Advanced Level】1085. Perfect Sequence (25)
- 1021. Deepest Root (25)【并查集+深搜】——PAT (Advanced Level) Practise
- 1040. Longest Symmetric String (25)【最长回文子串-马拉车(manacher算法)——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1066. Root of AVL Tree (25)
- 【PAT】【Advanced Level】1117. Eddington Number(25)
- 编程题目:PAT(Advanced Level) Practice 1002. A+B for Polynomials (25)