PAT.Emergency(求最短路条数 + 最短路中点权和最大的那条最短路的点权和)
Posted bianjunting
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT.Emergency(求最短路条数 + 最短路中点权和最大的那条最短路的点权和)相关的知识,希望对你有一定的参考价值。
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 Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C?1?? and C?2?? - 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 c?1??, c?2?? 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 C?1?? to C?2??.
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C?1?? and C?2??, 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
/* 本题大意:无向图,求结点之间的最短路的条数,并求出所有最短路中点权和最大的那条最短路的点权和。
本题思路:其实本题正规解法是spfa + 最大流,但是长时间不写网络流有点生疏,加上PAT甲级不考网络流哈哈哈,
就写了个dfs版本的。
*/ #include <iostream> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int maxn = 500 + 5, inf = 0x3f3f3f3f; struct edge { int to, w, _next; } edges[maxn * maxn]; int n, m, c1, c2, u, v, w; int head[maxn]; int cnt; void init() { memset(head, -1, sizeof head); cnt = 0; } void addedge(int u, int v, int w) { edges[cnt].to = v; edges[cnt].w = w; edges[cnt]._next = head[u]; head[u] = cnt ++; } int dist[maxn]; bool vis[maxn]; void spfa(int s, int t) { memset(dist, inf, sizeof dist); memset(vis, false, sizeof vis); vis[s] = true; dist[s] = 0; queue<int> que; que.push(s); while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; for(int k = head[u]; ~k; k = edges[k]._next) { int v = edges[k].to; if(dist[v] > dist[u] + edges[k].w) { dist[v] = dist[u] + edges[k].w; if(!vis[v]) { vis[v] = true; que.push(v); } } } } } int rescues[maxn]; int num, ans, temp; void dfs(int u, int pre) { if(u == c2) { ans = max(ans, temp); num ++; return; }; for(int k = head[u]; ~k; k = edges[k]._next) { int v = edges[k].to; if(edges[k].w + dist[u] == dist[v]) { temp += rescues[v]; dfs(v, u); temp -= rescues[v]; } } } int main() { init(); cin >> n >> m >> c1 >> c2; for(int i = 0; i < n; i ++) cin >> rescues[i]; while(m --) { cin >> u >> v >> w; addedge(u, v, w); addedge(v, u, w); } spfa(c1, c2); dfs(c1, c2); cout << num << ‘ ‘ << ans + rescues[c1] << endl; return 0; }
以上是关于PAT.Emergency(求最短路条数 + 最短路中点权和最大的那条最短路的点权和)的主要内容,如果未能解决你的问题,请参考以下文章