P1993 小K的农场
比较裸的差分约束,只是我判负环的时候sb了...
有负环意味着无解
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 100010
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar() using namespace std; int n,m;
int flag;
bool Flag;
int x,y,v;
int d[];
int num[];
queue<int>q;
bool vis[]; struct node{
int n;
int v;
node *next;
}*e[]; void in(int &x){
int y=;
char c=g();x=;
while(c<''||c>''){
if(c=='-')y=-;
c=g();
}
while(c<=''&&c>=''){
x=(x<<)+(x<<)+c-'';c=g();
}
x*=y;
}
void o(int x){
if(x<){
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void push(int x,int y,int v){
node *p;
p=new node();
p->n=y;
p->v=v;
if(e[x]==NULL)
e[x]=p;
else{
p->next=e[x]->next;
e[x]->next=p;
}
} inline void spfa(int x){
if(vis[x]||Flag){
Flag=true;
return;
}
vis[x]=true;
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]=false;
} int main(){
in(n);in(m);
For(i,,m){
in(flag);
in(x);in(y);
if(flag!=)
in(v);
if(flag==)
push(x,y,-v);
if(flag==)
push(y,x,v);
if(flag==){
push(x,y,);
push(y,x,);
}
}
For(i,,n)
push(,i,);
For(i,,n)
d[i]=inf;
spfa();
if(!Flag)
cout<<"Yes";
else
cout<<"No";
return ;
}