POJ2432 Around the world
题意描述
在一个圆上有 \(n\) 点,其中有 \(m\) 条双向边连接它们,每条双向边连接两点总是沿着圆的最小弧连接。
求从 \(1\) 号点出发并回到 \(1\) 号点的一条路径,在满足并非原路返回的情况下满足经过路线数量最小。
如果不存在这样的路径输出 \(-1\)。
算法分析
本来好好的一道 BFS 被我想的辣么复杂...。
如果是单向边就是一遍 BFS 的事,但是这里是双向边又不能原路返回...。
首先考虑特殊建边:
对于 \(edge(u,v)\),求出两者的距离 \(w=dis(u,v)\)。
如果 \(u\to v\) 是顺时针走建立 \(edge(u,v,w),edge(v,u,-w)\)。
否则建立 \(edge(v,u,w),edge(u,v,-w)\)。
然后 BFS 的同时计算走过的边权之和,只要到达 \(1\) 号点时边权之和 \(\neq 0\) 即可。
判重本来是用 \(bool\) 判的,然后喜得 MLE,所以乖乖用 set 了。
当然为了减小常数,dalao 都是直接用 Hash。(蒟蒻我懒...)
没了。
代码实现
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<set>
#define N 5010
#define M 25010
using namespace std;
int n,m,a[N],head[N],cnt=0;
struct Edge{
int nxt,to,val;
}ed[M<<1];
struct node{
int u,dis,step;
};
queue<node>q;
set<pair<int,int> >s;
int read(){
int x=0,f=1;char c=getchar();
while(c<'0' || c>'9') f=(c=='-')?-1:1,c=getchar();
while(c>='0' && c<='9') x=x*10+c-48,c=getchar();
return x*f;
}
int get_dis(int u,int v){
int w=min(abs(u-v),360-abs(u-v));
if((u+w)%360==v) return w;
return -w;
}
void add(int u,int v,int w){
ed[++cnt]=(Edge){head[u],v,w},head[u]=cnt;
ed[++cnt]=(Edge){head[v],u,-w},head[v]=cnt;
return;
}
int bfs(){
q.push((node){1,0,0});
while(!q.empty()){
node now=q.front();q.pop();
int u=now.u,dis=now.dis,step=now.step;
s.insert(make_pair(u,dis));
for(int i=head[u];i;i=ed[i].nxt){
int v=ed[i].to,w=ed[i].val;
if(v==1 && dis+w) return step+1;
if(s.find(make_pair(v,dis+w))!=s.end()) continue;
//set 提供 find 这个成员函数,如果其中没有这个值将返回 s.end()。
q.push((node){v,dis+w,step+1});
}
}
return -1;
}
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=m;i++){//特殊建边。
int u=read(),v=read();
int w=get_dis(a[u],a[v]);
if(w>0) add(u,v,w);
else add(v,u,-w);
}
printf("%d\n",bfs());
return 0;
}
完结撒花
POJ2432 Around the world的更多相关文章
随机推荐
- Django-当前菜单激活状态-模版 request | slice
如何满足这个需求? 1. view中传递过来一个当前页面的参数标识,通过模版语言进行判断 {% if current_page == 'index' %}active{% endif %} # 每一个 ...
- Emgu.CV怎么加载Bitmap
EmguCV 在4.0.1版本之后没办法用Bitmap创建Image了. 我给大家说下 EmguCV怎么加载Bitmap 下边是 EmguCV 官方文档写的,意思是从4.0.1以后的版本不能直接Bit ...
- IDEA中创建父子工程与maven打包Springboot聚合工程报错程序包不存在问题处理
公司新项目需使用java技术栈,便使用IDEA搭建了一个多SpringBoot项目的聚合工程,因为初次使用,遇到了很多问题,maven打包时各种报错,在网上查了好多终于解决了,为巩固记忆,特作此记录. ...
- 微服务 | Spring Cloud(一):从单体SSM 到 Spring Cloud
系列文章目录 微服务 | Spring Cloud(一):从单体SSM 到 Spring Cloud 目录 系列文章目录 前言 单体式架构 微服务架构 优点 缺点 服务发现与弹性扩展 参考 前言 在微 ...
- spring-boot-route(十三)整合RabbitMQ
这篇是SpringBoot整合消息队列的第一篇文章,我们详细介绍下消息队列的相关内容. 消息队列简介 1. 什么是消息队列 MQ(Message Quene):通过典型的生产者和消费者模型,生产者不断 ...
- python之线程了解部分
一.死锁(了解) 死锁产生的4个必要条件: 互斥:一个资源同一时刻只允许一个线程进行访问 占有未释放:一个线程占有资源,且没有释放资源 不可抢占:一个已经占有资源的线程无法抢占到其他线程拥有的资源 循 ...
- ubuntu 19.10 中防火墙iptables配置
$sudo which iptables /usr/sbin/iptables说明有安装 如果没有安装,那么使用sudo apt-get install iptables 安装. 刚装机,是这个样 ...
- git检出某文件的指定版本
比如当时文件所处的版本id是27e6266d86de3e6da6e1e7a8c43a8b51d6a87032 文件名是system/models/waimai/huodongdiscount.mdl. ...
- vue打包之后在本地运行,express搭建服务器,nginx 本地服务器运行
一.使用http-server 1.安装http-server npm install -g http-server 2.通过命令进入到dist文件夹 3.运行http-server 以上在浏览器输入 ...
- PJzhang:Firefox渗透测试插件HackTools样例
猫宁~~~ firefox插件hacktools地址: https://addons.mozilla.org/zh-CN/firefox/addon/hacktools/ HackTools由Ludo ...