• P5960 【模板】差分约束算法


    #include <bits/stdc++.h>
    #define inf 2333333333333
    #define N 1000010
    #define p(a) putchar(a)
    #define For(i,a,b) for(long long i=a;i<=b;++i)
    //by war
    //2020.8.5
    using namespace std;
    
    long long n,m,flag,x,y,v;
    long long d[N],num[N];
    bool vis[N];
    
    struct node{
        long long n;
        long long v;
        node *next;
    }*e[N];
    
    queue<long long>q;
    
    void in(long long &x){
        long long y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(long long x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void push(long long x,long long y,long long v){
        node *p;
        p=new node();
        p->n=y;
        p->v=v;
        if(e[x]==0)
            e[x]=p;
        else{
            p->next=e[x]->next;
            e[x]->next=p;
        }
    }
    
    void spfa(long long x){
        if(vis[x]||flag){
            flag=1;
            return;
        }
        vis[x]=1;
        for(node *i=e[x];i;i=i->next){
            if(flag) return;
            if(d[i->n]>d[x]+i->v){
                d[i->n]=d[x]+i->v;
                spfa(i->n);
            }
        }
        vis[x]=0;
    }
    
    signed main(){
        in(n);in(m);
        For(i,1,m){
            in(x);in(y);in(v);
            push(y,x,v);
        }
        For(i,1,n)
            push(0,i,0);
        For(i,1,n) d[i]=inf;
        spfa(0);
        if(!flag){
            For(i,1,n) o(d[i]),p(' ');
        }
        else cout<<"NO";
        return 0;
    }
  • 相关阅读:
    Valid Parentheses
    3Sum
    泛型(一)
    Longest Common Prefix
    Roman to Integer
    Integer to Roman
    Container With Most Water
    知道创宇研发技能表v2.2
    anti-dns pinning 攻击
    dominator
  • 原文地址:https://www.cnblogs.com/war1111/p/13438168.html
Copyright © 2020-2023  润新知