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
重新学习dijsktra算法,重做一遍
分两种情况考虑
到 j 的最短路径只有一条
到 j 的最短路径有两条
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 510; int n , m , a , b; int helper[N];//每个点的救援人数 int di[N][N];//路径的距离 int dis[N] , sum[N] = {0} , cnt[N] = {0}; bool st[N]; void init() { memset(di , 0x3f , sizeof di); memset(dis , 0x3f , sizeof dis); memset(st , false , sizeof st); } void dijsktra() { dis[a] = 0 , cnt[a] = 1 , sum[a] = helper[a]; for(int i = 0;i < n;i ++) { int t = -1; for(int j = 0;j < n;j ++) { if(!st[j] && (t == -1 || dis[t] > dis[j])) t = j; } st[t] = true; for(int j = 0;j < n;j ++) { if(dis[j] > dis[t] + di[t][j]) {//到j的最短路径只有一条 //就是从起始地先到t再到j的路径长度小于原先到j的长度 dis[j] = dis[t] + di[t][j];//更新路径 cnt[j] = cnt[t];//到j的路径数等于到t的路径数 sum[j] = sum[t] + helper[j]; //j救援人员的数目等于t的救援人员数目+j点的救援人数 } else if(dis[j] == dis[t] + di[t][j]) {//到j的最短路径有两条 cnt[j] += cnt[t];//最短路径数增加 sum[j] = max(sum[j], sum[t] + helper[j]); //救援人数取两条路径中最大的就行 } } } } int main() { cin >> n >> m >> a >> b; for(int i = 0;i < n;i ++) cin >> helper[i]; init(); for(int i = 0;i < m;i ++) { int c , d , e; cin >> c >> d >> e; di[c][d] = di[d][c] = e; } dijsktra(); cout << cnt[b] << " " << sum[b]; return 0; }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/95244.html