graph theory:

Floyd : City of Blinding Lights

Focus distance is 0 

I don't usually use floy, but when I use it, I can't even write it. . .

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long ll;
const int N = 500;
const int inf = 0x3f3f3f;
int g[N][N];

int main(){
    //cout<<inf<<endl;
   // cout<<400*400*350/2<<endl;
   // cout<<inf+inf<<endl;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            g[i][j] = inf;
            if(i==j)g[i][j] = 0;
        }
    }
    int n,m;
    scanf("%d%d",&n,&m);
    //scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        u - ;
        v- ; _
        g[u][v] = w;
    }
    for(int k=0;k<n;k++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(g[i][j]>0&&g[i][k]<inf&&g[k][j]<inf&&g[i][j]>g[i][k]+g[k][j]){
                    g[i][j] = g[i][k]+g[k][j];
                    //g[k][i] = g[i][j]+g[j][k];
                }
            }
        }
    }
    int Q;
    scanf("%d",&Q);
    for(int i=0;i<Q;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        u - ;
        v--;
        if(g[u][v]!=inf){cout<<g[u][v]<<endl;}
        else cout<<-1<<endl;
    }
    return 0;
}
View Code

 

Reprinted in: https://www.cnblogs.com/EdsonLin/p/5713286.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326431317&siteId=291194637