PAT 1003. Emergency (25)(俩点间最短路径)
来源:互联网 发布:软件研发管理体系 编辑:程序博客网 时间:2024/05/19 20:00
题目
1003. Emergency (25)时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueAs 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.InputEach 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.OutputFor 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 Input5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output2 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 0x3f3f3f3fmap<int ,vector<int> > pre;//最短路径条数和每条路径能带的team的个数,以及最大的teamint 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 51 2 1 5 3 40 1 10 2 20 3 11 2 12 3 12 4 12 5 13 4 14 5 16 9 0 21 2 1 5 3 40 1 10 2 20 3 11 2 12 3 12 4 12 5 13 4 14 5 16 9 0 41 2 1 5 3 40 1 10 2 20 3 11 2 12 3 12 4 12 5 13 4 14 5 1答案4 133 71 9
0 0
- PAT 1003. Emergency (25)(俩点间最短路径)
- 1003. Emergency (25)-PAT
- (PAT)1003. Emergency (25)
- PAT 1003. Emergency (25)
- PAT 1003. Emergency (25)
- [PAT]1003. Emergency (25)
- 【PAT】1003. Emergency (25)
- PAT 1003. Emergency (25)
- PAT 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 【PAT】1003. Emergency (25)
- PAT 1003. Emergency (25)
- pat 1003. Emergency (25)
- PAT:1003. Emergency (25)
- PAT:1003. Emergency (25)
- 【PAT】1003. Emergency (25)
- pat 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 自定义按钮--Selector
- ==与equal的区别
- POJ 2231 Moo Volume【求和】
- 自定义控件 ——可拖动测量尺子
- Git分布式版本控制系统 基本介绍
- PAT 1003. Emergency (25)(俩点间最短路径)
- V4L2视频应用程序编程架构
- Android accessibilityserver
- Android编译过程详解(二)
- sessionKey/tokenKey相关知识点收集汇总
- Android学习系列(4)--App自适应draw9patch不失真背景
- Button按钮状态背景的设置
- 日记0831
- HTML5中 HTML列表/块/布局 韩俊强的博客