PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用

来源:互联网 发布:知乎女神芈十四真名 编辑:程序博客网 时间:2024/05/18 22:44

1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

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

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int maxn = 500, INF = 0x3f3f3f3f;int N, M, C1, C2;//men存储第i个节点有多少人,cou存储到当前节点相同长度最短路的条数//d存储到节点i的最短距离,call存储到节点i能叫的最多的人数int men[maxn + 5], mapp[maxn + 5][maxn + 5];int cou[maxn + 5], d[maxn + 5], call[maxn + 5];bool vis[maxn + 5];void dijstra(int s) {memset(cou, 0, sizeof(cou));memset(call, 0, sizeof(call));memset(vis, false, sizeof(vis));call[s] = men[s];for (int i = 0; i < N; i++) {d[i] = mapp[s][i];if (mapp[s][i] < INF) {cou[i] = 1; //debugif (i != s) //debug 防止出现call[s] = call[s] + men[s]call[i] = call[s] + men[i];}}d[s] = 0;vis[s] = true;for (int t = 1; t <= N - 1; t++) {int minn = INF, index = s;for (int i = 0; i < N; i++) {if (!vis[i] && minn > d[i]) { //注意!vis[i]minn = d[i];index = i;}}vis[index] = true;for (int j = 0; j < N; j++) {if (!vis[j]) {//出现相同长度的最短路,则松弛call//若出现更短的路,则必须使得call[j] = call[index] + men[j]//因为先考虑路径最短然后才考虑尽可能多的人数if (d[j] == d[index] + mapp[index][j]) {cou[j] += cou[index]; //debug cou[j]++; 父节点那可能有很多相同长度的路,不能只加1if(call[j] < call[index] + men[j]) {call[j] = call[index] + men[j];}} else if(d[index] + mapp[index][j] < d[j]) {d[j] = d[index] + mapp[index][j];cou[j] = cou[index]; //debug cou[j] = 1; 父节点那可能有很多相同长度的路call[j] = call[index] + men[j];}}}}}int main(){cin >> N >> M >> C1 >> C2;for (int i = 0; i < N; i++) {scanf("%d", &men[i]);}for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {if (i == j) {mapp[i][j] = 0;} else {mapp[i][j] = INF;}}}for (int t = 0; t < M; t++) {int u, v, w;scanf("%d%d%d", &u, &v, &w);mapp[u][v] = mapp[v][u] = w;}if (C1 == C2) {printf("%d %d\n", 1, men[C1]);return 0;}dijstra(C1);printf("%d %d\n", cou[C2], call[C2]);return 0;}





0 0
原创粉丝点击