题意

给定一个有向图,每条边的花费为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. Initialization failed for 'https://start.spring.io' Please check URL

    错误描述:Initialization failed for 'https://start.spring.io' Please check URL, network and proxy setting ...

  2. Mobile game forensics

    My friend Carrie'd like to know "Garena 传说对决" violates any mobile risks such as insecure d ...

  3. .NET Core CSharp 中级篇 2-2 List,ArrayList和Dictionary

    .NET Core CSharp 中级篇 2-2 本节内容为List,ArrayList,和Dictionary 简介 在此前的文章中我们学习了数组的使用,但是数组有一个很大的问题就是存储空间不足,我 ...

  4. C#中属性的解析

    一.域的概念 C#中域是指成员变量和方法,在OOP编程中(面向对象编程)我们要求用户只知道类是干什么的,而不许知道如何完成的,或者说不允许访问类的内部,对于有必要在类外可见的域,我们用属性来表达,所以 ...

  5. 【JDK】JDK源码分析-AbstractQueuedSynchronizer(2)

    概述 前文「JDK源码分析-AbstractQueuedSynchronizer(1)」初步分析了 AQS,其中提到了 Node 节点的「独占模式」和「共享模式」,其实 AQS 也主要是围绕对这两种模 ...

  6. 分布式ID系列之为什么需要分布式ID以及生成分布式ID的业务需求

    为什么需要分布式id生成系统 在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识.如在美团点评的金融.支付.餐饮.酒店.猫眼电影等产品的系统中,数据日渐增长,对数据分库分表后需要有一个唯一ID ...

  7. 自练Eclipse搭建SSH全自动注解博客项目笔记

    1.创建一个动态的java项目 2.导入搭建所需要的jar包 3.配置web.xml文件 1).头文件 2).struts2的拦截器 3).定位加载Spring容器的配置文件 4).监听 5). 6) ...

  8. Java性能调优的11个实用技巧

    译文出处: ITeye    原文出处:dzone 大多数开发人员认为性能优化是个比较复杂的问题,需要大量的经验和知识.是的,这并不没有错.诚然,优化应用程序以获得最好的性能并不是一件容易的事情,但这 ...

  9. Unity实战案例教程之:不免费的PacMan(初级→中级)

    课程内容介绍: 本套课程适合以下人士: - 免费资料没教会你游戏开发的: - 学了Unity基础不知道怎么用在游戏项目里的: - 想快速开发一款好玩的游戏的: - 想学游戏不知道如何入门的: - 对游 ...

  10. kubernetes离线包分析

    k8s离线包解析 产品地址 鸣谢 大家好,首先感谢大家对我们产品的支持,特别是一些老客户的持续支持,让我可以有动力把这个事情持续进行下去. 感谢大家对付费产品的认可,尊重付费 产品介绍 我们专注于k8 ...