博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ3417[Poi2013]Tales of seafaring——BFS
阅读量:7003 次
发布时间:2019-06-27

本文共 3766 字,大约阅读时间需要 12 分钟。

题目描述

Young Bytensson loves to hang out in the port tavern, where he often listens to the sea dogs telling their tales of seafaring. Initially, he believed them all, however incredible they sounded. Over time though, he became suspicious. He has decided to write a program that will verify if there may be any grain of truth in those tall stories. Bytensson reasoned that while he cannot tell if the sailors indeed weathered all those storms, he can at least find out if their travel itineraries make sense. This is a task for a programmer, which Bytensson, unfortunately, is not. Help him out!

There are   ports and   waterways connecting them in the waters frequented by the sailors Bytensson listened to. If there is a waterway between two ports, then sailing from one to the other is possible. Any waterway can be sailed in both directions.
Bytensson got to know K seafaring tales. Each tells of a sailor who began his journey in one port, sailed a number of waterways, and ended up in another port, which may have been the one he initially set sail from. The sailor in question may have sailed through the same waterway many times, each time in any direction. 

一个n点m边无向图,边权均为1,有k个询问

每次询问给出(s,t,d),要求回答是否存在一条从s到t的路径,长度为d

路径不必是简单路(可以自交)

输入

In the first line of the standard input, there are three integers, N,M and K (2<=N<=5000,1<=M<=5000,1<=K<=1000000) These denote, respectively: the number of ports in the waters frequented by the sailors who told Bytensson their stories, the number of waterways, and the number of tales.

The M lines that follow specify the waterways. A single waterway's description consists of a single line that contains two integers, a and b (1<=a,b<=N,a<>b) separated by a single space; these specify the numbers of ports at the two ends of this particular waterway.
The K lines that follow specify the tales that Bytensson has heard. A single tale's description consists of a single line with three integers, s,t  and d (1<=S,T<=N,1<=d<=1000000000) separated by single spaces. These indicate that the tale's protagonist set sail from port no. s, ended the journey in port no. t, and sailed exactly d times through various waterways.

输出

Your program should print exactly K lines to the standard output; the i-th of them should contain the word TAK (Polish for yes) if the journey described in the i-th tale (in input order) could have taken place. If it could not, then the line should contain the word NIE(Polish for no).

样例输入

8 7 4
1 2
2 3
3 4
5 6
6 7
7 8
8 5
2 3 1
1 4 1
5 5 8
1 8 10

样例输出

TAK
NIE
TAK
NIE

提示

 
 
因为不必是简单路径,所以可以走最短路到达终点后在最后一条边来回走。
但最后来回走一条路的路径长是偶数,因此要维护每个点的奇偶最短路。
因为询问次数较多,但点数比较小,离线以每个点为起点bfs求一下最短路即可。
注意询问的s和t可能相同,需要判一下这个点有没有出度。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long longusing namespace std;int n,m,k;int head[5002];int to[100010];int next[100010];int f[5002][2];int x,y;vector
v[5002];int ans[1000010];struct node{ int s; int t; int d;}a[1000010];int tot;queue< pair
>q;void add(int x,int y){ tot++; next[tot]=head[x]; head[x]=tot; to[tot]=y;}int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); add(x,y); add(y,x); } for(int i=1;i<=k;i++) { scanf("%d%d%d",&a[i].s,&a[i].t,&a[i].d); v[a[i].s].push_back(i); } for(int i=1;i<=n;i++) { if(v[i].size()) { memset(f,-1,sizeof(f)); f[i][0]=0; q.push(make_pair(i,0)); while(!q.empty()) { int now=q.front().first; int dis=q.front().second; q.pop(); for(int j=head[now];j;j=next[j]) { if(f[to[j]][dis^1]==-1) { f[to[j]][dis^1]=f[now][dis]+1; q.push(make_pair(to[j],dis^1)); } } } int len=v[i].size(); for(int j=0;j

转载于:https://www.cnblogs.com/Khada-Jhin/p/9660329.html

你可能感兴趣的文章
spring高级 之 拦截器
查看>>
安卓的生命周期和布局大概
查看>>
使用APC加速你的PHP网站
查看>>
Redis Windows 服务启动异常 错误码1067
查看>>
关于 pip安装的可能错误的排除
查看>>
SQL-GROUP BY第四课
查看>>
Kubernetes 1.12全新发布!新功能亮点解析
查看>>
大型商城购物车原理
查看>>
CentOS下yum安装PHP,配置php-fpm服务
查看>>
搭建基于SSM的分布式电子商城的框架开源方便大家二次开发(已解决跨域问题)...
查看>>
感恩节那天,亚洲诚信收到了一封来自客户的致谢信……
查看>>
王坚:什么是真正的创新?| 干货
查看>>
SQL Server on Linux入门教程
查看>>
可直接嵌入业务系统为终端客户提供分析服务的阿里云分析型数据库
查看>>
DAO开发实战业务分析
查看>>
1.8 httpd的用户认证
查看>>
Mybatis2
查看>>
追加字节能优化性能
查看>>
哈希表原理
查看>>
学习运维需求分析、域名申请、域名解析、域名备案
查看>>