【PAT】1003. Emergency (25) DFS 最短路径及最短路径数

来源:互联网 发布:pg数据库 知乎 编辑:程序博客网 时间:2024/05/20 11:36

1003. Emergency (25)

时间限制
400 ms
内存限制
32000 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


题意:有n个城市,每个城市都有自己的救援队,给定m条路径,问从城市c1到c2去救援的最短路径数是多少及能最大的救援队伍。

参考http://blog.csdn.net/iaccepted/article/details/21451949

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxN 505#define Inf 0x3f3f3f3fusing namespace std;int resTm[maxN];bool visited[maxN];int map[maxN][maxN];int N, M, C1, C2;int x, y, L;int minDist = Inf;//最短路径距离和int maxRes; //最大救援数和int ccount; //路径数目//start->起点 end->终点 dist->路径长度 resNum->救援队数目 void dfs(int start, int end,int dist, int resNum){if (start == end)//停止条件{if (dist < minDist){minDist = dist;ccount = 1;//找到更短的路径maxRes = resNum;//救援队数目}else{if (dist == minDist){ccount++;//最短路径数+1if (resNum > maxRes)//更新最大救援数{maxRes = resNum;}}}return;}if (dist > minDist)return;//当前路径长度大于最短路径长度->进行剪枝for (int k = 0; k < N; k++){if (!visited[k] && map[start][k] != Inf)//判断是否访问过且是否可达{visited[k] = true;//回溯时避免出现环,即从end又找回到startdfs(k, end,map[start][k]+dist, resTm[k] + resNum);//对k的邻点进行dfs深搜visited[k] = false;//访问完所有邻点(邻点的邻点)后撤销标记,方便后续查找}}}int main1003(){//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);memset(map, Inf, sizeof(map));cin >> N >> M >> C1 >> C2;for (int i = 0; i < N; i++){cin >> resTm[i];visited[i] = false;}for (int i = 0; i < M; i++){cin >> x>>y >> L;map[x][y] = map[y][x] = min(L,map[x][y]);}dfs(C1, C2, 0, resTm[C1]);cout << ccount << " " << maxRes << endl;return 0;}/*Sample Input5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output2 4*/




0 0
原创粉丝点击