[Poj]1201——差分约束系统
[题目大意]
- 给定一个01数列上的一些关系,即从L到R的和至少C这样的关系,求满足所有条件的最小的1的个数
[分析题解]
- 差分约束系统
- 因为涉及到一段的和,所以转化为前缀和上的问题。
- 从L到R的和至少C -> S[R]-S[L-1]>=C -> S[L-1]-S[R]<=-C
0<=S[X]-S[X-1]<=1 -> S[X]-S[X-1]<=1 and S[X-1]-S[X]<=0
然后建图最短路之后输出S[Max of R]即可。
[个人代码]
SPFA
1 //10108083 perseawe 1201 Accepted 3928K 235MS Pascal 1777B 2012-04-22 22:03:18
2
3 Const
4 MaxNode=50000+10000;
5 MaxEdge=50000*10+10000;
6
7 Var
8 n,m,tot,Src,Max:Longint;
9 Edge:Array [0..MaxEdge] of Record c,wh,next:Longint;end;
10 hv,dis,Line:array [0..MaxNode] of Longint;
11 Use:Array [0..MaxNode] of Boolean;
12
13
14 Procedure AddEdge(u,v,c:Longint);
15 begin
16 inc(tot);
17 Edge[tot].next:=hv[u];hv[u]:=tot;
18 Edge[tot].wh:=v;Edge[tot].c:=c;
19 end;
20
21 Procedure Init;
22 var
23 i,l,r,c:Longint;
24 begin
25 readln(n);
26 Max:=0;
27 for i:=1 to n do
28 begin
29 readln(l,r,c);
30 if r>Max then Max:=r;
31 AddEdge(r,l-1,-c);
32 end;
33 for i:=1 to Max do
34 begin
35 AddEdge(i-1,i,1);
36 AddEdge(i,i-1,0);
37 end;
38 Src:=Max+1;
39 for i:=0 to Max do AddEdge(Src,i,0);
40 end;
41
42 Procedure Spfa;
43 var
44 I,top,tail,u,v,tnode:Longint;
45 begin
46 For I:=1 to Src-1 do Dis[I]:=1000000000;Dis[Src]:=0;
47 Fillchar(Use,sizeof(Use),False);Use[Src]:=True;
48 top:=0;tail:=1;line[1]:=Src;
49 Repeat
50 inc(top);
51 if top>MaxNode then top:=1;
52 u:=line[top];
53 Use[u]:=False;
54 tnode:=hv[u];
55 while tnode<>0 do
56 begin
57 v:=Edge[tnode].wh;
58 if Dis[v]>Dis[u]+Edge[tnode].c then
59 begin
60 Dis[v]:=Dis[u]+Edge[tnode].c;
61 if not(use[v]) then
62 begin
63 use[v]:=True;
64 inc(tail);
65 if tail>MaxNode then tail:=1;
66 Line[tail]:=v;
67 end;
68 end;
69 tnode:=Edge[tnode].next;
70 end;
71 Until top=tail;
72 end;
73
74 Procedure Main;
75 begin
76 Spfa;
77 end;
78
79 Procedure Print;
80 begin
81 writeln(dis[Max]-dis[0]);
82 end;
83
84 Begin
85 Init;
86 Main;
87 Print;
88 End.
2
3 Const
4 MaxNode=50000+10000;
5 MaxEdge=50000*10+10000;
6
7 Var
8 n,m,tot,Src,Max:Longint;
9 Edge:Array [0..MaxEdge] of Record c,wh,next:Longint;end;
10 hv,dis,Line:array [0..MaxNode] of Longint;
11 Use:Array [0..MaxNode] of Boolean;
12
13
14 Procedure AddEdge(u,v,c:Longint);
15 begin
16 inc(tot);
17 Edge[tot].next:=hv[u];hv[u]:=tot;
18 Edge[tot].wh:=v;Edge[tot].c:=c;
19 end;
20
21 Procedure Init;
22 var
23 i,l,r,c:Longint;
24 begin
25 readln(n);
26 Max:=0;
27 for i:=1 to n do
28 begin
29 readln(l,r,c);
30 if r>Max then Max:=r;
31 AddEdge(r,l-1,-c);
32 end;
33 for i:=1 to Max do
34 begin
35 AddEdge(i-1,i,1);
36 AddEdge(i,i-1,0);
37 end;
38 Src:=Max+1;
39 for i:=0 to Max do AddEdge(Src,i,0);
40 end;
41
42 Procedure Spfa;
43 var
44 I,top,tail,u,v,tnode:Longint;
45 begin
46 For I:=1 to Src-1 do Dis[I]:=1000000000;Dis[Src]:=0;
47 Fillchar(Use,sizeof(Use),False);Use[Src]:=True;
48 top:=0;tail:=1;line[1]:=Src;
49 Repeat
50 inc(top);
51 if top>MaxNode then top:=1;
52 u:=line[top];
53 Use[u]:=False;
54 tnode:=hv[u];
55 while tnode<>0 do
56 begin
57 v:=Edge[tnode].wh;
58 if Dis[v]>Dis[u]+Edge[tnode].c then
59 begin
60 Dis[v]:=Dis[u]+Edge[tnode].c;
61 if not(use[v]) then
62 begin
63 use[v]:=True;
64 inc(tail);
65 if tail>MaxNode then tail:=1;
66 Line[tail]:=v;
67 end;
68 end;
69 tnode:=Edge[tnode].next;
70 end;
71 Until top=tail;
72 end;
73
74 Procedure Main;
75 begin
76 Spfa;
77 end;
78
79 Procedure Print;
80 begin
81 writeln(dis[Max]-dis[0]);
82 end;
83
84 Begin
85 Init;
86 Main;
87 Print;
88 End.
[相关链接]
[启发总结]
——————————————————————————————————————
你说,我们的存在,永不消逝。对吧?
如果,我们都在努力创造了存在。我们,会幸福的。对吧?