[PAT-甲级]1003.Emergency

来源:互联网 发布:oa数据库设计文档 编辑:程序博客网 时间:2024/05/20 13:36

1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

Sample Input
5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4

解题思路:题意是让我们求无向图中任意两点之间最短距离的条数,以及(前提:该无向图中最短距离的条数可能不止一条)求解最短路径上救援队伍数量最大的一条,即最短路径上各顶点权值之和最大值。


解题思路:典型的DFS,具体的说明在代码中补充了详细的注释!也可以采用迪杰斯特拉算法来做,条条大路通罗马.

#include<stdio.h>#include<stdlib.h>#include<limits.h> int rescue[510]; //保存每个城市的救援队的数量 int exist[510]; // 城市访问标记 int map[510][510]; // mind保存c1和c2两城市之间的最短距离// cnt保存c1和c2两城市之间的最短距离的条数// c1和c2两城市之间最短距离可能有多条,maxt保存救援队数量最多的那一条的救援队数量 int mind, cnt, maxt, n;  // 初始化整个图 void init(int n){for(int i = 0; i < n; i ++){exist[i] = 0;for(int j = 0; j < n; j ++){map[i][j] = INT_MAX;}}}void DFS(int p, int c2, int dist, int numt){// 达到c2点,DFS结束 if(p == c2){// 达到c2点,此时距离比最短距离小 if(dist < mind){cnt = 1;//最短距离条数恢复为1 mind = dist;//更新最短距离mind maxt = numt;//maxt重置,因为之前保存的不是最短距离上的救援队数 }//达到c2点,此时距离跟最短距离相等 else if(dist == mind){cnt ++;//最短距离条数+1 if(maxt < numt)//更新maxt maxt = numt;}return ;}// 剪枝 if(dist > mind)return ;for(int i = 0; i < n; i ++){if(exist[i] == 0 && map[p][i] != INT_MAX){exist[i] = 1;DFS(i, c2, dist+map[p][i], numt+rescue[i]);exist[i] = 0;}}}int main(){int m, c1, c2, x, y, d;mind = INT_MAX;scanf("%d%d%d%d", &n, &m, &c1, &c2);init(n);for(int i = 0; i < n; i ++)scanf("%d", &rescue[i]);while(m --){// 要考虑可能重复输入城市x和y之间的距离,此题并没有设置此case点 scanf("%d%d%d", &x, &y, &d);map[x][y] = map[y][x] =d;if(map[x][y] > d){map[x][y] = d;map[y][x] = d;}}DFS(c1, c2, 0, rescue[c1]);printf("%d %d\n", cnt, maxt);return 0;}


原创粉丝点击