graph theory:

Floyd : City of Blinding Lights

重点距离为0 

平时都不用floy,一到用时,竟然写不来。。。

#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

 

转载于:https://www.cnblogs.com/EdsonLin/p/5713286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
组合数学与图论,英文版。内置高清图片,缺陷没有书签。 目录如下: 1 Fundamentals 7 1.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.2 Combinations and permutations . . . . . . . . . . . . . . . . . 11 1.3 Binomial coefficients . . . . . . . . . . . . . . . . . . . . . . . 16 1.4 Bell numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.5 Choice with repetition . . . . . . . . . . . . . . . . . . . . . . 27 1.6 The Pigeonhole Principle . . . . . . . . . . . . . . . . . . . . . 32 1.7 Sperner’s Theorem . . . . . . . . . . . . . . . . . . . . . . . . 36 1.8 Stirling numbers . . . . . . . . . . . . . . . . . . . . . . . . . 39 2 Inclusion-Exclusion 45 2.1 The Inclusion-Exclusion Formula . . . . . . . . . . . . . . . . . 45 2.2 Forbidden Position Permutations . . . . . . . . . . . . . . . . . 48 3 3 Generating Functions 53 3.1 Newton’s Binomial Theorem . . . . . . . . . . . . . . . . . . . 53 3.2 Exponential Generating Functions . . . . . . . . . . . . . . . . 56 3.3 Partitions of Integers . . . . . . . . . . . . . . . . . . . . . . . 59 3.4 Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . 62 3.5 Catalan Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 66 4 Systems of Distinct Representatives 71 4.1 Existence of SDRs . . . . . . . . . . . . . . . . . . . . . . . . 72 4.2 Partial SDRs . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.3 Latin Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4.4 Introduction to Graph Theory . . . . . . . . . . . . . . . . . . 83 4.5 Matchings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5 Graph Theory 91 5.1 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 5.2 Euler Circuits and Walks . . . . . . . . . . . . . . . . . . . . . 96 5.3 Hamilton Cycles and Paths . . . . . . . . . . . . . . . . . . . 100 5.4 Bipartite Graphs . . . . . . . . . . . . . . . . . . . . . . . . 103 5.5 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 5.6 Optimal Spanning Trees . . . . . . . . . . . . . . . . . . . . 108 5.7 Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . 110 5.8 Graph Coloring . . . . . . . . . . . . . . . . . . . . . . . . . 117 5.9 The Chromatic Polynomial . . . . . . . . . . . . . . . . . . . 124 5.10 Coloring Planar Graphs . . . . . . . . . . . . . . . . . . . . . 125 5.11 Directed Graphs . . . . . . . . . . . . . . . . . . . . . . . . 129 6 P´olya–Redfield Counting 135 6.1 Groups of Symmetries . . . . . . . . . . . . . . . . . . . . . 137 6.2 Burnside’s Theorem . . . . . . . . . . . . . . . . . . . . . . 140 6.3 P´olya-Redfield Counting . . . . . . . . . . . . . . . . . . . . 146 A Hints 151 Index 153
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值