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 Specification:

  Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - the number of cities (and the cities are numbered from 0 to N−1), M- the number of roads, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

  For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

题目解析

本题有若干个城市与若干条连接城市的道路,每个城市中会有一定数量的救援队,要求解初给出两城市之间的最短路条数,并求出在最短路的情况下所能聚集的救援队的最大数量。

第一行给出4个正整数,分别为N(<=500)-城市数量(城市编号由0 ~ N-1,M-道路的条数,C1、C2 – 给出的两个城市。第二行给出N个整数,分别为第0 ~ N-1号城市所拥有的救援队数量。之后给随M行表示道路信息,每行包括道路两端的城市c1、 c2 与道路长度L。

由于城市最高数量只有500,所以可以之间用一个二维数组来储存道路信息,用一维数组teamCnt储存每个城市所拥有的救援队数量,vector<int>pre[i] 存储i城市的前驱城市,之后dijkstra获取每个城市的前驱,这样便可以构建出一张由最短路径组成的邻接表。之后dfs这张邻接表获取C1到C2的最短路径数量与最多课聚集救援队数量即可。

  

 #include <bits/stdc++.h>
using namespace std;
const int MAX = ;
int n, m, c1, c2;
int G[MAX][MAX];
int teamCnt[MAX];
int d[MAX];
bool vis[MAX] = {false};
vector<int> pre[MAX];
void dijkstra(int bg){
fill(d, d + n, INT_MAX); //初始化起点到任何点的距离都为正无穷
d[bg] = ; //起点到本身的距离为0
for(int i = ; i < n ; i++){
int minCity = -, minDis = INT_MAX;
//minCity保存最近的城市 minDis保存最近的距离
for(int j = ; j < n; j++){ //找到当前还未访问过的距离起点最近的城市
if(d[j] < minDis && !vis[j]){
minDis = d[j];
minCity = j;
}
}
if(minCity == -) //若minCity为-1证明其他城市都不与起点连通
return;
vis[minCity] = true; //将找出的最近城市标记为已访问
for(int next = ; next < n; next++){ //遍历所有点
if(!vis[next] && d[next] > d[minCity] + G[minCity][next] && G[minCity][next] != ){
//若存在未访问的点next 与起点之间的距离可被minCity优化 优化该点
d[next] = d[minCity] + G[minCity][next];
pre[next].clear();
pre[next].push_back(minCity);
//将next的前驱清空并将minCity计入next的前驱
}else if(!vis[next] && d[next] == d[minCity] + G[minCity][next] && G[minCity][next] != ){
//若存在未访问的点next 与起点之间的距离和以minCity为中转的距离相等
pre[next].push_back(minCity);
//将minCity计入next的前驱
}
}
}
}
int maxTeamCnt = INT_MIN;
//记录最大聚集救援队数量
int roadCnt = ;
//记录最短路数量
void dfs(int ed, int nowTeamCnt){ //由终点向起点搜索 nowTeamCnt表示当前聚集的救援队数量
if(ed == c1){ //若搜索到起点
maxTeamCnt = max(maxTeamCnt, nowTeamCnt); //获取最大聚集数量
roadCnt++; //最短路加一
}
for(auto i : pre[ed]){ //遍历ed的前驱
if(!vis[i]){ //若前驱还每有被计入道路
vis[i] = true;
dfs(i, nowTeamCnt + teamCnt[i]);
vis[i] = false;
}
}
}
int main()
{
scanf("%d%d%d%d", &n, &m, &c1, &c2);
//输入城市数量n 道路数量m 要计算最短路的城市c1与c2
for(int i = ; i < n; i++) //输入每个城市的救援队数量
scanf("%d", &teamCnt[i]);
memset(G, , sizeof(G)); //初始化任意两个城市之间距离为0
for(int i = ; i < m; i++){ //输入道路信息
int u, v;
scanf("%d%d", &u, &v);
scanf("%d", &G[u][v]);
G[v][u] = G[u][v];
}
dijkstra(c1); //最短路以c1为起点获取前驱
memset(vis, false, sizeof(vis));
dfs(c2, teamCnt[c2]);
printf("%d %d\n", roadCnt, maxTeamCnt);
return ;
}

PTA (Advanced Level) 1003 Emergency的更多相关文章

  1. PAT (Advanced level) 1003. Emergency (25) Dijkstra

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  2. PAT (Advanced Level) 1003. Emergency (25)

    最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...

  3. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  4. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  5. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  6. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  7. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  8. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  9. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

随机推荐

  1. java基础-day7

    第07天 面向对象基础 今日内容介绍 u 面向对象概述 u 面向对象特性之封装 u 面向对象之构造方法 u 类名作为形参和返回值案例 第1章   面向对象概述 1.1      面向对象思想 1.1. ...

  2. RGB-D数据集(SLAM的和行人检测的)

    移动机器人编程一般用mrpt,这个软件来做三维,里面封装了很多常用算法. http://www.mrpt.org/download-mrpt/ SLAM的数据集,其中包括机器人slam http:// ...

  3. .NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序

    先贴上解决方案截图 一.新建4个解决方案文件夹 1-Presentation 2-Application 3-Domain 4-Infrastructure 二.在解决方案文件夹中分别创建项目 其余项 ...

  4. C++ OCCI API数据库操作之连接、返回查询结果集为json格式

    使用C++操作数据库,转换返回结果集为json格式,易于解析. 以下程序的编译.运行环境:Windows 10 1803.VS2017 17.5.2(vc14).解决方案配置:Release.解决方案 ...

  5. powerviot open error in sharepoint 2013

    Testing Service c2WTS +- Service c2WTS found +- Service c2WTS is running +- Path of service: C:\Prog ...

  6. 背水一战 Windows 10 (48) - 控件(集合类): FlipView

    [源码下载] 背水一战 Windows 10 (48) - 控件(集合类): FlipView 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) FlipView 示例Fl ...

  7. Google guava cache源码解析1--构建缓存器(1)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.guava cache 当下最常用最简单的本地缓存 线程安全的本地缓存 类似于ConcurrentHas ...

  8. Spring IOC 容器源码分析 - 获取单例 bean

    1. 简介 为了写 Spring IOC 容器源码分析系列的文章,我特地写了一篇 Spring IOC 容器的导读文章.在导读一文中,我介绍了 Spring 的一些特性以及阅读 Spring 源码的一 ...

  9. 关于WordCount的作业

    一.开发者:201631062418 二.代码地址:https://gitee.com/YsuLIyan/WordCount 三.作业地址:https://edu.cnblogs.com/campus ...

  10. Swift5 语言指南(二十二) 扩展

    扩展为现有的类,结构,枚举或协议类型添加新功能.这包括扩展您无法访问原始源代码的类型的能力(称为追溯建模).扩展类似于Objective-C中的类别.(与Objective-C类别不同,Swift扩展 ...