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<algorithm>
using namespace std;
const int N = 510;
#define INF 9999999
int e[N][N], Mindist[N], NumOfShortPath[N], NumOfRescue[N],MaxRescue[N],visit[N];
/*
e[i][j]: i到j的距离
Mindist[i]: 从源点到i的最小距离
NumOfShortPath[i]: 从源点到i的最短路径条数
NumOfRescue[i]: i的救援队的个数
MaxRescue[i]: 从源点到i点最多的救援队个数
visit[i]: 第i个节点是否被访问
*/
int main()
{
int i, j, k, n, m;
int c1, c2,x,y, dist,u,v;
//初始化
fill(e[0], e[0] + N * N, INF);
fill(Mindist, Mindist + N, INF);
cin >> n >> m >> c1 >> c2;
for (i = 0; i < n; i++) cin >> NumOfRescue[i];
for (i = 0; i < m; i++) {
cin >> x >> y >> dist;
e[x][y] = e[y][x] = dist;
}
Mindist[c1] = 0;
MaxRescue[c1] = NumOfRescue[c1];
NumOfShortPath[c1] = 1;
//Dijkstra
for (i = 0; i < n; i++) {
int _min = INF,MinV=-1;
//找一个未被访问的最小边节点
for (v = 0; v < n; v++) {
if (!visit[v] && Mindist[v]< _min) {
_min = Mindist[v];
MinV = v;
}
}
//没有节点可以走了,代表访问结束
if (MinV == -1) break;
visit[MinV] = 1;
//更新MinV的邻节点相关信息
for (v = 0; v < n; v++) {
if (!visit[v] && e[MinV][v]!=INF) {
if (Mindist[MinV] + e[MinV][v] < Mindist[v]) {
Mindist[v] = Mindist[MinV] + e[MinV][v];
NumOfShortPath[v] = NumOfShortPath[MinV];
MaxRescue[v] = MaxRescue[MinV] + NumOfRescue[v];
}
else if (Mindist[MinV] + e[MinV][v] == Mindist[v]) {
NumOfShortPath[v] += NumOfShortPath[MinV];
if (MaxRescue[MinV] + NumOfRescue[v] > MaxRescue[v]) {
MaxRescue[v] = MaxRescue[MinV] + NumOfRescue[v];
}
}
}
}
}
cout << NumOfShortPath[c2] << " " << MaxRescue[c2] << endl;
}