POJ 2983:is The information Reliable? (differential constraint)

Source: Internet
Author: User

The main topic: There are n points in a straight line, there are two types of relationship: P (x,y,v) means x at the north side of the V distance, V (x, y) means X at the north of the Y at least 1 distance out, give some of these relationships, to determine whether there is contradiction.

Analysis:

The differential constrained template problem, the constraint condition p:a-b>=v a-b<=v namely A-b>=v b-a<=-v,v:a-b>=1 namely B-a<=-1, the composition SPFA judgment negative right loop can.

It is important to note that a two-dimensional array can not be used for the storage of images, as there will be multiple edges between two points. 、

Spfa the method of judging the negative right loop, record the number of each point into the team, the number of points exceeds the existence of negative right loop.

Code:

 Programinfor;type Point=^node; Node=Recordx,len:longint; next:point; End;varA:Array[0.. +] ofPoint ; Q:Array[0..500000] ofLongint; Dis,vis:Array[0.. +] ofLongint; G:Array[0.. +] ofBoolean;  N,i,m,x,y,v:longint;  C:char; P:point;procedureput (x,y,v:longint);varP:point;beginnew (P); p^.x:=x; P^.len:=v; p^.next:=a[y];a[y]:=p;End;proceduregetnew;varI:longint;beginFillchar (q,sizeof (q),0); Fillchar (G,sizeof (g), false); Fillchar (vis,sizeof (VIS),0);  fori:=1  toN DoDis[i]:=maxlongintDiv 3;  fori:=0  toN Do beginDispose (A[i]); New (A[i]); a[i]:=Nil;End; dis[0]:=0; g[0]:=true; q[1]:=0;End;functionSpfa:boolean;varx,y,h,t:longint; p:point;beginh:=0; t:=1;  whileH<t Do   beginInc (H); x:=Q[H]; G[x]:=false; New (P);p: =A[x];  whileP<>Nil  Do      beginy:=p^.x; ifDis[x]+p^.len<dis[y] Then          beginDis[y]:=dis[x]+P^.len; ifG[y]=false Then              beginInc (T); Q[t]:=y; g[y]:=true; End; Inc (Vis[y]); ifVis[y]>n Thenexit (FALSE); End; P:=P^.next; End; End; Exit (true);End;beginreadln (n,m);  while(n<>0)or(m<>0) Do   begingetnew;  fori:=1  toN DoPut (I,0,0);  fori:=1  toM Do      beginread (c); ifC='P'  Then         beginRead (X,Y,V); Put (X,Y,V); Put (Y,X,-V);End        Else ifC='V'  Then  beginRead (x, y); Put (y,x,-1);End; ifI<m ThenReadln; End; ifSpfa ThenWriteln ('Reliable')ElseWriteln ('unreliable'); N:=0; m:=0;   READLN (N,M); End;End.
View Code

POJ 2983:is The information Reliable? (differential constraint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.