zoukankan      html  css  js  c++  java
  • 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-1] && dp[t][j][k-1]) dp[i][j][k] = 1;

    再开一个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 << "
    ";
    #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 '
    '
    
    #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<<17;
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    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=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',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 = 109;
                int n,m;
                int dp[maxn][maxn][maxn];
                ll dis[maxn][maxn];
    
     int main(){
                scanf("%d%d", &n, &m);
                rep(i, 1, n) rep(j, 1, n) dis[i][j] = inff;
                for(int i=1; i<=m; i++) {
                    int x,y;
                    scanf("%d%d", &x, &y);
                    dis[x][y] = 1;
                    dp[x][y][0] =1;
                }
    
                for(int k=1; k<=64; k++){
                    for(int i=1; i<=n; i++){
                        for(int j=1; j<=n; j++){
                        
                            for(int t=1; t<=n; t++){
                                if(dp[i][t][k-1] && dp[t][j][k-1]){
                                    dp[i][j][k] = 1;
                                    dis[i][j] = 1;
                                }
                            }
                        }
                    }
                }
      
                for(int i=1; i<=n; i++){
                    for(int j=1; j<=n; j++){
                        for(int k=1; k<=n; k++){
                            if(dis[i][j] > dis[i][k] + dis[k][j])
                                dis[i][j] = dis[i][k] + dis[k][j];
                        }
                    }
                }
    
                printf("%lld
    ", dis[1][n]);
                return 0;
     }
    View Code
    
    
  • 相关阅读:
    大道至简阅读笔记03
    团队项目二阶段-个人总结07
    团队项目二阶段-个人总结06
    团队项目二阶段-个人总结05
    学习进度条06
    领扣(LeetCode)单调数列 个人题解
    领扣(LeetCode)数字转换为十六进制数 个人题解
    领扣(LeetCode)字符串相加 个人题解
    领扣(LeetCode)删除链表中的节点 个人题解
    领扣(LeetCode)有效的括号 个人题解
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/10381288.html
Copyright © 2011-2022 走看看