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
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.
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 1Sample Output
2 4
提交代码
#include<stdio.h>
#include<string.h>
#define maxv 501
typedef struct{
int numv, nume;//顶点数,边数
int vex[maxv];//保存每个顶点上的队伍数;
int arc[maxv][ maxv];//边的权值
}Graph;
Graph G;//图设为全局变量,因为若是每次传参时传递图,图很大,每递归一次,压栈一次,当数据量足够大时,栈会崩溃
int c1, c2, min = 65535, max, num, visit[maxv];//min保存迄今为止的最短路径,max保存最大队伍数,num保存最短路径的不同走法数,visit记录
是否已经走过,c1是始点,c2是终点
void dfs(int i, int minpath, int maxp){
int j;
if(minpath > min)
return ;//若当前已经搜到的最短路径大于迄今为止记录的最短路径,直接返回,不继续搜
if(i == c2){//递归终止条件,当走到终点时
if(min == minpath){//若搜到的当前路径等于最短路径,最短路数量加1,如果需要,更新最大队伍数
num++;
if(maxp > max)
max = maxp;
}
if(min > minpath){
num = 1;
min = minpath;
max = maxp;
}
return ;
}
for(j = 0; j < G.numv; j++){
if(!visit[j] && G.arc[i][j]){
visit[j] = 1;
dfs(j, minpath + G.arc[i][j], maxp + G.vex[j]);
visit[j] = 0;//深搜,递归回来时恢复
}
}
}
int main(){
int i, j, k, w;
scanf("%d %d %d %d", &G.numv, &G.nume, &c1, &c2);
for(i = 0; i < G.numv; i++){
scanf("%d", &G.vex[i]);//输入每个顶点对应的队伍数
visit[i] = 0;//初始化顶点都未被访问过
}
for(i = 0; i < G.numv; i++)
for(j = 0; j < G.numv; j++)
G.arc[i][j] = 0;//初始化边都不存在,置为0
for(i = 0; i < G.nume; i++){//输入存在的边
scanf("%d %d %d", &j, &k, &w);
G.arc[j][k] = G.arc[k][j] = w;
}
visit[c1] = 1;
dfs(c1, 0, G.vex[c1]);
printf("%d %d\n", num, max);
return 0;
}
思路:一遍图的深搜,记录路径长度,找出最短路径,并每次更新最大队伍数。
注意:图的v数组保存的每个顶点的队伍数,邻接矩阵存储图。
输出所有最短路当中,队伍数最大的那个队伍数的值,而不是所有最短路上的队伍数,开始读错了题目