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, C1and 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, c2and 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 C1and 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
这里要求单源点的最短路径,所以采用了dijkstra算法,在求完最短路径后,使用了dfs来求出路径数和最多救援队数量。
#include<stdio.h>
int e[500][500],numFirmen=0;//数组e用来保存两个城市之间路的长度
int INF = 99999999;//定义INF为一个很大的值
int minRoad = 0;//路径数
int f[501];//存储消防员数目的数组
int end, numCity;//终点和城市数
int book2[601] = { 0 };//dfs中的标记数组,标记该点是否走过
void dfs(int distance,int firemen,int current_position) {//深度优先搜索消防员人数和路径数
int v;//下一步走的位置
if(distance<0) return;
if (distance == 0&¤t_position==end) {
minRoad++;
if (numFirmen < firemen)
numFirmen = firemen;
return;
}
if (distance != 0&¤t_position==end) return;
if(distance==0)return;
if(distance>0){
for (v = 0; v < numCity; v++) {
if (e[current_position][v]!=INF&&book2[v]==0) {
book2[v] = 1;
dfs( distance-e[current_position][v], firemen+f[v], v);
book2[v] = 0;
}
}
}
return;
}
int main(void) {
int dis[600], book[600],m,t1,t2,t3,c1,i,j;
int min,u=0,v;
scanf("%d %d %d %d", &numCity, &m, &c1, &end);
for (i = 0; i < numCity; i++)
for (j = 0; j < numCity; j++)
if (i == j)e[i][j] = 0;
else e[i][j] = INF;
for (i = 0; i < numCity; i++) {
scanf("%d", &f[i]);
}
for (i = 0; i < m; i++) {
scanf("%d %d %d", &t1, &t2, &t3);
e[t1][t2] =e[t2][t1]= t3;
}
for (i = 0; i < numCity; i++) {
dis[i] = e[c1][i];
}
for (i = 0; i < numCity; i++)
book[i] = 0;
book[c1] = 1;
//Dijkstra算法核心语句
for (i = 0; i < numCity - 1; i++) {
min = INF;
for (j = 0; j < numCity; j++) {//找到距离从c1最近的顶点
if (book[j] == 0 && dis[j] < min) {
min = dis[j];
u = j;
}
}
book[u] = 1;
for (v = 0; v < numCity; v++) {
if (e[u][v] != INF) {
if (dis[v] > dis[u] + e[u][v])
dis[v] = dis[u] + e[u][v];
}
}
}
book2[c1]=1;
dfs(dis[end], f[c1], c1);//输入剩余路程数,现有消防员数量,当前位置
printf("%d %d", minRoad, numFirmen);
}
我在完成代码时因为错把j打成i导致后三个测试点一直过不去,所以建议在多重循环中不要使用i,j这种相似的变量。
还应注意因为是无向图,所以将路径存入时e[t1][t2] =e[t2][t1]= t3;不要忘记在对称的位置存入。