仰望星空

位我上者,灿烂星空;道德律令,在我心中。

0%

Dijkstra

【道德经·第十八章】大道废,有仁义;智慧出,有大伪;六亲不和,有孝慈;国家昏乱,有忠臣。

迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。
迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法的策略,每次遍历到始点距离最近且未访问过的顶点的邻接节点,直到扩展到终点为止。 ——来源:百度百科

PAT甲级·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:

1
2
3
4
5
6
7
8
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:

1
2 4

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(0);
int e[510][510], teamcnt[510], dist[510], w[510], num[510];
bool visited[510];
const int inf = 99999999;

fill(e[0], e[0] + 510*510, inf);
fill(dist, dist + 510, inf);

int n, m, c1, c2;
cin >> n >> m >> c1 >> c2;

for(int i = 0; i < n; ++i)
cin >> teamcnt[i];

int a, b, c;
for(int i = 0; i < m; ++i) {
cin >> a >> b >> c;
e[a][b] = e[b][a] = c;
}

dist[c1] = 0;
w[c1] = teamcnt[c1];
num[c1] = 1;
for(int i = 0; i < n; ++i) {
int u = -1, minn = inf;
for(int j = 0; j < n; ++j) {
if(!visited[j] && dist[j] < minn) {
u = j;
minn = dist[j];
}
}
if(u == -1) break;
visited[u] = true;
for(int v = 0; v < n; ++v) {
if(!visited[v] && e[u][v] != inf) {
if(dist[u] + e[u][v] < dist[v]) {
dist[v] = dist[u] + e[u][v];
w[v] = w[u] + teamcnt[v];
num[v] = num[u];
} else if(dist[u] + e[u][v] == dist[v]) {
num[v] = num[v] + num[u];
if(w[u] + teamcnt[v] > w[v])
w[v] = w[u] + teamcnt[v];
}

}
}
}

cout << num[c2] << " " << w[c2] << "\n";

system("pause");
return 0;
}

位我上者,灿烂星空;道德律令,在我心中。