CodePlus #4 最短路
北极为什么会有企鹅啊,而且北纬91°在哪啊?
关键在建图
因为任意两个城市间都可以互相到达,再加上还有“快捷通道”,光是建图就已经\(\rm{T}\)了……
但这题给了一个提示:除去快捷通道,边权只与两个城市的异或值有关
根据这个性质,我们可以发现,直接建图时,有很多边是多余的,我们只要将异或值的二进制中只含有一个\('1'\)的边加入图中就好了,其他的边都是不必要的
因为假如一条边中有多个\('1'\),我们完全可以通过走其他的只有一个\('1'\)的边来将它“凑”出来,所以这条边的存在毫无意义
知道这个性质后,我们的建图就是\(O(nlogn)\)的了
for(int i=0;i<=n;i++)
for(int j=0;j<=20;j++){
int to=i^(1<<j);
//因为"^"本身就是自己的逆运算,所以计算出来的"to"一定满足"to^i=(1<<j)"
if(to<=n) add(i,to,(1<<j)*c); //因为一共只有n个点,计算出来的"to"不能大于n
}
建完图后,跑一边最短路即可(不建议使用SPFA)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct zzz{
int t,nex,len;
}e[500010<<4]; int head[500010],tot;
void add(int x,int y,int z){
e[++tot].t=y;
e[tot].len=z;
e[tot].nex=head[x];
head[x]=tot;
}
int read(){
int k=0; char c=getchar();
for(;c<'0'||c>'9';) c=getchar();
for(;c>='0'&&c<='9';c=getchar())
k=k*10+c-48;
return k;
}
struct hhh{
int v,l;
bool operator < (const hhh &y) const{
return l > y.l;
}
};
priority_queue <hhh> q;
int dis[500010];
int main(){
int n=read(),m=read(),c=read();
for(int i=0;i<=n;i++)
for(int j=0;j<=20;j++){ // 本题的核心
int to=i^(1<<j);
if(to<=n) add(i,to,(1<<j)*c);
}
for(int i=1;i<=m;i++){
int x=read(),y=read(),z=read();
add(x,y,z);
}
int start=read(),end=read();
memset(dis,127/3,sizeof(dis));
q.push(hhh{start,0}); dis[start]=0;
while(!q.empty()){
hhh k=q.top(); q.pop();
if(k.l!=dis[k.v]) continue;
for(int i=head[k.v];i;i=e[i].nex){
if(dis[e[i].t]>dis[k.v]+e[i].len){
dis[e[i].t]=dis[k.v]+e[i].len;
q.push(hhh{e[i].t,dis[e[i].t]});
}
}
}
cout<<dis[end];
return 0;
}
CodePlus #4 最短路的更多相关文章
- 【LibreOJ】#6354. 「CodePlus 2018 4 月赛」最短路 异或优化建图+Dijkstra
[题目]#6354. 「CodePlus 2018 4 月赛」最短路 [题意]给定n个点,m条带权有向边,任意两个点i和j还可以花费(i xor j)*C到达(C是给定的常数),求A到B的最短距离.\ ...
- 【BZOJ5109】[CodePlus 2017]大吉大利,晚上吃鸡! 最短路+拓扑排序+DP
[BZOJ5109][CodePlus 2017]大吉大利,晚上吃鸡! Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏 ...
- [BZOJ5109][LOJ #6252][P4061][CodePlus 2017 11月赛]大吉大利,今晚吃鸡!(最短路+拓扑排序+传递闭包+map+bitset(hash+压位))
5109: [CodePlus 2017]大吉大利,晚上吃鸡! Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 107 Solved: 57[Sub ...
- LOJ#6354. 「CodePlus 2018 4 月赛」最短路[最短路优化建图]
题意 一个 \(n\) 个点的完全图,两点之间的边权为 \((i\ xor\ j)*C\) ,同时有 \(m\) 条额外单向路径,问从 \(S\) 到 \(T\) 的最短路. \(n\leq 10^5 ...
- BZOJ5109 CodePlus 2017大吉大利,晚上吃鸡!(最短路+拓扑排序+bitset)
首先跑正反两遍dij求由起点/终点到某点的最短路条数,这样条件一就转化为f(S,A)*f(T,A)+f(S,B)*f(T,B)=f(S,T).同时建出最短路DAG,这样图中任何一条S到T的路径都是最短 ...
- 「CodePlus 2018 4 月赛」最短路
$n \leq 100000$,$m \leq 500000$的有向图,两点之间还可以以$a \ \ xor \ \ b$的代价从$a$到$b$,问$s$到$t$的最短路. 被自己蠢哭QAQ 首先两个 ...
- LOJ6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡! 最短路+bitset
题目传送门 https://loj.ac/problem/6252 https://lydsy.com/JudgeOnline/problem.php?id=5109 题解 首先跑最短路,只保留 \( ...
- [Codeplus 4月赛]最短路
题意:理论上是给定一张完全图,有边权,在给一些单向边求最短路. 思路: 我充分体会到了我图论的菜. 理论上建图肯定是不能\(n^2\)的,考虑如何优化呢? 将边权异或值二进制替换,最后一遍最短路就行, ...
- @loj - 6354@「CodePlus 2018 4 月赛」最短路
目录 @description@ @solution@ @accepted code@ @details@ @description@ 企鹅国中有 N 座城市,编号从 1 到 N . 对于任意的两座城 ...
随机推荐
- 移动端专用css
通过设置css属性 -webkit-tap-highlight-color: rgba(0, 0, 0, 0);取消掉手机端webkit浏览器 点击按钮或超链接之类的 默认灰色背景色 设置css属性 ...
- 平衡的阵容 st表学习
模板 预处理 void rmq_isit() { ;i<=n;i++) mx[i][]=mn[i][]=a[i]; ;(<<j)<=n;j++) ;i+(<<j)- ...
- uoj#352. 新年的五维几何(概率期望+爆搜)
传送门 我还以为这是个五维半平面交呢--结果没看数据范围-- 题解 //minamoto #include<bits/stdc++.h> #define R register #defin ...
- GFS安装
GlusterFS 搭建 1.环境要求 IP地址 主机名称 系统 172.16.2.201 test01 Centos 6.4 172.16.2.202 test02 Centos 6.4 172.1 ...
- Python-10-条件和条件语句
num = int(input('Enter a number: ')) if num > 0: print('The number is positive') elif num < ...
- Mybatis Plugin(拦截器)的开发
1.Plugin MyBatis 允许使用插件来拦截的方法调用包括: • Executor (update, query, flushStatements, commit, rollback, g ...
- 2019湘潭校赛 H(dp)
题目传送 dp是常规的:\(m^2\)的预处理:把位置存进vector然后\(O(1)\)算出想要的:WA点:要注意特意设置一下val[i][v.size()]=0,即全天都放鸽子则花费时间为0. # ...
- Hive进阶_Hive数据的导入
使用Load语句执行数据的导入 语法: load data [local] inpath 'filepath' [overwrite] into table tablename [partition ...
- System.Span, System.Memory,还有System.IO.Pipelines
System.Span, System.Memory,还有System.IO.Pipelines 使用高性能Pipelines构建.NET通讯程序 .NET Standard支持一组新的API,Sys ...
- LCD1602显示中文汉字
小子在西藏 2011-11-25编写 特别说明笔者是上面的作者,感谢那些原意分享知识的人.时隔5年我又看到了笔者当年写的东西,我想这期间还有许许多多的人 今天写在博客上,愿更多后来者可以学习. LCD ...