Detailed negative ring (No subject is recommended)

What is negative ring?

As the name suggests, it is a right side and a negative side to all of the ring

Negative ring happens?

We know that, under normal circumstances, the shortest on the graph is determined. But once the graph has a negative ring, \ (S \) to \ (T \) is the shortest will travel long distances to cover this ring (as long as they arrive), and the trouble took over and over again. Due to the negative side of the ring and the right to be negative, and it is a ring, that is to say to go again and go many times have stayed at the point of entry. Then the shortest path through the negative ring once, this will reduce the cost per bit, if after numerous times, which is infinitesimal, that is the shortest path does not exist . Of course, there is a limited number of times after each point is not more than \ (1 \) times.

Since the negative impact of such a large ring, then it would lead to our next question.

How negative judgment ring?

I believe we all learned at least two of the shortest path calculation method, but some will also \ (dijkstra \) and \ (SPFA \) of these two algorithms (if not please go to learn the SPFA come watch this blog). Two flowers, with each one. Here we talk about \ (SPFA \) . As we all know, \ (SPFA \) judgment way is to continue to tighten each point of the shortest path to the starting point, each will not necessarily receive the most tight, but as long as a solution, will eventually harvest the most compact (which it is also the reason it is so well cards, little by little, you can not slow). As we mentioned earlier, if there is a negative ring, then the shortest path will continue to shrink infinitesimally small. So here we can apply it in this feature. In \ (SPFA \) , each point is the shortest Other \ (n-1 \) point each time to tighten, if tightened \ (n \) times, it is clearly unreasonable. Then we record the number of each point is tightening, there is no point exceed \ (n \) times, we can determine the presence of a negative ring, and if \ (SPFA \) successfully run over, as evidenced by the negative loop does not exist.

Amazingly, I use monotonous queue optimization \ (SPFA \) never had this question, so we still use ordinary \ (SPFA \) to determine it (after all, the question people always can not let you can not determine).

Topic Link

Here is the code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<queue>
#define ll long long
#define gc getchar
#define maxn 2050
#define maxm 3050
using namespace std;

inline ll read(){
    ll a=0;;int f=0;char p=gc();
    while(!isdigit(p)){f|=p=='-';p=gc();}
    while(isdigit(p)){a=(a<<3)+(a<<1)+(p^48);p=gc();}
    return f?-a:a;
}int t,n,m;

struct ahaha{
    int w,to,next;
}e[maxm<<1];int tot,head[maxn];
inline void add(int u,int v,int w){
    e[tot]={w,v,head[u]};head[u]=tot++;
}

queue<int>q;
int d[maxn],s[maxn];
inline bool spfa(){d[1]=0;
    q.push(1);
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];~i;i=e[i].next){
            int v=e[i].to;if(d[v]<=d[u]+e[i].w)continue;
            if(s[v]+1==n)return 1;++s[v];d[v]=d[u]+e[i].w;
            q.push(v);
        }
    }return 0;
}

inline void clear(){tot=0;
    memset(head,-1,sizeof head);
    memset(d,63,sizeof d);
    memset(s,0,sizeof s);
}

int main(){
    t=read();
    while(t--){clear();
        n=read();m=read();
        for(int i=1;i<=m;++i){
            int u=read(),v=read(),w=read();
            add(u,v,w);if(w>=0)add(v,u,w);
        }
        puts(spfa()?"YE5":"N0");
        while(!q.empty())q.pop();
    }
    return 0;
}

Long time no contact with OI, and a little rusty, will hold the examples (in fact, do not have time to find).

I do not know whether my blog help How about you? If you help slightly, it might point a recommendation.

Guess you like

Origin www.cnblogs.com/hanruyun/p/11361632.html