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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
#include <iostream>
#include <vector>
#include <limits>
#include <queue>
using namespace std;
const int INF = numeric_limits<int>::max();//int max
struct Edge
{
int to;
int length;
Edge(int _to,int _length):to(_to),length(_length){}
};
typedef pair<int, int>pii;//pair存储距离和城市
void dijkstra(vector<vector<Edge>>& graph, vector<int>& teams, int start, int end, int n)
{
vector<int>dist(n, INF);//距离
vector<int>num_paths(n, 0);
vector<int>rescue_teams(n, 0);
priority_queue<pii, vector<pii>, greater<pii>>pq;
// 使用最小堆的优先队列,根据距离排序
dist[start] = 0;
num_paths[start] = 1;
rescue_teams[start] = teams[start];
pq.push({ 0,start });
while (!pq.empty()) {
int d = pq.top().first; 当前节点的最短距离
int u = pq.top().second;// 当前节点的索引
pq.pop();
if (d > dist[u])continue;
for (Edge& e : graph[u]) {
int v = e.to;
int len = e.length;
if (dist[u] + len < dist[v]) {
dist[v] = dist[u] + len;
num_paths[v] = num_paths[u];
rescue_teams[v] = rescue_teams[u] + teams[v];
pq.push({ dist[v],v });
}
else if(dist[u]+len==dist[v]) {
num_paths[v] += num_paths[u];
if (rescue_teams[u] + teams[v] > rescue_teams[v]) {
rescue_teams[v] = rescue_teams[u] + teams[v];
}
}
}
}
cout << num_paths[end] << " " << rescue_teams[end] << endl;
}
int main()
{
int N, M, C1, C2;
cin >> N >> M >> C1 >> C2;
vector<int>teams(N);//each city's rescue teams
for (int i = 0; i < N; i++) {
cin >> teams[i];
}
vector<vector<Edge>>graph(N);//建立无权图
for (int i = 0; i < M; i++) {
int c1, c2, l;
cin >> c1 >> c2 >> l;
graph[c1].emplace_back(c2, l);//添加边到图
graph[c2].emplace_back(c1, l);//无权图,反向添加
}
dijkstra(graph, teams, C1, C2, N);//计算最短路径的方法
return 0;
}
注意:
标签:pq,dist,rescue,Emergency,int,teams,length,1003 From: https://blog.csdn.net/2301_80161204/article/details/140767280