Codeforces 576D Flights for Regular Customers(矩阵加速DP)
题目链接 Flights for Regular Customers
首先按照$d$的大小升序排序
然后分成$m$个时刻,每条路径一次处理过来。
$can[i][j]$表示当前时刻$i$能否走到$j$
$can$通过上一条路径后的$can$和当前的可行路径矩阵的$d$次幂得到。
这由$floyd$求解即可。考虑到$d$很大,用矩阵快速幂加速。
TLE on test 10
矩阵乘法的时候用$bitset$优化。
更新答案的时候,我们枚举每个点。
若第$1$个点可以走到第$i$个点,则更新答案。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 160;
const int inf = 0x3f3f3f3f; int n, m, ans = inf;
int dis[N][N], f[N][N];
int cnt;
bitset <N> can[N], now[N], tmp[N], base[N]; struct node{
int x, y, d;
void scan(){ scanf("%d%d%d", &x, &y, &d); }
friend bool operator < (const node &a, const node &b){
return a.d < b.d;
}
} e[N]; void Mul(bitset<N> *a, bitset<N> *b){
bitset<N> ret[N];
rep(i, 1, n) rep(j, 1, n) if(a[i][j]) ret[i] |= b[j];
rep(i, 1, n) a[i] = ret[i];
} void Pow(bitset <N> *a, int b){
bitset <N> ret[N];
rep(i, 1, n) ret[i][i] = 1;
for (; b; b >>= 1){
if (b & 1) Mul(ret, a);
Mul(a, a);
}
rep(i, 1, n) a[i] = ret[i];
} int main(){ scanf("%d%d", &n, &m);
rep(i, 1, m) e[i].scan(); sort(e + 1, e + m + 1); rep(i, 1, n) rep(j, 1, n) dis[i][j] = inf;
rep(i, 1, n) dis[i][i] = 0; cnt = 0;
ans = inf;
rep(i, 1, n) can[i][i] = 1; rep(i, 1, m){
int x = e[i].x, y = e[i].y, d = e[i].d;
rep(j, 1, n) tmp[j] = base[j];
Pow(tmp, d - cnt);
Mul(can, tmp);
rep(j, 1, n) rep(k, 1, n) dis[j][k] = min(dis[j][k], dis[j][x] + 1 + dis[y][k]);
rep(j, 1, n - 1) if (can[1][j]) ans = min(ans, d + dis[j][n]);
cnt = d;
base[x][y] = 1;
} if (ans < 0x3f3f3f3f) printf("%d\n", ans);
else puts("Impossible");
return 0;
}
Codeforces 576D Flights for Regular Customers(矩阵加速DP)的更多相关文章
- Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP
题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...
- Codeforces 576D Flights for Regular Customers (图论、矩阵乘法、Bitset)
题目链接 http://codeforces.com/contest/576/problem/D 题解 把边按\(t_i\)从小到大排序后枚举\(i\), 求出按前\((i-1)\)条边走\(t_i\ ...
- Codeforces 576D - Flights for Regular Customers(bitset 优化广义矩阵乘法)
题面传送门 题意: 有一张 \(n\) 个点 \(m\) 条边的有向图,你初始在 \(1\) 号点,边上有边权 \(c_i\) 表示只有当你经过至少 \(c_i\) 条边的时候你才能经过第 \(i\) ...
- Codeforces 576D. Flights for Regular Customers(倍增floyd+bitset)
这破题调了我一天...错了一大堆细节T T 首先显然可以将边权先排序,然后逐个加进图中. 加进图后,倍增跑跑看能不能到达n,不能的话加新的边继续跑. 倍增的时候要预处理出h[i]表示转移矩阵的2^0~ ...
- CF576D Flights for Regular Customers 矩阵乘法 + Bitset优化
%%%cxhscst2's blog Codeforces 576D Flights for Regular Customers(矩阵加速DP) 代码非常优美 + 简洁,学习到了 Code: #inc ...
- (中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。
In the country there are exactly n cities numbered with positive integers from 1 to n. In each city ...
- 576D Flights for Regular Customers
分析 https://www.cnblogs.com/onioncyc/p/8037056.html 写的好像有点问题 但是大致就是这个意思 代码很好理解 代码 #include<bits/st ...
- 【CodeForces】576 D. Flights for Regular Customers
[题目]D. Flights for Regular Customers [题意]给定n个点m条边的有向图,每条边有di表示在经过该边前必须先经过di条边,边可重复经过,求1到n的最小经过边数.n,m ...
- 「CF576D」 Flights for Regular Customers
「CF576D」 Flights for Regular Customers 对不起我又想网络流去了 你看这长得多像啊,走过至少多少条边就是流量下界,然后没上界 但是这个题求的最少走多少条边啊...完 ...
随机推荐
- laravel中redis各方法的使用
在laravel中使用redis自带方法的时候会发现许多原生的方法都不存在了,laravel对其进行了重新的封装但是在文档中并没有找到相关的资料最后在 \vendor\predis\predis\sr ...
- 【 android】When an app is installed on the external storage
When an app is installed on the external storage: The .apk file is saved to the external storage, bu ...
- centos 7 安装WordPress的参考博文
安装方法: https://www.cnblogs.com/flankershen/p/7476415.html 安装完,测试不成功的解决办法: https://blog.csdn.net/u0104 ...
- leetcode-7-hashTable
解题思路: 这道题需要注意的是s和t长度相等,但都为空的情况.只需要扫描一遍s建立字典(char, count),然后扫描t,如果有 未出现的字母,或者键值小于0,就可以返回false了. bool ...
- ProC第一弹
编译pro*c 的makefile例子 原来只需在makefile中追加include $(ORACLE_HOME)/precomp/lib/env_precomp.mk,其他一切按照makefile ...
- LeetCode(292) Nim Game
题目 You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...
- 基于IAR6或者IAR7建立STM32开发工程(通过实际测试,使用IAR6.30.4)
IAR和keil两个开发平台都是arm开发当中比较流行的平台,keil4的版本之间,可以兼容,但是版本4和版本5还是不兼容的,但是IAR的兼容性更加差,好像6.30.x之间是能够兼容的吧,没有实测过, ...
- Monkeyrunner脚本的录制与回放
继上一篇monkeyrunner环境搭建:http://www.cnblogs.com/zh-ya-jing/p/4351245.html 之后,我们可以进一步学习monkeyrunner了. 我也是 ...
- HBase0.94.2-cdh4.2.0需求评估测试报告1.0之四
第二组:文件存储读过程记录 第一组:一个列,四个分区,随机ID 测试列和分区 测试程序或命令 导入文件大小(Mb) 导入文件个数(个) 是否触发flush事件(布尔) 是否触发compact事件(布尔 ...
- luogu2740 [USACO4.2]草地排水Drainage Ditches 最大流EK
练一下最大流 #include <iostream> #include <cstring> #include <cstdio> #include <queue ...