1003 Emergency (25 分)
1003 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 (≤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
代码
迪杰斯特拉(Dijkstra)算法求解
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=510;
int n,m,c1,c2;// n是城市数量,m是路径数量,c1起点,c2终点
int weight[maxn]; // weight[i]为第i个城市的救援队人数
int weight2[maxn]; // weight2[i]为c1到城市i的最多救援人数
int edge[maxn][maxn]; // edge[i][j] 从i到j的距离
int dis[maxn]; // dis[i]为c1到城市i的最短距离
int num[maxn]; // num[i]为 c1到城市i的最短路径条数
int visit[501];// 该路径是否被走过,0未走过,1走过
int main(){
cin >> n >> m >> c1 >> c2;
for(int i=0; i<n; i++) {
cin >> weight[i];
}
// 初始化数据
memset(edge,0x3f,sizeof edge);
memset(dis,0x3f,sizeof dis);
memset(visit,0,sizeof visit);
for(int i=0; i<m; i++) {
int start,end,value;
cin >> start >> end >> value;
edge[start][end]=edge[end][start]=value;
}
// 初始化起点
dis[c1]=0;
weight2[c1]=weight[c1];
num[c1]=1;
// n个城市,连接两个城市最多n-1次即可
for(int i=0; i<n-1; i++) {
int u=-1,minV=INF;
// 迪杰斯特拉算法,找到距离最近的一个边
for(int j=0; j<n; j++) {
if(visit[j] == 0 && dis[j] < minV) {
u = j;
minV = dis[j];
}
}
if(u == -1) break;
visit[u] = 1;// 路径访问过了已经
// 更新最短距离
for(int v = 0; v < n; v++) {
if(visit[v] == 0 && edge[u][v] != INF) {
if(dis[u] + edge[u][v] < dis[v]) { //有更短的路径
dis[v] = dis[u] + edge[u][v]; //最短距离更改
num[v] = num[u];// 拥有最短距离的数量更改
weight2[v] = weight2[u] + weight[v]; //起点到该城市的救援人数更新
} else if(dis[u] + edge[u][v] == dis[v]) {// 路径一样长的时候,更新数量
num[v] = num[v] + num[u];// 拥有最短距离的数量更改
if(weight2[u] + weight[v] > weight2[v]) { //只能选一条路,选择救援人数多的那一条
weight2[v] = weight2[u] + weight[v];
}
}
}
}
}
cout << num[c2] << " " << weight2[c2];
return 0;
}
相关文章
- 【LOJ#6682】梦中的数论(min_25筛)
- 【UOJ#188】Sanrd(min_25筛)
- Flutter移动电商实战 --(25)列表页_使用Provide控制子类-1
- 2022年8月刷爆的Java面试八股文来了,包含中级-高级-源码面试题解析,内涵25个专题,200+面试题解析
- (25)打鸡儿教你Vue.js
- 资源 | 25个机器学习面试题,期待你来解答
- iOS应用性能调优的25个建议和技巧
- MySQL详解(25)-----------MySQL性能优化
- 25、【字符串】最长公共前缀(C++版)
- 程序的载入和运行(五)——《x86汇编语言:从实模式到保护模式》读书笔记25
- 每天一个linux命令(25):df 命令