题意

给定一个有向图,每条边的花费为1。现在有一个空间跑路器,可以走2^k长度的路,只用花1秒的时间。问从1走到n最少的时间。
n <= 50, k <= 64。

思路

这道题说是倍增,但是写起来不见倍增的影子,我觉得真妙,对倍增有了更膜拜的认识。
我们可以开一个bool矩阵dp【i】【j】【k】,表示i到j是否可以通过2^k的路程到达。更新这个矩阵可以通过类似floyd最短路的思想

if(dp[i][t][k-] && dp[t][j][k-]) dp[i][j][k] = ;

再开一个dis【i】【j】记录距离,跑一遍floyd就可以了
复杂度是O(n*n*n*64)

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = ;
int n,m;
int dp[maxn][maxn][maxn];
ll dis[maxn][maxn]; int main(){
scanf("%d%d", &n, &m);
rep(i, , n) rep(j, , n) dis[i][j] = inff;
for(int i=; i<=m; i++) {
int x,y;
scanf("%d%d", &x, &y);
dis[x][y] = ;
dp[x][y][] =;
} for(int k=; k<=; k++){
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){ for(int t=; t<=n; t++){
if(dp[i][t][k-] && dp[t][j][k-]){
dp[i][j][k] = ;
dis[i][j] = ;
}
}
}
}
} for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
for(int k=; k<=n; k++){
if(dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
} printf("%lld\n", dis[][n]);
return ;
}
												

P1613 跑路 倍增思想 + 邻接矩阵的更多相关文章

  1. P1613 跑路——倍增思想,floyd

    https://www.luogu.org/problemnew/show/P1613 他有一个跑路机器,每次只能跑2k   (单位)路程,每相邻两个点的路程为1,也就是说如果连边1——>2—— ...

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

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

  3. P1613 跑路(倍增 + floyd)

    https://www.luogu.org/problemnew/show/P1613 思路: 1.读入 2.建图 3.对于每一个点,向距离它 2^k 长度的点连一条长度为 1 的边 4.在新图上跑1 ...

  4. LUOGU P1613 跑路 (倍增floyd)

    解题思路 倍增$floyd$,首先设$f[i][j][k]$表示$i$这个点到$j$的距离能否为$2^k$,初值是如果x,y之间有边,那么$f[x][y][0]=1$.转移方程就是$f[i][j][t ...

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

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

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

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

  7. P1613 跑路(倍增)

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

  8. 洛谷P1613 跑路

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

  9. 洛谷 P1613 跑路 解题报告

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

随机推荐

  1. ASP.NET Core MVC 之局部视图(Partial Views)

    1.什么是局部视图 局部视图是在其他视图中呈现的视图.通过执行局部视图生成的HTML输出呈现在调用视图中.与视图一样,局部视图使用 .cshtml 文件扩展名.当希望在不同视图之间共享网页的可重用部分 ...

  2. mcrp 对接软件换

     如何配置UniMRCP Server的启动选项 UniMRCP Server的配置参数,比如:ASR server IP 地址.输出目录. 在哪儿设置这些自定义参数,在插件中如何获取这些参数. 修改 ...

  3. 在docker中开启新的container

    当你在启动某个容器类型的时候  如果产生了类似的错误: docker: Error: No such image: b27f5dfcfc70c16d7b135889460def6b3f831bcc72 ...

  4. python基础--基于套接字进行文件传输、异常处理、socketserver模块

    异常处理: 什么是异常处理: 程序在运行过程中出现了不可预知的错误,并且该错误没有对应的处理机制,那么就会以异常的形式表现出来,造成的影响就是整个程序无法再正常运行 异常的结构: 异常的类型.异常的信 ...

  5. 如何保证FPGA PCIe唤醒能满足PC的100ms 的时间要求(Autonomous Mode)?

    原创By DeeZeng [ Intel FPGA笔记 ]  PC 需要PCIe设备在 100ms 内启动,这样PC 才能扫描到PCIe 设备.对于 FPGA PCIe 板卡,同样也需要满足这个时间要 ...

  6. 【0805 | Day 8】Python进阶(二)

    列表类型内置方法 一.列表类型内置方法(list) 用途:多个爱好.多个武器.多种化妆品 定义:[ ]内可以有多个任意类型的值,逗号分隔元素 # my_boy_friend = list(['jaso ...

  7. egg-sequelize-ts 插件

    egg-sequelize-ts plugin 目的 (Purpose) 能让使用 typescript 编写的 egg.js 项目中能够使用 sequelize方法,并同时得到egg.js所赋予的功 ...

  8. 不可错过的几款GitHub开源项目

    工作之余或者周末感觉无聊?不知道干什么?想继续提高技术,但是不知道做什么的同学,看过来,不妨利用闲暇时间来撸几个 GitHub 上还不错的开源项目,本文推荐的开源项目比较适合新手.及对MVP设计模式不 ...

  9. Security Guards (Gym - 101954B)( bfs + 打表 )

    题意及思路 题目主要是讲先给出所有guard的位置,再给出所有incidents的位置,求出guard到达每个incident处最小的steps,其中guard每次可以向四周8个方向移动. 思路:对于 ...

  10. [ZJOI2011]看电影(组合数学,高精度)

    [ZJOI2011]看电影 这题模型转化很巧妙.(神仙题) 对于这种题首先肯定知道答案就是合法方案除以总方案. 总方案显然是\(k^n\). 那么考虑怎么算合法方案. 当\(n>k\)的时候显然 ...