PAT 1003. Emergency (25)(俩点间最短路径)
2016-08-31 20:42
429 查看
题目
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 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
解题思路
1.题目大意是找出最短路径的条数和找出这些最短路径中team数最多的一组。
2.可以先用
Dijkstra算法求最短路径,其中用
pre保存前一个节点,由于最短路径可能有多条,所以前节点也可能有多个,可用数组保存,最后在用
DFS算法从
c2找到
c1,并计算路径个数和找到最大值。
3.也可以直接用
dfs算法求俩点间的最短路径
AC代码
#include<iostream> #include<cstring> #include<vector> #include<map> #include<algorithm> using namespace std; int n,m,c1,c2; int road[501][501],dist[501],numOfTeam[501]; bool visited[501]; #define INF 0x3f3f3f3f map<int ,vector<int> > pre; //最短路径条数和每条路径能带的team的个数,以及最大的team int cnt=0,team_city[501],MAX_TEAM=0; void Dijkstra(int c){ //标记为访问 visited[c] = true; //更新相邻边最短路径 for (int i = 0; i < n; ++i) { if (dist[i] > dist[c] + road[c][i]&&road[c][i]!=INF) { dist[i] = dist[c] + road[c][i]; //找到比之前小的路径了,pre要重置 pre[i].clear(); pre[i].push_back(c); //如果是相通的,并且等于之前找到的最短路径,则pre增加一个数 }else if (dist[i] == dist[c] + road[c][i]&&road[c][i]!=INF) { pre[i].push_back(c); } } //找到为访问的最小的dist[i],并记录下一个要访问的点 int tem_min = INF,nx=-1; for (int i = 0; i < n; ++i) { //找到未访问的最小值 if (visited[i]) { continue; }else if (dist[i] < tem_min) { tem_min = dist[i]; nx = i; } } //访问下一点 if (nx != -1) { Dijkstra(nx); } } void DFS(int c){ //如果已经找到了c1,则cnt+1,并return if (c==c1) { MAX_TEAM = max(MAX_TEAM,team_city[c1]); cnt++; return; } //找到一个儿子并访问 vector<int> tem = pre[c]; for (int i = 0; i < tem.size(); ++i) { //更新下一点的team,并访问下一点 team_city[tem[i]] = team_city[c] + numOfTeam[tem[i]]; DFS(tem[i]); } } int main(int argc, char *argv[]) { memset(road,INF,sizeof(road)); memset(dist,INF,sizeof(dist)); memset(visited,false,sizeof(visited)); cin >> n >> m >> c1 >> c2; for (int i = 0; i < n; ++i) { cin >> numOfTeam[i]; } int where,to,cost; for (int i = 0; i < m; ++i) { cin >> where >> to >> cost; road[where][to] = road[to][where] = cost; } //用Dijkstra算法找到最短路径并保存pre //初始化 dist[c1] = 0; Dijkstra(c1); //用DFS算法遍历(从c2到c1),记录路径条数和最大team数 //更新visited memset(visited,false,sizeof(visited)); //初始化 team_city[c2] = numOfTeam[c2]; DFS(c2); cout << cnt << " " << MAX_TEAM << endl; return 0; }
测试用例(作者)
6 9 0 5 1 2 1 5 3 4 0 1 1 0 2 2 0 3 1 1 2 1 2 3 1 2 4 1 2 5 1 3 4 1 4 5 1 6 9 0 2 1 2 1 5 3 4 0 1 1 0 2 2 0 3 1 1 2 1 2 3 1 2 4 1 2 5 1 3 4 1 4 5 1 6 9 0 4 1 2 1 5 3 4 0 1 1 0 2 2 0 3 1 1 2 1 2 3 1 2 4 1 2 5 1 3 4 1 4 5 1 答案 4 13 3 7 1 9
相关文章推荐
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- PAT 1003. Emergency (25)<最短路径,最大搜救人数>
- [PAT甲级]1003. Emergency (25)(求城市间最短路径和最大救援队数目)
- 【PAT】1003. Emergency (25) DFS 最短路径及最短路径数
- PAT甲题题解-1003. Emergency (25)-最短路径+路径数目
- 1003. Emergency (25):简单最短路径问题
- PAT 数据结构 06-图5. 旅游规划(25)Dijkstra最短路径算法
- PAT 甲级 1003. Emergency (25)
- PAT 1003. Emergency (25)---深搜
- PAT-A1111. 图-Dijkstra输出最短路径
- 【PAT】1003. Emergency (25)【单源最短路问题/dijkstra算法】
- PAT 1003. Emergency (25)
- PAT_A 1003. Emergency (25)
- 3种方法解决PAT_1003_最短路径
- (PAT)1003. Emergency (25)
- PAT 1003. Emergency (25) 【精华】
- PAT 解题报告 1003. Emergency (25)
- 06-2. 旅游规划(25)最短路径
- pat甲级1003. Emergency (25)
- 2011-02-28 CLRS Chapter25 All-Pairs Shortest Paths 每对顶点间的最短路径