P1613 跑路 倍增思想 + 邻接矩阵
题意
给定一个有向图,每条边的花费为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 跑路 倍增思想 + 邻接矩阵的更多相关文章
- P1613 跑路——倍增思想,floyd
https://www.luogu.org/problemnew/show/P1613 他有一个跑路机器,每次只能跑2k (单位)路程,每相邻两个点的路程为1,也就是说如果连边1——>2—— ...
- 洛谷 P1613 跑路 (倍增 + DP + 最短路)
题目链接:P1613 跑路 题意 给定包含 \(n\) 个点和 \(m\) 条边的有向图,每条边的长度为 \(1\) 千米.每秒钟可以跑 \(2^k\) 千米,问从点 \(1\) 到点 \(n\) 最 ...
- P1613 跑路(倍增 + floyd)
https://www.luogu.org/problemnew/show/P1613 思路: 1.读入 2.建图 3.对于每一个点,向距离它 2^k 长度的点连一条长度为 1 的边 4.在新图上跑1 ...
- LUOGU P1613 跑路 (倍增floyd)
解题思路 倍增$floyd$,首先设$f[i][j][k]$表示$i$这个点到$j$的距离能否为$2^k$,初值是如果x,y之间有边,那么$f[x][y][0]=1$.转移方程就是$f[i][j][t ...
- [Luogu P1613]跑路 (DP+倍增+最短路)
题面 传送门:https://www.luogu.org/problemnew/show/P1613 Solution 挺有意思的一道题. 题面已经挺明显的描述出了这题的主要思想:倍增. 先这样想,我 ...
- 洛谷P1613 跑路(最短路+倍增)
P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...
- P1613 跑路(倍增)
P1613 跑路(倍增) 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十 ...
- 洛谷P1613 跑路
P1613 跑路 176通过 539提交 题目提供者该用户不存在 标签倍增动态规划 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 这个题的数据.. 题意问题 表意 题目描述 小A的工作不仅繁 ...
- 洛谷 P1613 跑路 解题报告
P1613 跑路 题目描述 小\(A\)的工作不仅繁琐,更有苛刻的规定,要求小\(A\)每天早上在\(6:00\)之前到达公司,否则这个月工资清零.可是小\(A\)偏偏又有赖床的坏毛病.于是为了保住自 ...
随机推荐
- python3 读取文件-2
1.脚本 from sys import argv script,filename = argv#以读的模式打开txt文件txt = open(filename,'r+')print ("t ...
- Ubuntu启动Apache
划重点:最重要的是找到一个文件--------startup.sh 如果你是按照百度上的方法下载的Apache2,那么就按照百度的方式来 但是异曲同工 如果你是自己在网上下载的Apache到的磁盘 找 ...
- 基于高德开放平台的 NODE 天气信息组件
看看了画在手上的Armani手表,马上就快到了下班的时间了,心里总觉的空唠唠的, 好像空缺了什么一样,仔细的想了一想,微微叹了一口气,觉得是时候在这里和大家分享一下原因了........ 首先: ## ...
- Thinkphp 3.2.3 parseWhere设计缺陷导致update/delete注入 分析
目录 分析 总结 分析 首先看一下控制器,功能是根据用户传来的id,修改对应用户的密码. 13行把用户传来的id参数送入where()作为SQL语句中的WHERE语句,将pwd参数送入save()作为 ...
- redis缓存穿透,缓存击穿,缓存雪崩原因+解决方案
一.前言 在我们日常的开发中,无不都是使用数据库来进行数据的存储,由于一般的系统任务中通常不会存在高并发的情况,所以这样看起来并没有什么问题,可是一旦涉及大数据量的需求,比如一些商品抢购的情景,或者是 ...
- chapter01作业
1. 请用命令查出ifconfig命令程序的绝对路径 [root@localhost chen]# which ifconfig /usr/sbin/ifconfig 2.请用命令展示以下命令哪些是内 ...
- abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十二)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- MySQL多表(理论知识总结)
1. 多表关系 外键 foreign key 添加外键语法: alter table 表名1 add foreign key(外键名称) references 表名2(主键名称 ...
- let 、const 、var、function声明关键字的新理解
今天在群里看到大佬们讨论let .const 的提升问题,有个大佬问 三种声明都在什么阶段提升? 什么阶段?这个真不清楚,以前是只知道let.const存在死区,没有变量提升,一下子就懵了 后经手 ...
- Java源码之ConcurrentHashMap
⑴背景 ConcurrentHashMap是线程安全高效的HashMap.而HashMap在多线程情况下强行使用HashMap的put方法可能会导致程序死循环,使CPU使用率达到100%.(http: ...