• 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
    ", ans);
    	else puts("Impossible");
    	return 0;
    }
    
  • 相关阅读:
    jxl将list导入到Excel中供下载
    秒转为时分秒格式js
    秒转为时分秒格式
    加载样式js
    myeclipse:web项目不能显示Web App Libraries
    myeclipse出现src作为报名一部分src.com.*
    top命令
    Linux编写一个C程序HelloWorld
    前端jsp页面script引入url项目名使用${appName}
    流量监控iftop安装-CentOS7
  • 原文地址:https://www.cnblogs.com/cxhscst2/p/7628125.html
Copyright © 2020-2023  润新知