问题描述

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

思路: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的更多相关文章

  1. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  2. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  3. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  4. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  5. PAT 1003 Emergency

    1003 Emergency (25 分)   As an emergency rescue team leader of a city, you are given a special map of ...

  6. PAT 1003 Emergency[图论]

    1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map ...

  7. 1003 Emergency (25 分)

    1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...

  8. 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 ...

  9. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

随机推荐

  1. 网络安全初级实战笔记(一):owasp zap 暴力破解

    网络安全里装着好多人的侠客梦.但是不能触碰铁律,所以,只小小的自娱自乐. 自己练习,大都会用到DVWA,一个很好的安全测试平台,自己搭建(很简单,傻瓜式搭建),自己设置安全级别,自己验证各种漏洞攻击方 ...

  2. [redis读书笔记] 第一部分 数据结构与对象 压缩列表

    压缩列表是为了节省内存而设计的,是列表键和哈希键的底层实现之一. 压缩列表的逻辑如下,

  3. VFP调整本机日期时间与服务器日期时间一致

    *!*调整本机日期时间与服务器日期时间一致( YYYY-MM-DD HH:MM:SS.FFF )*!*以下cSqlStr及nCon为SQL查询串及连接句柄cSqlStr="Select Sy ...

  4. 能否不同udp socket绑定到同一IP地址和port

    http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-4.html http ...

  5. 1058 - Parallelogram Counting 计算几何

    1058 - Parallelogram Counting There are n distinct points in the plane, given by their integer coord ...

  6. Webpack中hash、chunkhash和contenthash三者的区别

    在webpack中有三种的方式生成哈希值,分别为hash.chunkhash和contenthash.这三种方式有着不同的用处,或者说在webpack的不同环境中,会使用不同的方式生成哈希值.那为什么 ...

  7. Shiro -- (二) 身份验证基本流程

    简介: 在 shiro 中,用户需要提供 principals (身份)和 credentials(证明)给 shiro,从而应用能验证用户身份: principals:身份,即主体的标识属性,可以是 ...

  8. WebStorm新建JS文件、CSS文件时自动生成文件注释

    WebStorm 是jetbrains公司旗下一款优秀的前端开发工具.随着现在大型项目模块越来越多,参与人员也越来越多,实际项目中经常需要明确文件用途和文件的归属,所以创建文件时添加文件注释是一种必要 ...

  9. fastJson&edis

    fastJson&redis 1. fastJson 1.1 依赖 <dependency> <groupId>com.alibaba</groupId> ...

  10. C++ Primer 抄书笔记(一)

    操作系统通过调用main函数(function)来运行C++程序: int main(){ ; } main函数返回类型必为int.大多数系统中main的返回值被用来指示状态.0即成功:非0由系统定义 ...