PAT1003:Emergency
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.
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 思路
和PAT1030一样的思路,Dijkstra + DFS的结合求解就行,只不过这道题只要求输出对应最短路径的条数和征召的最多的小组数,比1030要简单点。 代码
#include<vector>
#include<iostream>
#include<math.h>
using namespace std;
const int Inf = pow(,);
vector<int> team();
vector<vector<int>> graph(,vector<int>(,Inf));
vector<int> dis(,Inf);
vector<vector<int>> pre();
vector<bool> visit(,false);
int n,m,c1,c2;
vector<vector<int>> paths;
vector<int> tmp;
int maxteamnum = -; void dfs(int v)
{
if(v == c1)
{
int tmpteamnum = ;
tmp.push_back(v);
paths.push_back(tmp);
for(int i = tmp.size() - ;i >= ;i--)
{
tmpteamnum += team[tmp[i]];
}
if(maxteamnum < tmpteamnum)
{
maxteamnum = tmpteamnum;
}
tmp.pop_back();
return;
}
tmp.push_back(v);
for(int i = ;i < pre[v].size();i++)
dfs(pre[v][i]);
tmp.pop_back();
} int main()
{
cin >> n >> m >> c1 >> c2;
for(int i = ;i < n;i++)
{
cin >> team[i];
}
for(int i = ;i < m;i++)
{
int s,d;
cin >> s >> d;
cin >> graph[s][d];
graph[d][s] = graph[s][d];
} pre[c1].push_back(-);
dis[c1] = ; //Dijkstra
for(int i = ;i < n;i++)
{
int u = -,mindis = Inf;
for(int j = ;j < n;j++)
{
if(!visit[j] && dis[j] < mindis)
{
u = j;
mindis = dis[j];
}
}
if( u == -)
break;
visit[u] = true;
for(int v = ;v < n;v++)
{
if(!visit[v] && graph[u][v] != Inf)
{
if(dis[u] + graph[u][v] < dis[v])
{
dis[v] = dis[u] + graph[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if(dis[u] + graph[u][v] == dis[v])
pre[v].push_back(u);
}
}
}
dfs(c2);
cout << paths.size() << " " << maxteamnum << endl;
}
PAT1003:Emergency的更多相关文章
- PAT-1003 Emergency(Dijkstra)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...
- pat1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT-1003 Emergency (25 分) 最短路最大点权+求相同cost最短路的数量
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- PAT甲级1003. Emergency
PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
- 1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- Emergency(山东省第一届ACM省赛)
Emergency Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Kudo’s real name is not Kudo. H ...
- 1003. Emergency
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- C3P0连接池问题,APPARENT DEADLOCK!!! Creating emergency..... [问题点数:20分,结帖人lovekong]
采用c3p0连接池,每次调试程序,第一次访问时(Tomcat服务器重启后再访问)都会出现以下错误,然后连接库需要很长时间,最终是可以连上的,之后再访问就没问题了,请高手们会诊一下,希望能帮小弟解决此问 ...
随机推荐
- Xcode中的全局异常断点
一旦异常断点被添加,你可以鼠标右键选择 Edit Breakpoint 打开弹出菜单. 改变异常类型为Objective-C,这可以防止C++异常被捕获,你可能不想捕获这些. 因为通常情况下你的App ...
- 算法面试题-leetcode学习之旅(二)
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- android重启代码
首先新建一个app然后添加 android:sharedUserId="android.uid.system" 再添加重启的权限 <uses-permission andro ...
- UTL_HTTP Call a Web Service and Pass Parameters as Part of the URL
SET DEFINE OFF SET SERVEROUTPUT ON DECLARE req UTL_HTTP.REQ; resp UTL_HTTP.RESP; value VARCHAR2(3276 ...
- Struts2技术内幕 读书笔记二 web开发的基本模式
最佳实践 在讨论基本模式之前,我们先说说一个词:最佳实践 任何程序的编写都得遵循一个特定的规范.这种规范有约定俗称的例如:包名全小写,类名每个单词第一个字母大写等等等等;另外还有一些需要我们严格遵守的 ...
- 认识Zygote
概述 在java中不同的虚拟机实例会为不同的应用分配不同内存,为了使Android系统尽快启动,使用了Zygote来预加载核心类库和一些加载时间长的类(超过1250ms),让Dalvik虚拟机共享代码 ...
- 内核调试神器SystemTap — 更多功能与原理(三)
a linux trace/probe tool. 官网:https://sourceware.org/systemtap/ 用户空间 SystemTap探测用户空间程序需要utrace的支持,3.5 ...
- iOS 百度地图计算两个点时间的距离
最近在解项目bug,更新地位城市的时候有个错误,后来想在位置改变多少距离之后,再取更新位置,这个功能去年做过.但是又忘记了! 所以还是记录一下吧. 百度地图提供了一个方法: BMKMapPointFo ...
- Java不走弯路教程(4.Client-Server模式(1)-Server)
4.Client-Server模式(1)-Server 在上一章中,我们完成了MyDataBase.java的编写,类似于一个简单的数据库功能,提供了用户验证,查询操作. 在本章中,我们将继续扩展这个 ...
- 进程间通信——IPC之共享内存
共享内存是三个IPC机制中的一个.它允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在进行的进程之间传递数据的一种非常有效的方式. 大多数的共享内存的实现,都把由不同进程之间共享 ...