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开发之百度地图导航
本篇主要讲述百度地图的导航功能: 第一步:在使用百度导航之前,我们需要在百度地图开放平台上下载导航的 SDK,共85.8M,网速不好的同学可提前准备好. 第二步:引入导航所需的系统包 将AudioTo ...
- zendstudio文件编码修改问题
转载:http://blog.csdn.net/kunlong0909/article/details/7818620 朋友,在zendstudio ide中,你是否碰到导入一个项目后,发现项目中文件 ...
- 不同servlet版本的web.xml的头部信息
servlet2.5 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&quo ...
- yii2 GridView常见操作
作者:白狼 出处:http://www.manks.top/article/yii2_gridview 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...
- CVEH项目观察与思考
2013-07-01 项目进展: 从启动至今已有三个星期,但是进展甚慢,取得的进展有: A. 封装成库,和HB调用库的接口有些进展,但进未完成 B. 整个框架,类视图,调用视图,只有两三层的进展: C ...
- XML语言基础1
这学期选修了XML技术这门课,没有发课本,于是参考了W3school教程,整理一下上课的内容. 1.XML简介 XML是一种标记语言,很类似HTML,它不是对HTML的替代,而是对HTML的补充.在大 ...
- 一秒钟看懂SaaS、CRM、OA、ERP、HR、进销存
自2014年以来,SaaS.CRM.OA.ERP.HR.APM.进销存.财务系统等,这些名词大量出现在微信朋友圈.电视楼宇广告和千百万融资资讯中.它们到底是什么意思?相互之间又有什么区别?在这个飞速发 ...
- SQLServer基本函数
1.字符串函数 长度与分析用 datalength(Char_expr) 返回字符串包含字符数,但不包含后面的空格 substring(expression,start,length) 取子串 ri ...
- PS网页设计教程——30个优秀的PS网页设计教程的中文翻译教程
PS网页设计教程--30个优秀的PS网页设计教程的中文翻译教程 作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,"熟读唐诗三百首,不会作 ...
- 烂泥:puppet添加带密码的用户
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前一篇文章,我们介绍了有关puppet3.7的安装与配置,这篇文章我们再来介绍下如何利用puppet添加带密码的用户. 要通过puppet添加带密码的用 ...