1003. Emergency
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
使用变形的dijkstra算法 并且随时更新最短路径的数目以及能够搜集到的队伍数目。
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std; #define MaxDistance 10000
vector< vector <int>> GMatrix;
vector<int> dist;
vector<int> teams; int N,M,start,en; void dijkstra()
{
vector<int> flag(N, );
vector<int> pathcount(N, );
vector<int> amount(N, );
for (size_t i = ; i < N; i++)//initial the dist
{
if (GMatrix[start][i] != -)
dist.push_back(GMatrix[start][i]);
}
amount[start] = teams[start];
pathcount[start] = ; while()
{
int min = MaxDistance;
int k;
for (size_t j = ; j < N; j++)
{
if ((flag[j] == ) && (dist[j] < min))
{
min = dist[j];
k = j;
}
}
if (k == en|| min==MaxDistance) break; //can not find the mini or find the end node, then break the iterative while(1)
flag[k] = ;
for (size_t j = ; j < N; j++)
{
int temp = dist[k] + GMatrix[k][j];
if ((flag[j] == ) && (temp < dist[j]))
{
dist[j] = temp;
amount[j] = amount[k] + teams[j]; //update the teams you can gather
pathcount[j] = pathcount[k];// update the shortest road sums
}
else if ((flag[j] == ) && (temp == dist[j]))
{
pathcount[j] += pathcount[k]; //update
if (amount[k] + teams[j]>amount[j])
amount[j] = amount[k] + teams[j]; //update
}
}
}
//for (size_t i = 0; i < N; i++)//output the dist for check
//{
// cout << dist[i] << " ";
//}
//cout << endl;
cout << pathcount[en] << " " << amount[en] << endl; }
int main()
{
cin >> N >> M >> start >> en;
for (size_t i = ; i < N; i++)//input the teams
{
int t; cin >> t;
teams.push_back(t);
}
for (size_t i = ; i < N; i++)//initial the GMtraix
{
vector<int> arr(N, MaxDistance);
GMatrix.push_back(arr);
GMatrix[i][i] = ;//for the same start and end, the dist=0;
}
for (size_t i = ; i < M; i++)//input the road to the graph
{
int left, right, road;
cin >> left >> right >> road;
GMatrix[left][right] = road; GMatrix[right][left] = road;
}
dijkstra(); return ;
}
1003. Emergency的更多相关文章
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003 Emergency
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of ...
- PAT 1003 Emergency[图论]
1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map ...
- 1003 Emergency (25 分)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...
- 1003 Emergency (25)(25 point(s))
problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...
- PAT甲级1003. Emergency
PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
随机推荐
- iOS 学习 - 9.Block 入门
来自李明杰的视频. block 用来保存一段代码 block 的标志:^ block 跟函数很像: 1).可以保存代码 2).有返回值 3). 有形参 temp1:没有返回值.没有形参的 blo ...
- IOS设计模式-组合设计模式
前言:本篇博文将让你学会软件开发中的"何为树形结构"."何为组合模式"."组合模式可以解决的问题"等相关知识. 内容大纲: 1.树形结构 2 ...
- HttpClient 教程 (A)
前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了.Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需 ...
- github邮箱验证技巧
申请的github账号,绑定邮箱之后才能创建库,而反复几次的发送邮件均为收到验证邮件,猜测有两个原因: 1.腾讯邮件服务器屏蔽了github的来信 (腾讯不会这么狭隘的,×) 2.自己邮箱的域名黑名单 ...
- java中实现定时功能
网上资料: 我们可以使用Timer和TimerTask类在java中实现定时任务,详细说明如下: 1.基础知识java.util.Timer一种线程设施,用于安排以后在后台线程中执行的任务.可安排任务 ...
- 持续集成 .Net手册--提升开发效率和质量
参考:http://blog.csdn.net/chelsea/article/details/132525 持续集成 .Net手册 一.概念 Martin Fowler的文章:Continuous ...
- mysql锁机制总结
1.隔离级别 (1)读不提交(Read Uncommited,RU) 这种隔离级别下,事务间完全不隔离,会产生脏读,可以读取未提交的记录,实际情况下不会使用. (2)读提交(Read commited ...
- 十五天精通WCF——第六天 你必须要了解的3种通信模式
wcf已经说到第六天了,居然还没有说到这玩意有几种通信模式,惭愧惭愧,不过很简单啦,单向,请求-响应,双工模式,其中的第二种“请求-响应“ 模式,这个大家不用动脑子都清楚,这一篇我大概来分析下. 一: ...
- js 浮点运算出现的精度丢失问题
var myf='6.202555'; myf=Number(myf).toFixed(2);//使用方法 Number.prototype.toFixed = function(scale) { v ...
- 学会使用简单的 MySQL 常用操作
一.MySQL 数据库的基本安装 # yum 安装 mysql 版本:5.1.73 [root@mysql ~]# yum install -y mysql-server mysql # 启动 MyS ...