最短路径
1 原题
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\ (\le 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C_1 and C_2 - 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 c_1 , c_2 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 C_1 to C_2.
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C_1 and C_2, 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
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
栈限制:8192 KB
2 代码示例
Java
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static final int N = 510;
static final int INF = 0x3f3f3f3f;
static int n, m, S, T;
static int[][] g = new int[N][N];
static int[] w = new int[N];
static int[] dist = new int[N];
static int[] sum = new int[N];
static int[] cnt = new int[N];
static boolean[] st = new boolean[N];
public static void dijkstra() {
Arrays.fill(dist, INF);
Arrays.fill(st, false);
Arrays.fill(sum, 0);
Arrays.fill(cnt, 0);
dist[S] = 0;
sum[S] = w[S];
cnt[S] = 1;
for (int i = 0; i < n - 1; ++i) {
int t = -1;
for (int j = 0; j < n; ++j)
if (!st[j] && (t == -1 || dist[j] < dist[t]))
t = j;
st[t] = true;
for (int j = 0; j < n; ++j) {
if (dist[j] > dist[t] + g[t][j]) {
dist[j] = dist[t] + g[t][j];
sum[j] = sum[t] + w[j];
cnt[j] = cnt[t];
} else if (dist[j] == dist[t] + g[t][j]) {
cnt[j] += cnt[t];
if (sum[j] < sum[t] + w[j]) sum[j] = sum[t] + w[j];
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < N; ++i)
Arrays.fill(g[i], INF);
n = scanner.nextInt();
m = scanner.nextInt();
S = scanner.nextInt();
T = scanner.nextInt();
for (int i = 0; i < n; ++i)
w[i] = scanner.nextInt();
for (int i = 0; i < m; ++i) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int len = scanner.nextInt();
g[a][b] = g[b][a] = Math.min(g[a][b], len);
}
dijkstra();
System.out.printf("%d %d\n", cnt[T], sum[T]);
scanner.close();
}
}
C++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510, INF = 0x3f3f3f3f;
int n, m, S, T;
int g[N][N], w[N]; // [0, n - 1]
int dist[N], sum[N], cnt[N];
bool st[N];
void dijkstra() {
memset(dist, 0x3f, sizeof dist);
memset(st, false, sizeof st);
memset(sum, 0, sizeof sum);
memset(cnt, 0, sizeof cnt);
dist[S] = 0;
sum[S] = w[S];
cnt[S] = 1;
for (int i = 0; i < n - 1; ++i) {
int t = -1;
for (int j = 0; j < n; ++j)
if (!st[j] && (t == -1 || dist[j] < dist[t]))
t = j;
st[t] = true;
for (int j = 0; j < n; ++j) {
if (dist[j] > dist[t] + g[t][j]) {
dist[j] = dist[t] + g[t][j];
sum[j] = sum[t] + w[j];
cnt[j] = cnt[t];
} else if (dist[j] == dist[t] + g[t][j]) {
cnt[j] += cnt[t];
if (sum[j] < sum[t] + w[j]) sum[j] = sum[t] + w[j];
}
}
}
}
int main() {
memset(g, 0x3f, sizeof(g));
scanf("%d%d%d%d", &n, &m, &S, &T);
for (int i = 0; i < n; ++i) scanf("%d", &w[i]);
for (int i = 0; i < m; ++i) {
int a, b, len;
scanf("%d%d%d", &a, &b, &len);
g[a][b] = g[b][a] = min(g[a][b], len);
}
dijkstra();
printf("%d %d\n", cnt[T], sum[T]);
return 0;
}