[BZOJ1202] [HNOI2005] cunning businessman

Solution

   emmm ...... differential constraints bare title. Note that the amount of income the value of positive and negative, so do not even try to be smart like me 0 edge. . .

  Also, multiple sets of data is not empty, OI burst zero See ancestors. . .

  Code

#include<bits/stdc++.h>
using namespace std;
const int N=105,M=1005;
inline int read(){
    int x=0,w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x; 
}
struct edge{
    int v,w,last;
}e[M<<1];
int tot,tail[N];
inline void add(int x,int y,int z){
    e[++tot]=(edge){y,z,tail[x]};
    tail[x]=tot;
}
int t,n,m,d[N];
bool vis[N];
bool dfs_SPFA(int x){
    vis[x]=true;
    for(int p=tail[x];p;p=e[p].last){
        int &v=e[p].v,&w=e[p].w;
        if(d[x]+w<d[v]){
            d[v]=d[x]+w;
            if(vis[v]||!dfs_SPFA(v))
                return false;     
        }
    }
    vis[x]=false;
    return true;
}
bool check(){    
    memset(d,0,sizeof d);
    memset(vis,0,sizeof vis);
    for(int i=0;i<=n;++i)
        if(!dfs_SPFA(i)) return false;
    return true;
}
int main(){
    t=read();
    while(t--){    
        tot=0;memset(tail,0,sizeof tail);
        n=read(),m=read();
        while(m--){
            int x=read(),y=read(),z=read();//y,x-1
            add(y,x-1,-z),add(x-1,y,z);
        }
        //for(int i=0;i<n;++i) add(i+1,i,0);
        puts(check()?"true":"false");
    }
    return 0;
}
BZOJ1202

 

Guess you like

Origin www.cnblogs.com/gosick/p/11246892.html