P1993 small K farm, p1993 farm
Description
Little K builds many farms in Minecraft, n in total, so that he forgets Every
The number of crops planted on the farm. He only remembers some vague information (m in total ).
Description:
However, because the memory of Xiao K is somewhat biased, he wants to know that there is no such situation
The number of crops is consistent with all the information in his memory.
Input/Output Format
Input Format:
Input data from farm. in
The first line contains two integers, n and m, indicating the number of farms and the number of small K memories.
Next, m rows:
If the first number of each row is 1, there are three integers a, B, c, indicating that farm a is planted more than farm B.
A crop of c units.
If the first number of each row is 2, there are three integers a, B, c, indicating that farm a is more planted than farm B.
A crop of c units. If the first number in each row is 3, there are two integers, a and B, indicating that farm a is terminated.
The number is the same as that of B.
Output Format:
Output to farm. out
If there is a situation that matches the memory of small K, "Yes" is output; otherwise, "No" is output ".
Input and Output sample input sample #1:
3 33 1 21 1 3 12 2 3 2
Output sample #1:
Yes
Description
100% data guarantee: 1 ≤ n, m, a, B, c ≤ 10000.
Remember to use deep search to write the difference constraint in the future, otherwise it is difficult to judge the negative ring.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 const int MAXN=100001; 9 const int maxn=0x7ffffff;10 void read(int &n)11 {12 char c='+';int x=0;bool flag=0;13 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}14 while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();}15 flag==1?n=-x:n=x;16 }17 int n,m;18 struct node19 {20 int u,v,w,nxt;21 }edge[MAXN];22 int head[MAXN];23 int num=1;24 void add_edge(int x,int y,int z)25 {26 edge[num].u=x;27 edge[num].v=y;28 edge[num].w=z;29 edge[num].nxt=head[x];30 head[x]=num++;31 }32 int dis[MAXN];33 int vis[MAXN];34 int happen[MAXN];35 bool SPFA(int bg)36 {37 vis[bg]=1;38 for(int i=head[bg];i!=-1;i=edge[i].nxt)39 {40 if(dis[edge[i].v]>dis[edge[i].u]+edge[i].w)41 {42 dis[edge[i].v]=dis[edge[i].u]+edge[i].w;43 if(vis[edge[i].v])44 return false;45 else if(!SPFA(edge[i].v))46 return false;47 }48 }49 vis[bg]=0;50 return true;51 }52 int main()53 {54 read(n);read(m);55 for(int i=0;i<=n;i++)56 head[i]=-1;57 for(int i=1;i<=m;i++)58 {59 int how,a,b,c;60 read(how);61 if(how==1)62 {63 read(a);read(b);read(c);64 add_edge(b,a,c);65 }66 else if(how==2)67 {68 read(a);read(b);read(c);69 add_edge(a,b,-c);70 }71 else if(how==3)72 {73 read(a);read(b);74 add_edge(a,b,0);75 add_edge(b,a,0);76 }77 }78 for(int i=0;i<=n;i++)79 dis[i]=maxn;80 dis[0]=0;81 for(int i=1;i<=n;i++)82 add_edge(0,i,1);83 if(SPFA(0))84 printf("Yes");85 else 86 printf("No");87 return 0;88 }