[Template] negative ring

There is such a magical presence in graph theory algorithms broad and profound!

When we seek the shortest, often find that there is a negative side to the right of the presence, at this time we can not dijksra a good fit for his duty.

So great spfa algorithm appeared (as for students in the fourth year of this algorithm, they must be heard: on SPFA, he died because spfa time at dfs optimization complexity very good, pro-test is dijksta about 10/1, but it has obvious flaws,

Likely to be the topic of people out of extreme data card, so we are talking about today is a commonly used bfs of spfa)

We use a queue to store all the points, with a storage array vis He is visited, and the rest is the relaxation operation.

I generally love to write it to bool type, number of visits if the node> n ring that is negative, returns false

code show as below:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<queue>
#define maxn 120000
using namespace std;

inline int read()
{
    char c=getchar();int re=0,f=1;
    while('0'>c||c>'9')
    {
        if(c=='-')
        {
            f=-1;
        }
        c=getchar();
    }
    while('0'<=c&&c<='9')
    {
        re=re*10+c-'0';
        c=getchar();
    }
    return re*f;
}

int head[maxn],n,m,cnt;
int vis[maxn],dis[maxn],num[maxn];

struct node
{
    int next,to,w;
}e[maxn];

inline void add(int x,int y,int z)
{
    e[++cnt].to=y;
    e[cnt].w=z;
    e[cnt].next=head[x];
    head[x]=cnt;
}


bool spfa(int s)
{
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f3f3f3f,sizeof(dis));
    memset(num,0,sizeof(num));
    queue<int> q;
    q.push(s);
    vis[s]=1;
    dis[s]=0;
    
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        //if(num[u]>=n)return 1; 
        for(int i=head[u];i;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                    ++num[v];
                    if(num[v]>n)
                    {
                        return 1;
                    }
                }
                
            }
        }
    }
    return 0;
}

int main()
{
    int t=read();
    while(t--)
    {
        n=read(),m=read();
        memset(head,0,sizeof(head));
        cnt=0;
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            x=read(),y=read(),z=read();
            if(z>=0)
            {
                add(x,y,z);
                add(y,x,z);
            }
            else
            {
                add(x,y,z);
            }
            
        }
        if(spfa(1))
            {
            cout<<"YE5"<<endl;
        }
        else{
            cout<<"N0"<<endl;
        }
    }
    return 0;
}

However, in the absence of the title right to emphasize the negative side, we still have to use the stack optimized dijksta ah!

--2021 session Dong Chiyuan

Guess you like

Origin www.cnblogs.com/btjzoi/p/11824178.html