【PAT】1003. Emergency (25) DFS 最短路径及最短路径数
来源:互联网 发布:pg数据库 知乎 编辑:程序博客网 时间:2024/05/20 11:36
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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample 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*/
- 【PAT】1003. Emergency (25) DFS 最短路径及最短路径数
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- PAT 1003. Emergency (25)<最短路径,最大搜救人数>
- 1003. Emergency (25):简单最短路径问题
- 最短路径数 Dijkstra+dfs
- pat-1087【最短路径】
- PAT 1030 最短路径
- 最短路径条数
- PAT 1018 Public Bike Management(最短路径+DFS)
- BFS循环求迷宫最短路径及路径数
- 迷宫问题,求最短路径长度、最短路径条数、所有最短路径,DFS+BFS+栈+队列
- PAT:图的最短路径
- 最短路径算法
- 最短路径算法
- 最短路径理解
- 最短路径算法
- 最短路径算法
- 最短路径问题
- 左式堆--C语言实现
- Java对象的硬、软、弱和虚引用
- halcon基础之Blob Analysis
- 老友记一路相伴---2015年12月英语月总
- 堆排序的初步学习
- 【PAT】1003. Emergency (25) DFS 最短路径及最短路径数
- CCA Spark and Hadoop 开发者认证技能点【2016只为hadoop达到巅峰】
- mysql基础学习笔记
- Pashmak and Garden
- watchman--no-pretty get-sockname returned with exit code 1 ERROR
- 20160101罗振宇的跨年演讲
- oozie ErrorCode 含义
- 客户端在连接服务器报10061错误的原因
- 2440重新学习