【题解】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跑路的更多相关文章

  1. 洛谷 P1613 跑路 题解

    P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...

  2. 洛谷P1613 跑路

    P1613 跑路 176通过 539提交 题目提供者该用户不存在 标签倍增动态规划 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 这个题的数据.. 题意问题 表意 题目描述 小A的工作不仅繁 ...

  3. P1613 跑路(倍增)

    P1613 跑路(倍增) 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十 ...

  4. 【Luogu】P1613 跑路

    [Luogu]P1613 跑路 一.题目 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资 ...

  5. 洛谷 P1613 跑路 解题报告

    P1613 跑路 题目描述 小\(A\)的工作不仅繁琐,更有苛刻的规定,要求小\(A\)每天早上在\(6:00\)之前到达公司,否则这个月工资清零.可是小\(A\)偏偏又有赖床的坏毛病.于是为了保住自 ...

  6. 洛谷P1613 跑路(最短路+倍增)

    P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...

  7. 洛谷——P1613 跑路

    P1613 跑路 题目大意: 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B ...

  8. 洛谷 P1613 跑路 (倍增 + DP + 最短路)

    题目链接:P1613 跑路 题意 给定包含 \(n\) 个点和 \(m\) 条边的有向图,每条边的长度为 \(1\) 千米.每秒钟可以跑 \(2^k\) 千米,问从点 \(1\) 到点 \(n\) 最 ...

  9. [Luogu P1613]跑路 (DP+倍增+最短路)

    题面 传送门:https://www.luogu.org/problemnew/show/P1613 Solution 挺有意思的一道题. 题面已经挺明显的描述出了这题的主要思想:倍增. 先这样想,我 ...

随机推荐

  1. httperf+autobench测试web应用

    测试性能相关的概念理解 httperf使用 主页:  http://www.hpl.hp.com/research/linux/httperf/ 下载: http://httperf.googleco ...

  2. [TJOI2014] 上升子序列

    刚刚做的时候一看:这不是个傻逼题吗hhhhh....然后发现写完了过不了样例,仔细一看题:同构的算一种. 这可咋办啊? 其实很简单,设f[i] 为 以a[i] 结尾的上升子序列个数,我们考虑当前如果算 ...

  3. 记录下我的阿里云centos服务器之路

    以下内容都已经过试验,边走边记,懒得排版 安装aphach yum install -y httpd systemctl start httpd netstat -tulp    安装桌面 尽量不用桌 ...

  4. Oracle 检查表空间使用情况

    --检查表空间使用情况  SELECT f.tablespace_name       , a.total "total (M)"       , f.free "fre ...

  5. wmware下载地址

    https://my.vmware.com/cn/group/vmware/info?slug=desktop_end_user_computing/vmware_workstation/8_0 粗体 ...

  6. ZOJ - 3228 Searching the String (AC自己主动机)

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  7. C#里判断字符串是否为纯数字

    c bool IsNumeric(string str) //接收一个string类型的参数,保存到str里 { if (str == null || str.Length == 0) //验证这个参 ...

  8. Linux学习之十一-Linux字符集及乱码处理

    Linux字符集及乱码处理 1.字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等.字符集(Character set)是多个字符的集合,字符集种类较多,每个 ...

  9. mysqldumps 远程备份

    普通模式 mysqldump -uroot -ppassword -h10.26.114.25 -P3306 --databases databasename > XXX.sql 多条在一起模式 ...

  10. linux虚拟机上挂载U盘

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTQwMjU5Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...