码迷,mamicode.com
首页 > Web开发 > 详细

http://47.95.147.191/problem/P3

时间:2020-02-26 19:10:33      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:快速   ons   ==   pac   std   ++   floyd   amp   最短路   

http://47.95.147.191/problem/P3
规定边数的最短路,跑floyd+矩阵快速幂

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
#define mod 1000000007
//by war
//2020.2.26
using namespace std;
long long n,k,S,T,x,y,lim,c;
long long m[1010];
struct matrix{
    long long a[310][310];
    matrix operator*(const matrix&b)const{
        matrix r;
        For(i,1,n)
            For(j,1,n){
                r.a[i][j]=inf;    
                For(k,1,n)
                r.a[i][j]=min(r.a[i][j],a[i][k]+b.a[k][j]);
            }
        return r;
    }
}a;
void in(long long &x)
{
    long long y=1;
    char c=g();x=0;
    while(c<0||c>9)
    {
    if(c==-)
    y=-1;
    c=g();
    }
    while(c<=9&&c>=0)x=(x<<1)+(x<<3)+c-0,c=g();
    x*=y;
}
void o(long long x)
{
    if(x<0)
    {
        p(-);
        x=-x;
    }
    if(x>9)o(x/10);
    p(x%10+0);
}

matrix ksm(matrix a,long long b){
    matrix r=a;b--;
    while(b>0){
        if(b%2==1) r=r*a;
        a=a*a;
        b>>=1;
    }
    return r;
}

int main(){
    in(lim);in(k);in(S);in(T);
    For(i,0,300)
        For(j,0,300)
            a.a[i][j]=inf;
    For(i,1,k){
        in(c);in(x);in(y);
        if(!m[x]) m[x]=++n;
        if(!m[y]) m[y]=++n;
        a.a[m[x]][m[y]]=a.a[m[y]][m[x]]=c;
    }
    matrix r=ksm(a,lim);
    o(r.a[m[S]][m[T]]);
    return 0;
}

 

http://47.95.147.191/problem/P3

标签:快速   ons   ==   pac   std   ++   floyd   amp   最短路   

原文地址:https://www.cnblogs.com/war1111/p/12368053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!