PAT (Advanced Level) Practise 1003 解题报告
问题描述
Emergency (25)
时间限制 400 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard
作者 CHEN, Yue
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
大意是:
n个城市m条路,每个城市都有救援小组。已知各边权值,给定起点和重点,求从起点到重点的最短路径条数以及最短路径上的救援小组数目之和。
n<=500.
解题思路
最短路径,可以用dijsktra或深搜(数据不大)。dijsktra多记录一下路径数和小组总数。
代码
dijsktra
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k,n,m,s,t,c1,c2,a[501][501],b[501],c[501],d[501],e[501],f[501]={0};
memset(a,0x3f,sizeof(a));
memset(d,0x3f,sizeof(d));
cin>>n>>m>>c1>>c2;
for (i=0;i<n;i++) cin>>b[i];
for (i=0;i<m;i++)
{
cin>>j>>k>>t;
a[j][k]=a[k][j]=t;
}
c[c1]=b[c1];
d[c1]=0;
e[c1]=1;
for (i=0;i<n;i++)
{
s=0x3f3f3f3f;
t=-1;
for (j=0;j<n;j++)
if ((f[j]==0)&&(d[j]<s))
{
t=j;
s=d[j];
}
if (t==-1) break;
f[t]=1;
for (j=0;j<n;j++)
if (f[j]==0)
if (d[j]>d[t]+a[t][j])
{
d[j]=d[t]+a[t][j];
c[j]=c[t]+b[j];
e[j]=e[t];
}
else
if (d[j]==d[t]+a[t][j])
{
e[j]+=e[t];
if (c[j]<c[t]+b[j]) c[j]=c[t]+b[j];
}
}
cout<<e[c2]<<' '<<c[c2];
return 0;
}
DFS
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,s,t,maxt,mind,c1,c2,d,a[501][501],b[501]={0},c[501]={0};
void dfs(int k)
{
int i,j;
if (k==c2)
{
if (d<mind)
{
mind=d;
maxt=t;
s=1;
}
else if (d==mind)
{
s++;
if (t>maxt) maxt=t;
}
}
c[k]=1;
for (i=0;i<n;i++)
if ((c[i]==0)&&(a[k][i]>0))
{
d+=a[k][i];
t+=b[i];
dfs(i);
d-=a[k][i];
t-=b[i];
}
c[k]=0;
}
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k;
memset(a,-1,sizeof(a));
cin>>n>>m>>c1>>c2;
for (i=0;i<n;i++) cin>>b[i];
for (i=0;i<m;i++)
{
cin>>j>>k>>t;
a[j][k]=a[k][j]=t;
}
maxt=0;
mind=0x7FFFFF;
t=b[c1];
d=0;
dfs(c1);
cout<<s<<' '<<maxt;
return 0;
}
提交记录
dijsktra
DFS
PAT (Advanced Level) Practise 1003 解题报告的更多相关文章
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- PAT (Advanced Level) Practise 1002 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...
- PAT (Advanced Level) Practise 1001 解题报告
GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise
题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...
随机推荐
- binary(binary区分大小写),unsigned,unsigned zerofill关键字介绍
mysql建表时,每个字段的属性有三个选项: binary,unsigned,unsigned zerofill,作用如下: 一.binary CHAR VARCHAR:值根据缺省字符集以大小写不区分 ...
- LeetCode(67):二进制求和
Easy! 题目描述: 给定两个二进制字符串,返回它们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = " ...
- java web----TCP/DUP 通信
服务端和单客户端通信 注意事项:如果服务端或者客户端采用read() 一个字节这种读取数据,只要另一方没有关闭连接,read是永远读取不到-1,会陷入死循环中: 解决方法:加上一个判断,程序员自己跳出 ...
- 论文阅读笔记二十七:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks(CVPR 2016)
论文源址:https://arxiv.org/abs/1506.01497 tensorflow代码:https://github.com/endernewton/tf-faster-rcnn 室友对 ...
- 饮冰三年-人工智能-linux-09 服务
1:SSH服务(提供远程连接服务) 客户端使用Xshell 链接成功 加快连接速度 关闭防火墙 2:apache 服务(提供网页服务) 2.0 准备环境 关闭防火墙:service iptables ...
- Go之viper配置
这个模块功能强大,读取配置,命令行,监听配置改变. 堪称多面手,在k8s,docker中,都多有应用. 这套应用,主要包括cobra,pflag,viper三件套. 了解得差不多啦...:) pack ...
- sed 详解【转】
原文地址:http://www.cnblogs.com/sparkdev/archive/2017/07/10/7138073.html 基本命令格式 sed [常用选项] 命令文本 输入 常用选项 ...
- HL7消息部分笔记
1.关于HL7标准 HL7是HealthLevel7的缩写,主要用于医疗领域不同的系统.应用之间的信息传递.规范各个系统间的信息传递格式. 2.字段含义: Z信息段: Z信息段是指与HL7第二版标准其 ...
- asp.net core 自定义401和异常显示内容(JWT认证、Cookie Base认证失败显示内容)
asp.net core 2.0使用JWT认证园子里已经有挺多帖子了,但开发中发现认证未授权情况下返回的401状态码是没有任何信息的,业务中可能有需要返回一串错误的Json信息.在这里我分享一个自定义 ...
- 【前端基础系列】slice方法将类数组转换数组实现原理
问题描述 在日常编码中会遇到将类数组对象转换为数组的问题,其中常用到的一种方式使用Array.prototype.slice()方法. 类数组对象 所谓的类数组对象,JavaScript对它们定义为: ...