洛谷P2886牛继电器

传送门啦

倍增 $ Floyd $

注意结构体里二维数组不能开到 $ 2000 $

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Re register 
using namespace std;

inline int read(){
    char ch = getchar();
    int f = 1 , x = 0;
    while(ch > '9' || ch < '0') {if(ch == '-' ) f = -1 ; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar();}
    return x * f;
}

long long n,tot,m,s,t,x,y,z;
long long vis[20050],cnt;
long long w[2050][2050];

struct node{
    long long f[205][205];
    node() {
        memset(f , 0x3f , sizeof(f)) ;
    }
}ans ;

node mul(node a , node b) {
    node res ;
    for(Re int k = 1 ; k <= tot ; ++k)
        for(Re int i = 1 ; i <= tot ; ++i)
            for(Re int j = 1 ; j <= tot ; ++j)
                if(res.f[i][j] > a.f[i][k] + b.f[k][j])
                    res.f[i][j] = a.f[i][k] + b.f[k][j];
    return res;
}

inline long long quick_power(long long k){
    node res ;
    for(Re int i = 1 ; i <= tot ; ++i) 
        res.f[i][i] = 0;
    while(k) {
        if(k & 1) res = mul(res , ans);
        ans = mul(ans , ans ) ;
        k >>= 1;
    }
    return res.f[vis[s]][vis[t]] ;
}

int main(){
    n = read();  m = read(); s = read(); t = read();
    for(Re int i = 1 ; i <= m ; ++i) {
        z = read() ; x = read() ; y = read() ;
        if(!vis[x])  vis[x] = ++tot;
        if(!vis[y])  vis[y] = ++tot;
        ans.f[vis[x]][vis[y]] = ans.f[vis[y]][vis[x]] = min(ans.f[vis[x]][vis[y]] , z) ;
    }
    printf("%lld\n" , quick_power(n));
    return 0;
}

转载于:https://www.cnblogs.com/Stephen-F/p/9929616.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值