https://www.luogu.org/problemnew/show/P1613

题意:每一秒钟可以跑千米,求最短路

C++版本一

题解:倍增+最短路(Dijkstra)

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[33][N][N];
bool vis[N];
char str;
struct node{
    int u,v,w;
    node(){};
    node(int _u,int _v,int _w):u(_u),v(_v),w(_w){}
    bool operator < (const node &S)const{
        return w>S.w;
    }
}x,tmp;
vector<node>G[N];
priority_queue<node>q;
void ST(){
    for(int p=0;p<=31;p++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                for(int k=1;k<=n;k++){
                    a[p+1][i][j]=a[p+1][i][j]||(a[p][i][k]&&a[p][k][j]);
                    if(a[p+1][i][j]){
                        G[i].push_back({i,j,1});
                        //cout<<p+1<<" "<<i<<" "<<j<<endl;
                        break;
                    }
                }
            }
        }
    }
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        a[0][u][v]=1;
        G[u].push_back({u,v,1});
    }
    ST();
    q.push({0,1,0});
    vis[1]=1;
    while(!q.empty()){
        x=q.top();
        q.pop();//cout<<x.v<<endl;
        if(x.v==n){
            break;
        }
        int u=x.v;
        for(int i=0,j=G[u].size();i<j;i++){
            int v=G[u][i].v;
            if(!vis[v]){
               vis[v]=1;
               q.push({0,v,x.w+1});
            }
        }
    }
    cout<<x.w<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

题解:倍增+最短路(Floyd)

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int n,m;
int u,v;
int a[N][N];
bool f[N][N][N];
int main()
{
    memset(a,0x3f3f3f,sizeof(a));
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        a[i][i]=0;
    for(int i=1;i<=m;i++)
    {
        cin>>u>>v;
        f[u][v][0]=1;
        if(u==v)
            a[u][v]=0;
        else a[u][v]=1;
    }
    for(int s=1;s<=20;s++)
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(f[i][k][s-1] && f[k][j][s-1])
    {
        f[i][j][s]=1;
        if(i==j)
            a[i][j]=0;
        else a[i][j]=1;
    }
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
        a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
    cout<<a[1][n];
    return 0;
}