PTA 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 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, 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 Specification:
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
思路:Floyd+DFS 遇到的问题:
1.一开始题看错了,问路径数量想当然地以为是问最短路径长度,又撞上了如此巧的Sample,惨
2.Floyd翻了个车,里面的if语句条件写的有点问题,碰巧又错得很艺术,简单的样例都能过,大囧 总结:太久不摸键盘了,手生了,以后还是得多多练习
代码:
#include <iostream>
using namespace std; int n,m,c1,c2,dist[][],res[],map[][],len=,sup,ressum,resmax=,count=;
// n城市数,m路径数,c1、c2起止城市,dist存距离,res存救援队数量,map存路径长度
// len存DFS路径长度,sup存c1到c2距离,ressum存DFS过程中救援队数量,resmax存最大救援队数量,count存路径数
bool proc[]; //存储DFS过程中城市经过情况,防止重复经过某一城市 void dfs(int x){
if (len>sup) return; // 走过距离超过c1、c2距离直接返回
if (x==c2){ //到达c2
if (len==sup){ //恰好是最短的路径
count++; //路径数+1
if (ressum>resmax) resmax=ressum; //判断此时救援队数量
}
return;
}
for (int i=;i<n;i++){
if (map[x][i]>= && proc[i]==false){ // i是x可以通往的城市
len+=map[x][i];
ressum+=res[i];
if (len<=sup){
proc[i]=true; //上述处理DFS准备
dfs(i);
proc[i]=false; //下述处理DFS结束后还原
}
len-=map[x][i];
ressum-=res[i];
}
}
return;
} int main(){
int x,y,dis;
//初始化
for (int i=;i<=;i++){
proc[i]=false;
for (int j=;j<=;j++){
dist[i][j]=map[i][j]=-;
}
}
//读入
cin >> n >> m >> c1 >> c2;
for (int i=;i<n;i++)
cin >> res[i];
for (int i=;i<m;i++){
cin >> x >> y >> dis;
dist[y][x]=dist[x][y]=map[y][x]=map[x][y]=dis;
}
//特殊情况判断
if (c1==c2){
cout << "1 " << res[c1];
return ;
}
//Floyd处理
for (int k=;k<n;k++)
for (int i=;i<n;i++)
for (int j=;j<n;j++)
if ((dist[i][j]< && dist[i][k]>= && dist[k][j]>=)||
(dist[i][j]>dist[i][k]+dist[k][j]&&dist[i][k]>=&&dist[k][j]>=))
dist[i][j]=dist[i][k]+dist[k][j];
//DFS
sup=dist[c1][c2];
proc[c1]=true;
ressum=res[c1];
dfs(c1);
//输出
cout << count << " " << resmax;
return ;
}
PTA 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 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
随机推荐
- 大神是如何学习 Go 语言之 Channel 实现原理精要
转自: https://mp.weixin.qq.com/s/ElzD2dXWeldYkJmVVY6Djw 作者Draveness Go 语言中的管道 Channel 是一个非常有趣的数据结构,作为语 ...
- 《自拍教程14》Linux的常用命令
Linux操作系统, 包括我们大家熟知的Android, Ubuntu, Centos, Red Hat, UOS等. 这些常用命令先大概了解下,当然能熟练掌握并运用到实际工作中那最好不过了. 后续技 ...
- Charm Bracelet 一维01背包
A - Charm Bracelet Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Su ...
- Flutter Widgets 之 FutureBuilder
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 展示异步任务状态 当有一个Future(异步)任务需要展示 ...
- tomcat+memcached+nginx部署文档(附完整部署包直接运行即可)
1 前言 1.1 目的 为了正确的部署“ngix+memcached”特编写此部署手册,使安装人员可以通过部署手册知道如何部署系统,也为需要安装该系统的安装人员正确.快速的部署本系统提供帮助. 1.2 ...
- java 开发社区蔬菜、食品交易平台系统 源码
开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+Mysql数据库 运行效果图 源码及原文链接:https://javadao.xyz/forum.php? ...
- Shiro -- (一)简介
简介: Apache Shiro 是一个强大易用的 Java 安全框架,提供了认证.授权.加密和会话管理等功能,对于任何一个应用程序,Shiro 都可以提供全面的安全管理服务.并且相对于其他安全框架, ...
- MySql学习-1.MySql的安装:
1.安装包的下载(mysql-v5.7.25 )(NavicatforMySQL_11.2.15): 链接:https://pan.baidu.com/s/166hyyYd3DMjYhMwdW805F ...
- 最短路问题 Dijkstra算法- 路径还原
// 路径还原 // 求最短路,并输出最短路径 // 在单源最短路问题中我们很容易想到,既然有许多条最短路径,那将之都存储下来即可 // 但再想一下,我们是否要把所有的最短路径都求出来呢? // 实际 ...
- Dijkstra算法 1
// Dijkstra算法,适用于没有负边的情况 // 注意:是没有负边,不是没有负环 // 在这一条件下,可以将算法进行优化 // 从O(v*E)的复杂度,到O(V^2)或者是O(E*log(V)) ...