P1993 小K的农场

P1993 小K的农场
比较裸的差分约束,只是我判负环的时候sb了...

有负环意味着无解

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 100010
 17 #define For(i,a,b) for(register int i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 
 23 int n,m;
 24 int flag;
 25 bool Flag;
 26 int x,y,v;
 27 int d[100000];
 28 int num[100000];
 29 queue<int>q;
 30 bool vis[100000];
 31 
 32 struct node{
 33     int n;
 34     int v;
 35     node *next;
 36 }*e[1000000];
 37 
 38 void in(int &x){
 39     int y=1;
 40     char c=g();x=0;
 41     while(c<'0'||c>'9'){
 42         if(c=='-')y=-1;
 43         c=g();
 44     }
 45     while(c<='9'&&c>='0'){
 46         x=(x<<1)+(x<<3)+c-'0';c=g();
 47     }
 48     x*=y;
 49 }
 50 void o(int x){
 51     if(x<0){
 52         p('-');
 53         x=-x;
 54     }
 55     if(x>9)o(x/10);
 56     p(x%10+'0');
 57 }
 58 
 59 void push(int x,int y,int v){
 60     node *p;
 61     p=new node();
 62     p->n=y;
 63     p->v=v;
 64     if(e[x]==NULL)
 65         e[x]=p;
 66     else{
 67         p->next=e[x]->next;
 68         e[x]->next=p;
 69     }
 70 }
 71 
 72 inline void spfa(int x){
 73     if(vis[x]||Flag){
 74         Flag=true;
 75         return;
 76     }
 77     vis[x]=true;
 78     for(node *i=e[x];i;i=i->next){
 79         if(Flag)
 80             return;
 81         if(d[i->n]>d[x]+i->v){
 82             d[i->n]=d[x]+i->v;
 83             spfa(i->n);
 84         }
 85     }    
 86     vis[x]=false;
 87 }
 88 
 89 int main(){
 90     in(n);in(m);
 91     For(i,1,m){
 92         in(flag);
 93         in(x);in(y);
 94         if(flag!=3)
 95         in(v);
 96         if(flag==1)
 97             push(x,y,-v);
 98         if(flag==2)
 99             push(y,x,v);
100         if(flag==3){
101             push(x,y,0);
102             push(y,x,0);
103         }
104     }
105     For(i,1,n)
106         push(0,i,0);
107     For(i,1,n)
108         d[i]=inf;
109     spfa(0);
110     if(!Flag)
111         cout<<"Yes";
112     else
113         cout<<"No";
114     return 0;
115 }
View Code

 

posted @ 2019-02-13 11:54  WeiAR  阅读(258)  评论(0编辑  收藏  举报