POJ 1459

POJ 1459

题目大意:网络流。。

解:就是读入恶心死人,本来代码都没错的检查了老久, 最后发现是seekeof和eof的读入差距, 我总是读多一个0。

Seekeof:读到最后一个可见字符为止

Eof:读到最后一个字符为止。

View Code
  1 const
2 maxn=1111;
3 maxm=1111111 << 1;
4 bilibili=maxlongint >> 1;
5 legal = ['0'..'9', '(', ',', ')'];
6 type
7 data=record
8 cost, dest, next, op: longint;
9 end;
10 var
11 tot, st, ed, n, np, nc, m, ans: longint;
12 edge: array[1..maxm]of data;
13 vect, dist: array[0..maxn]of longint;
14 visit: array[0..maxn]of boolean;
15 procedure add(x, y, z: longint);
16 begin
17 inc(tot);
18 with edge[tot] do begin
19 dest := y;
20 cost := z;
21 next := vect[x];
22 vect[x] := tot;
23 op := tot + 1;
24 end;
25 inc(tot);
26 with edge[tot] do begin
27 dest := x;
28 cost := 0;
29 next := vect[y];
30 vect[y] := tot;
31 op := tot - 1;
32 end;
33 end;
34
35 procedure rea(var x, y, z: longint);
36 var
37 c: char;
38 tmp, st: string;
39 begin
40 read(c);
41 while c<>'(' do read(c);
42 st := ''; read(c);
43 while c<>',' do begin
44 if c in ['0'..'9'] then st := st + c;
45 read(c);
46 end;
47 val(st, x);
48 st := ''; read(c);
49 while c<>')' do begin
50 if c in ['0'..'9'] then st := st + c;
51 read(c);
52 end;
53 val(st, y);
54 read(z);
55 end;
56
57 procedure re(var x, z: longint);
58 var
59 c : char;
60 st : string;
61 begin
62 read(c);
63 while c<>'(' do read(c);
64 st := ''; read(c);
65 while c<>')' do begin
66 if c in ['0'..'9'] then st := st + c;
67 read(c);
68 end;
69 val(st, x);
70 read(z);
71 end;
72
73 function cin: string;
74 var
75 s: string;
76 c: char;
77 begin
78 read(c);
79 s := '';
80 repeat
81 if c in legal then s := s + c;
82 read(c);
83 until c=')';
84 exit(copy(s, 2, length(s) - 1));
85 end;
86
87 procedure init;
88 var
89 i, j, x, y, z: longint;
90 s: string;
91 begin
92 tot := 0; ans := 0;
93 fillchar(vect, sizeof(vect), 0);
94 read(n, np, nc, m);
95 st := n+1; ed := n+2;
96 for i := 1 to m do begin
97 rea(x, y, z);
98 add(x, y, z);
99 end;
100 for i := 1 to np do begin
101 re(x, z);
102 add(st, x, z);
103 end;
104 for i := 1 to nc do begin
105 re(x, z);
106 add(x, ed, z);
107 end;
108 end;
109
110 procedure print;
111 begin
112 writeln(ans);
113 end;
114
115 function bfs: boolean;
116 var
117 q: array[1..maxn]of longint;
118 head, tail, u, i: longint;
119 begin
120 filldword(dist, sizeof(dist)>> 2, bilibili);
121 fillchar(visit, sizeof(visit), 0);
122 tail := 1; head := 0;
123 q[tail] := st; dist[st] := 1;
124 repeat
125 head := head mod maxn +1;
126 u := q[head];
127 i := vect[u];
128 while i<>0 do
129 with edge[i] do begin
130 if (cost>0)and(dist[u] + 1 < dist[dest]) then begin
131 dist[dest] := dist[u] + 1;
132 tail := tail mod maxn +1;
133 q[tail] := dest;
134 end;
135 i := next;
136 end;
137 if dist[ed]<bilibili then break;
138 until head=tail;
139 exit(dist[ed]<>bilibili);
140 end;
141
142 function min(a, b: longint): longint;
143 begin if a<b then exit(a) else exit(b); end;
144
145 function dfs(u, flow: longint): longint;
146 var
147 i, tmp: longint;
148 begin
149 if u=ed then exit(flow);
150 dfs := 0; tmp := 0;
151 i := vect[u];
152 while i<>0 do
153 with edge[i] do begin
154 if (not visit[dest])and(cost>0)and(dist[u]+1=dist[dest]) then begin
155 tmp := dfs(dest, min(flow, cost));
156 cost := cost - tmp;
157 dfs := dfs + tmp;
158 flow := flow - tmp;
159 edge[op].cost := edge[op].cost + tmp;
160 if flow = 0 then break;
161 end;
162 i := next;
163 end;
164 if dfs = 0 then visit[u] := true;
165 end;
166
167 procedure main;
168 begin
169 while bfs do
170 ans := ans + dfs(st, maxlongint);
171 end;
172
173 begin
174 assign(input,'aaa.in'); reset(input);
175 // assign(output,'aaa.out'); rewrite(output);
176 while {not(eof)!!!!!}not seekeof do
177 begin
178 init;
179 main;
180 print;
181 end;
182 close(input); close(output);
183 end.



posted @ 2012-04-06 19:38  F.D.His.D  阅读(224)  评论(0编辑  收藏  举报