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;
} 
找负圈√
原文地址:https://www.cnblogs.com/gc812/p/5984540.html