1003. Emergency(25分)
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 (≤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 Specification:
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 结尾无空行
#include <iostream> #include <vector> #include <queue> using namespace std; struct node { int vertex; int dist; }; class mycomparison { public: bool operator()(const node &a, node &b) { return a.dist > b.dist; } }; int main() { // cin int N, M, c1, c2; cin >> N >> M >> c1 >> c2; int rescueteams[N]; for (int i = 0; i < N; i++) { cin >> rescueteams[i]; } int G[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { G[i][j] = 0; } } for (int i = 0; i < M; i++) { int start, end, weight; cin >> start >> end >> weight; G[start][end] = weight; G[end][start] = weight; } vector<bool> visit(N, false); vector<int> teams(N, 0); vector<int> dist(N, 1e9); vector<int> num(N, 1); priority_queue<node, vector<node>, mycomparison> q; node tmp; // Dijkstra visit[c1] = true; dist[c1] = 0; teams[c1] = rescueteams[c1]; for (int i = 0; i < N; i++) { if (G[c1][i]) { dist[i] = G[c1][i]; teams[i] = teams[c1] + rescueteams[i]; tmp.dist = dist[i]; tmp.vertex = i; q.push(tmp); } } while (!q.empty()) { int minv = q.top().vertex; visit[minv] = true; q.pop(); for (int i = 0; i < N; i++) { if (!visit[i] && G[minv][i] && dist[minv] + G[minv][i] <= dist[i]) { if (dist[minv] + G[minv][i] == dist[i]) { teams[i] = max(teams[i], teams[minv] + rescueteams[i]); num[i]++; } else { teams[i] = teams[minv] + rescueteams[i]; } dist[i] = dist[minv] + G[minv][i]; tmp.dist = dist[i]; tmp.vertex = i; q.push(tmp); } } } cout << num[c2] << " " << teams[c2]; return 0; }
本题主要考察Dijkstra算法,我通过最小堆实现。这道题卡了挺久,主要是没有很好的理解题意。题目要输出的是最短路径数量和最大救援队伍数。对应要修改Dijkstra模板的部分为 dist[minv] + G[minv][i] <= dist[i],在判断通过当前最短路径点经过当前点时是否小于等于当前点原先储存的最短路径。特别要注意等于的情况,如果等于,最短路径数量加一,救援队伍数 max(teams[i], teams[minv] + rescueteams[i]),比较先前最短路径下的救援队伍数和当前路径下的救援队伍数,输出最大值。