zoukankan      html  css  js  c++  java
  • Floyd

    QAQ不资磁负环的最短路算法


    模板酱

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #define inf 233333333333
    using namespace std;
    struct edge
    {
        int from,to,cost;
    };//邻接矩阵 
    edge es[100000];//存边 
    int d[100000];//最短路 
    int v,e;//v顶点数 e边数 
    void floyd(int s)//求解从s出发的最短路 
    {
        for(int i=0;i<v;++i)d[i]=inf;
        d[s]=0;
        while(true)
        {
            bool update=false;
            for(int i=0;i<e;++i)
            {
                edge e=es[i];
                if(d[e.from]!=inf&&d[e.to]>d[e.from]+e.cost)
                {
                    d[e.to]=d[e.from]+e.cost;
                    update=true;
                }
            }
            if(!update)break;
        }
        
    } 
    int main()
    {
        cin>>v>>e;
        int s,t;
        for(int i=0;i<e;++i)
        {
            scanf("%d%d",&s,&t);
            
        }
        return 0;
    }
    抄书哒
    bool floyd()//如果返回true->存在负环 
    {
        memset(d,0,sizeof(d));
        for(int i=0;i<v;++i)
        {
            for(int j=0;j<e;++j)
            {
                edge e=es[j];
                if(d[e.to]>d[e.from]+e.cost)
                {
                    if(i==v-1)return true;
                    //如果第n次仍然更新了,则存在负圈 
                }
            }
        }
        return false;
    } 
    找负圈√
  • 相关阅读:
    Java正则表达式入门
    sql join
    java collection
    JAVA中this用法小结
    SQL Server 触发器
    SQL 存储过程
    123456 所有组合 递归
    sublime plugin & add number to mulitple line .
    重做树莓派系统盘
    制作树莓派系统盘
  • 原文地址:https://www.cnblogs.com/gc812/p/5984540.html
Copyright © 2011-2022 走看看