【题解】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. 利用Jdk 6260652 Bug解析Arrays.asList

    在java.util.ArrayList源码中: c.toArray might (incorrectly) not return Object[] (see 6260652) 产生疑惑: 附上Jav ...

  2. rownum详解

    对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...

  3. Understanding Objective-C Blocks

    The aim of this tutorial is to give a gentle introduction to Objective-C blocks while paying special ...

  4. cmd 中键入netstat,net等出现不是内部或外部命令,也不是可运行的程序或批处理文件

    这是环境变量的问题,查下环境变量中path项是否包含%SystemRoot%\system32;再查下%SystemRoot%\system32有没有netstat.exe这个文件

  5. MATLAB逻辑函数

    %%逻辑函数 %%all:判断是否有元素非0,A是多维矩阵,all(A)是以列为单位来处理的,当前列的逻辑 %值为1,当且仅当当前列的每一个元素都非0 A=[1,2,3;0,2,1;5,0,2]; % ...

  6. django中引入json

    try: from django.utils import simplejson as jsonexcept: import simplejson as json

  7. xampp 安装 mysql-python

    在已经安装brew前提下:brew install mysql-connector-c pip install MySQL-python

  8. > 1366 - Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95...' for column 'description' at row 1 字符串格式错误

    mysql 执行insert时报错 > 1366 - Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95...' for column 'desc ...

  9. java:注解(一)

    什么是注解 注解(Annotation),也叫(metadata)元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法. ...

  10. 提高SharePoint2013服务器性能

    一劳永逸,删除search services application,停止Windows服务:SharePoint Search Host Controller和SharePoint Server S ...