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中的调试工具栏简介
如下图所示: 从左至右,第一个按钮用来隐藏调试区域. 第二个按钮向你展示断点是否被全局开启或禁用.如果它不是高亮蓝色,则没有断点会被触发. 第三个按钮暂停或继续程序的执行,你一般点击它继续运行到程序的 ...
- 如何编写51单片机超声波测距SR04_lcd1602显示程序
超声波测距在我们日常生活中很常见,比如说车在倒退的时候,为了防止车撞到障碍物,会在车尾加上一个超声波测距模块.在智能车比赛中,也有超声波测距模块等等.可见超声波非常的重要,接下来,我们上代码研究一下如 ...
- Sample Code for Qp_preq_pub.Price_request Api to Simulate an Ask for Promotion Modifier
DECLARE p_line_tbl QP_PREQ_GRP.LINE_TBL_TYPE; p_qual_tbl QP_PREQ_GRP.QUAL_TBL_TYPE; p_line_attr_tbl ...
- iOS监听模式系列之NSNotificationCenter的简单使用
NSNotificationCenter 对于这个没必要多说,就是一个消息通知机制,类似广播.观察者只需要向消息中心注册感兴趣的东西,当有地方发出这个消息的时候,通知中心会发送给注册这个消息的对象.这 ...
- 带三方登录(qq,微信,微博)
实现QQ.微信.新浪微博和百度第三方登录(Android Studio) 前言: 对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于 ...
- iOS中动态计算不同颜色、字体的文字高度
在改项目bug的时候,有一个问题动态计算label的高度,前开发者竟然用字符串长度除以14.16这样的常量来计算是否换行,结果cell的高度问题非常严重. 因为label内容里有部分关键字是要另一种颜 ...
- nasm中的表达式
nasm表达式支持2个特殊的记号 $和$$;前者标识其所在源码行的开始处地址,所以你可以这样写死循环: jmp $ 而后者标识当前段开始处的地址,你可以通过: $-$$ 找出当前代码在段内的偏移. n ...
- ELF 文件 动态连接 - 延迟绑定(PLT)
PLT 全称:Procedure Linkage Table ,直译:过程连接表 由于在动态连接中,程序的模块之间包含了大量的函数引用,所以在程序开始执行前,动态链接会耗费较多的时间用于模块之间函数引 ...
- Linux命令之文件搜索
locate 文件名 locate只能搜索文件名,不能搜索文件大小.搜索速度快. locate并不会搜索到那些新加入的文件.新加入文件后,使用updatedb,更新数据库后,再使用locate搜索. ...
- java中Scanner类nextLine()和next()的区别和使用方法
转载:http://blog.csdn.net/zhiyuan_ma/article/details/51592730 在实现字符窗口的输入时,很多人更喜欢选择使用扫描器Scanner,它操作起来比较 ...