【题解】P1613跑路
【题解】P1613 鸽王跑路
一道思维好题!
考虑\(2^k\)的传递性。直接64遍\(floyd\)求所有\(2^k\)的路径,转移方程是
\(dp(i,j,k)=[dp[i][t][k-1]\)&&\(dp[t][j]][k-1]\)
有了这个之后先\(O(n^3)\)预处理,然后根据这样的数组直接建边跑最短路即可。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<bitset>
#include<vector>
#include<map>
#include<ctime>
#include<cstdlib>
#include<set>
#include<bitset>
#include<stack>
#include<list>
#include<cmath>
using namespace std;
#define RP(t,a,b) for(register int (t)=(a),edd_=(b);t<=edd_;++t)
#define DRP(t,a,b) for(register int (t)=(a),edd_=(b);t>=edd_;--t)
#define ERP(t,a) for(int t=head[(a)];t;t=e[t].nx)
#define Max(a,b) ((a)<(b)?(b):(a))
#define Min(a,b) ((a)<(b)?(a):(b))
#define TMP template<class ccf>
typedef long long ll;
TMP inline ccf qr(ccf k){
char c=getchar();
ccf x=0;
int q=1;
while(c<48||c>57)
q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)
x=x*10+c-48,c=getchar();
if(q==-1)
x=-x;
return x;
}
int m,n;
const int maxn=55;
bool d[maxn][maxn][71];
inline void add(int fr,int to){
d[fr][to][0]=1;
}
const int maxe=51*51*15;
struct E{
int to,nx;
}e[maxe];
int head[maxn];
int cnt;
inline void add2(int fr,int to){
e[++cnt]=(E){to,head[fr]};
head[fr]=cnt;
}
int t1,t2;
queue< int > q;
int ans[maxn];
bool in[maxn];
const int inf=0x3f3f3f3f;
inline void spfa(){
RP(t,1,n)
ans[t]=inf;
ans[1]=0;
in[1]=1;
q.push(1);
while(!q.empty()){
t1=q.front();
q.pop();
in[t1]=0;
ERP(t,t1){
if(ans[e[t].to]>ans[t1]+1){
ans[e[t].to]=ans[t1]+1;
if(!in[e[t].to])
q.push(e[t].to);
in[e[t].to]=1;
}
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
n=qr(1);
m=qr(1);
RP(t,1,m){
t1=qr(1);
t2=qr(1);
add(t1,t2);
}
RP(k,0,65){
RP(i,1,n){
RP(t,1,n){
RP(f,1,n){
d[t][f][k+1]|=(d[t][i][k]&&d[i][f][k]);
}
}
}
}
RP(t,1,n){
RP(i,1,n){
if(t==i)
continue;
RP(k,0,65){
if(d[t][i][k]){
add2(t,i);
break;
}
}
}
}
spfa();
cout<<ans[n]<<endl;
return 0;
}
【题解】P1613跑路的更多相关文章
- 洛谷 P1613 跑路 题解
P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...
- 洛谷P1613 跑路
P1613 跑路 176通过 539提交 题目提供者该用户不存在 标签倍增动态规划 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 这个题的数据.. 题意问题 表意 题目描述 小A的工作不仅繁 ...
- P1613 跑路(倍增)
P1613 跑路(倍增) 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十 ...
- 【Luogu】P1613 跑路
[Luogu]P1613 跑路 一.题目 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资 ...
- 洛谷 P1613 跑路 解题报告
P1613 跑路 题目描述 小\(A\)的工作不仅繁琐,更有苛刻的规定,要求小\(A\)每天早上在\(6:00\)之前到达公司,否则这个月工资清零.可是小\(A\)偏偏又有赖床的坏毛病.于是为了保住自 ...
- 洛谷P1613 跑路(最短路+倍增)
P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...
- 洛谷——P1613 跑路
P1613 跑路 题目大意: 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B ...
- 洛谷 P1613 跑路 (倍增 + DP + 最短路)
题目链接:P1613 跑路 题意 给定包含 \(n\) 个点和 \(m\) 条边的有向图,每条边的长度为 \(1\) 千米.每秒钟可以跑 \(2^k\) 千米,问从点 \(1\) 到点 \(n\) 最 ...
- [Luogu P1613]跑路 (DP+倍增+最短路)
题面 传送门:https://www.luogu.org/problemnew/show/P1613 Solution 挺有意思的一道题. 题面已经挺明显的描述出了这题的主要思想:倍增. 先这样想,我 ...
随机推荐
- ABS已死: Archlinux 放弃支持 ABS
今天访问archlinux官网,突然看到官方放弃支持ABS的新闻,声明如下: 由于 Arch Build System 的相关服务器端脚本的维护开销日益增高,我们决定放弃 abs及其相关的通过 rsy ...
- unsupported Scan, storing driver.Value type []uint8 into type *time.Time 解决方案
数据库取数据的字段为created_at,数据库中类型是TIMESTAMP,允许NULL,此时在取数据的时候就会出现这种报错. 解决方案:在数据库连接的字符串中添加:&parseTime=Tr ...
- Android自定义控件-Path之贝赛尔曲线和手势轨迹、水波纹效果
从这篇开始,我将延续androidGraphics系列文章把图片相关的知识给大家讲完,这一篇先稍微进阶一下,给大家把<android Graphics(二):路径及文字>略去的quadTo ...
- 在程序中使用NV 3D Vision 【转】
http://www.cnblogs.com/gongminmin/archive/2010/11/21/1883392.html 多年前NVIDIA就发布了3D Vision技术,能提供多种立体渲染 ...
- TRIZ系列-创新原理-20-有效作用的连续性原理
有效作用的连续性原理表述例如以下:1)连续实施动作不要中断,物体的全部部分应该一直处于满负荷工作状态.2)去除全部空暇的,中间的动作:3)用循环的动作取代"来来回回"的动作: 这个 ...
- C#控件之ListView
1.添加一行 listView1.Items.Add(new ListViewItem(new string[] {str1, str2, str3 })); 2.鼠标悬停在listview某项时弹出 ...
- 奇怪!post提交 地址栏参数竟然可见
转: http://blog.csdn.net/yuebinghaoyuan/article/details/7727802 在做项目中,form标签中method="post&quo ...
- nfs部署和优化 -2
客户端: cat /etc/passwd 显示用户 weifeng 500 服务端: vim /etc/exports /mnt 192.168.1.105(rw,sync,all_squash, ...
- Android · PendingIntent学习
Intent 是及时启动,intent 随所在的activity 消失而消失 PendingIntent用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. ...
- 淘宝数据库OceanBase SQL编译器部分 源代码阅读--解析SQL语法树
OceanBase是阿里巴巴集团自主研发的可扩展的关系型数据库,实现了跨行跨表的事务,支持数千亿条记录.数百TB数据上的SQL操作. 在阿里巴巴集团下,OceanBase数据库支持了多个重要业务的数据 ...