1003 Emergency(25 分)
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
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.
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.
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.
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
2 4
N个城市 ,M条道路,从C1到C2,首先保证路径最短,其次要保证救援队伍的数目最大,然后输出路径数量。
我们首先可以确定使用dijkstra算法计算最短路,然后再选择路径的位置增加题目额外条件,如果下一条路径比当前路径短,则选择最短路,并计算救援队伍数量和路径数量。如果新路径与当前路径相同,则比较两者经过的救援队伍数量多少来进行选择,同时路径+1。
也就是代码中的这一段:
if(!vis[j] && G[minv][j] != INF) {
if(G[minv][j] + d[minv] < d[j]) {
d[j] = G[minv][j] + d[minv];
road[j] = road[minv];
maxres[j] = maxres[minv] + resnum[j];
} else if(G[minv][j] + d[minv] == d[j]) {
road[j] += road[minv];
if(maxres[j] < maxres[minv] + resnum[j]) {
maxres[j] = maxres[minv] + resnum[j];
}
}
}
AC代码:
// 2018年8月28日11:16:35
// 这个题出的一点也不好,因为使用暴力破解的方法也同样可以解题,使用数学推倒可以优化解题过程 算法笔记中有更好的方法
#include
#include
using namespace std;
const int maxn = 510;
const int INF = 1000000000;
int n, m, c1, c2;
bool vis[maxn] = {false};
int resnum[maxn], G[maxn][maxn], d[maxn], road[maxn], maxres[maxn];
void dijkstra(int);
int main() {
fill(G[0], G[0] + maxn * maxn, INF);
scanf("%d%d%d%d", &n, &m, &c1, &c2);
for(int i = 0; i < n; ++i) {
scanf("%d", resnum + i);
}
for(int i = 0; i < m; ++i) {
int a, b, rl;
scanf("%d%d%d", &a, &b, &rl);
G[a][b] = rl;
G[b][a] = rl;
}
dijkstra(c1);
printf("%d %d\n", road[c2], maxres[c2]);
return 0;
}
void dijkstra(int c1) {
fill(d, d + maxn, INF);
d[c1] = 0;
maxres[c1] = resnum[c1];
road[c1] = 1;
for(int i = 0; i < n; ++i) {
if(vis[c2]) {
return ;
}
int minv = -1, minl = INF;
for(int j = 0; j < n; ++j) {
if(!vis[j] && d[j] < minl) {
minl = d[j];
minv = j;
}
}
if(minv == -1) {
return ;
}
vis[minv] = true;
for(int j = 0; j < n; ++j) {
if(!vis[j] && G[minv][j] != INF) {
if(G[minv][j] + d[minv] < d[j]) {
d[j] = G[minv][j] + d[minv];
road[j] = road[minv];
maxres[j] = maxres[minv] + resnum[j];
} else if(G[minv][j] + d[minv] == d[j]) {
road[j] += road[minv];
if(maxres[j] < maxres[minv] + resnum[j]) {
maxres[j] = maxres[minv] + resnum[j];
}
}
}
}
}
}
如有错误,欢迎指摘。