题目链接  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)的更多相关文章

  1. Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP

    题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...

  2. Codeforces 576D Flights for Regular Customers (图论、矩阵乘法、Bitset)

    题目链接 http://codeforces.com/contest/576/problem/D 题解 把边按\(t_i\)从小到大排序后枚举\(i\), 求出按前\((i-1)\)条边走\(t_i\ ...

  3. Codeforces 576D - Flights for Regular Customers(bitset 优化广义矩阵乘法)

    题面传送门 题意: 有一张 \(n\) 个点 \(m\) 条边的有向图,你初始在 \(1\) 号点,边上有边权 \(c_i\) 表示只有当你经过至少 \(c_i\) 条边的时候你才能经过第 \(i\) ...

  4. Codeforces 576D. Flights for Regular Customers(倍增floyd+bitset)

    这破题调了我一天...错了一大堆细节T T 首先显然可以将边权先排序,然后逐个加进图中. 加进图后,倍增跑跑看能不能到达n,不能的话加新的边继续跑. 倍增的时候要预处理出h[i]表示转移矩阵的2^0~ ...

  5. CF576D Flights for Regular Customers 矩阵乘法 + Bitset优化

    %%%cxhscst2's blog Codeforces 576D Flights for Regular Customers(矩阵加速DP) 代码非常优美 + 简洁,学习到了 Code: #inc ...

  6. (中等) 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 ...

  7. 576D Flights for Regular Customers

    分析 https://www.cnblogs.com/onioncyc/p/8037056.html 写的好像有点问题 但是大致就是这个意思 代码很好理解 代码 #include<bits/st ...

  8. 【CodeForces】576 D. Flights for Regular Customers

    [题目]D. Flights for Regular Customers [题意]给定n个点m条边的有向图,每条边有di表示在经过该边前必须先经过di条边,边可重复经过,求1到n的最小经过边数.n,m ...

  9. 「CF576D」 Flights for Regular Customers

    「CF576D」 Flights for Regular Customers 对不起我又想网络流去了 你看这长得多像啊,走过至少多少条边就是流量下界,然后没上界 但是这个题求的最少走多少条边啊...完 ...

随机推荐

  1. APP客户端图片上传PHP接口

    1.客户端 file_get_contents($_FILES['img']['tmp_name']) //获取临时目录下的上传文件流,加密传给接口   2.接口处理端 $img = file_get ...

  2. Python9-hashilib模块-day28(大年初三)

    __getitem__\__setitem__\__delitem__ class Foo: def __init__(self,name,age,sex): self.name = name sel ...

  3. 金阳光Android自动化测试第一季

    第一季:http://www.chuanke.com/v1983382-106000-218422.html 第一节:Android自动化预备课程基础(上)     1. 基于坐标点触屏:monkey ...

  4. C语言高效编程的几招,你会了几招了?

    编写高效简洁的C 语言代码,是许多软件工程师追求的目标.本文就工作中的一些体会和经验做相关的阐述,不对的地方请各位指教. 第1 招:以空间换时间 计算机程序中最大的矛盾是空间和时间的矛盾,那么,从这个 ...

  5. BZOJ 3257: 树的难题

    树形DP #include<cstdio> #include<algorithm> #define rep(i,x,y) for (int i=x; i<=y; i++) ...

  6. HDU 5044 Tree LCA

    题意: 给出一棵\(n(1 \leq n \leq 10^5)\)个节点的树,每条边和每个点都有一个权值,初始所有权值为0. 有两种操作: \(ADD1 \, u \, v \, k\):将路径\(u ...

  7. luogu3370 【模板】字符串哈希

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  8. 谋哥:搞APP,做得累的都不对!

    最近谋哥(微信viyi88)我刚加入“秦王会”,思想收到猛烈地冲击,各位大佬的思维有时候会让我大脑短路,收获不少.同时,我也慢慢发现我一直平静的 心开始浮躁,我发现苗头不对,于是开始静下心来.静下心, ...

  9. LiveScript 操作符

    The LiveScript Book     The LiveScript Book 操作符 数字 标准的数学操作符: 1.1 + 2 # => 32.3 - 4 # => -13.6 ...

  10. [SDOI2013]直径 (树的直径,贪心)

    题目链接 Solution 我们直接找到一条直径 \(s\),起点为 \(begin\),终点为 \(end\). 从前往后遍历点 \(u\) ,若子树中最大的距离与 \(dis(u,begin)\) ...