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 ...
随机推荐
- RabbitMQ入门教程——.NET客户端使用
众所周知RabbitMQ使用的是AMQP协议.我们知道AMQP是一种网络协议,能够支持符合要求的客户端应用和消息中间件代理之间进行通信. 其中消息代理扮演的角色就是从生产者那儿接受消息,并根据既定的路 ...
- Join的表顺序
在今天的文章里,我想谈下SQL Server里一个非常有趣的话题:在表联接里,把表指定顺序的话是否有意义?每次我进行查询和性能调优的展示时,大家都会问我他们是否应该把联接中的表指定下顺序,是否会帮助查 ...
- LeetCode #303. Range Sum Query
问题: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- Java魔法堂:找外援的利器——Runtime.exec详解
一.前言 Java虽然五脏俱全但总有软肋,譬如获取CPU等硬件信息,当然我们可以通过JNI调用C/C++来获取,但对于对C/C++和Windows API不熟的码农是一系列复杂的学习和踩坑过程.那能不 ...
- Python字符串的编码与解码(encode与decode)
首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unico ...
- Linux autojump命令
一.简介 autojump是一个命令行工具,它允许你可以直接跳转到你喜爱的目录,而不用管你现在身在何处. 二.安装 yum install autojump 三.用法 j [目录的名字或名字的一部分] ...
- [转]菜鸟程序员之Asp.net MVC Session过期异常的处理
本文转自:http://www.cnblogs.com/JustRun1983/p/3377652.html 小赵是刚毕业的计算机专业方面的大学生,4年的大学时间里面,他读过了很多编程方面的数据,也动 ...
- plain framework 1 参考手册 入门指引之 代码风格
代码风格 介绍 介绍 框架自身采用了google的C++风格,作者也鼓励在你的应用中使用此风格,有关此风格你可以查阅相关资料了解.下面是一段plain framework中的代码,以便大家参考: 你可 ...
- 【HTML5】标记文字
1.用基本的文字元素标记内容 先看显示效果: 对应HTML代码: <!DOCTYPE html> <html lang="en"> <head> ...
- AngularJS 控制器
AngularJS 控制器 控制 AngularJS 应用程序的数据. AngularJS 控制器是常规的 JavaScript 对象. AngularJS 控制器 AngularJS 应用程序被控制 ...