-
Notifications
You must be signed in to change notification settings - Fork 498
/
notebook.html
3549 lines (3178 loc) · 189 KB
/
notebook.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>Stanford ACM-ICPC Team Notebook</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#1F00FF" ALINK="#FF0000" VLINK="#9900DD">
<A NAME="top">
<CENTER><H1><U>Stanford ACM-ICPC Team Notebook</U></H1></CENTER>
<H1>Table of Contents</H1>
<H2>Combinatorial optimization</H2>
<OL START=1>
<LI><A HREF="#file1">Sparse max-flow</A></LI>
<LI><A HREF="#file2">Min-cost max-flow</A></LI>
<LI><A HREF="#file3">Push-relabel max-flow</A></LI>
<LI><A HREF="#file4">Min-cost matching</A></LI>
<LI><A HREF="#file5">Max bipartite matchine</A></LI>
<LI><A HREF="#file6">Global min-cut</A></LI>
<LI><A HREF="#file7">Graph cut inference</A></LI>
</OL>
<H2>Geometry</H2>
<OL START=8>
<LI><A HREF="#file8">Convex hull</A></LI>
<LI><A HREF="#file9">Miscellaneous geometry</A></LI>
<LI><A HREF="#file10">Java geometry</A></LI>
<LI><A HREF="#file11">3D geometry</A></LI>
<LI><A HREF="#file12">Slow Delaunay triangulation</A></LI>
</OL>
<H2>Numerical algorithms</H2>
<OL START=13>
<LI><A HREF="#file13">Number theory (modular, Chinese remainder, linear Diophantine)</A></LI>
<LI><A HREF="#file14">Systems of linear equations, matrix inverse, determinant</A></LI>
<LI><A HREF="#file15">Reduced row echelon form, matrix rank</A></LI>
<LI><A HREF="#file16">Fast Fourier transform</A></LI>
<LI><A HREF="#file17">Simplex algorithm</A></LI>
</OL>
<H2>Graph algorithms</H2>
<OL START=18>
<LI><A HREF="#file18">Fast Dijkstra's algorithm</A></LI>
<LI><A HREF="#file19">Strongly connected components</A></LI>
<LI><A HREF="#file20">Eulerian path</A></LI>
</OL>
<H2>Data structures</H2>
<OL START=21>
<LI><A HREF="#file21">Suffix array</A></LI>
<LI><A HREF="#file22">Binary Indexed Tree</A></LI>
<LI><A HREF="#file23">Union-find set</A></LI>
<LI><A HREF="#file24">KD-tree</A></LI>
<LI><A HREF="#file25">Splay tree</A></LI>
<LI><A HREF="#file26">Lazy segment tree</A></LI>
<LI><A HREF="#file27">Lowest common ancestor</A></LI>
</OL>
<H2>Miscellaneous</H2>
<OL START=28>
<LI><A HREF="#file28">Longest increasing subsequence</A></LI>
<LI><A HREF="#file29">Dates</A></LI>
<LI><A HREF="#file30">Regular expressions</A></LI>
<LI><A HREF="#file31">Prime numbers</A></LI>
<LI><A HREF="#file32">C++ input/output</A></LI>
<LI><A HREF="#file33">Knuth-Morris-Pratt</A></LI>
<LI><A HREF="#file34">Latitude/longitude</A></LI>
<LI><A HREF="#file35">Emacs settings</A></LI>
</OL>
<HR>
<A NAME="file1">
<H1>code/Dinic.cc 1/35</H1>
[<A HREF="#top">top</A>][prev][<A HREF="#file2">next</A>]
<PRE>
<I><FONT COLOR="#B22222">// Adjacency list implementation of Dinic's blocking flow algorithm.
</FONT></I><I><FONT COLOR="#B22222">// This is very fast in practice, and only loses to push-relabel flow.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// Running time:
</FONT></I><I><FONT COLOR="#B22222">// O(|V|^2 |E|)
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// INPUT:
</FONT></I><I><FONT COLOR="#B22222">// - graph, constructed using AddEdge()
</FONT></I><I><FONT COLOR="#B22222">// - source and sink
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// OUTPUT:
</FONT></I><I><FONT COLOR="#B22222">// - maximum flow value
</FONT></I><I><FONT COLOR="#B22222">// - To obtain actual flow values, look at edges with capacity > 0
</FONT></I><I><FONT COLOR="#B22222">// (zero capacity edges are residual edges).
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include<cstdio></FONT></B>
#<B><FONT COLOR="#5F9EA0">include<vector></FONT></B>
#<B><FONT COLOR="#5F9EA0">include<queue></FONT></B>
using namespace std;
<B><FONT COLOR="#228B22">typedef</FONT></B> <B><FONT COLOR="#228B22">long</FONT></B> <B><FONT COLOR="#228B22">long</FONT></B> LL;
<B><FONT COLOR="#228B22">struct</FONT></B> Edge {
<B><FONT COLOR="#228B22">int</FONT></B> u, v;
LL cap, flow;
Edge() {}
Edge(<B><FONT COLOR="#228B22">int</FONT></B> u, <B><FONT COLOR="#228B22">int</FONT></B> v, LL cap): u(u), v(v), cap(cap), flow(0) {}
};
<B><FONT COLOR="#228B22">struct</FONT></B> Dinic {
<B><FONT COLOR="#228B22">int</FONT></B> N;
vector<Edge> E;
vector<vector<<B><FONT COLOR="#228B22">int</FONT></B>>> g;
vector<<B><FONT COLOR="#228B22">int</FONT></B>> d, pt;
Dinic(<B><FONT COLOR="#228B22">int</FONT></B> N): N(N), E(0), g(N), d(N), pt(N) {}
<B><FONT COLOR="#228B22">void</FONT></B> AddEdge(<B><FONT COLOR="#228B22">int</FONT></B> u, <B><FONT COLOR="#228B22">int</FONT></B> v, LL cap) {
<B><FONT COLOR="#A020F0">if</FONT></B> (u != v) {
E.emplace_back(Edge(u, v, cap));
g[u].emplace_back(E.size() - 1);
E.emplace_back(Edge(v, u, 0));
g[v].emplace_back(E.size() - 1);
}
}
<B><FONT COLOR="#228B22">bool</FONT></B> BFS(<B><FONT COLOR="#228B22">int</FONT></B> S, <B><FONT COLOR="#228B22">int</FONT></B> T) {
queue<<B><FONT COLOR="#228B22">int</FONT></B>> q({S});
fill(d.begin(), d.end(), N + 1);
d[S] = 0;
<B><FONT COLOR="#A020F0">while</FONT></B>(!q.empty()) {
<B><FONT COLOR="#228B22">int</FONT></B> u = q.front(); q.pop();
<B><FONT COLOR="#A020F0">if</FONT></B> (u == T) <B><FONT COLOR="#A020F0">break</FONT></B>;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k: g[u]) {
Edge &e = E[k];
<B><FONT COLOR="#A020F0">if</FONT></B> (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
d[e.v] = d[e.u] + 1;
q.emplace(e.v);
}
}
}
<B><FONT COLOR="#A020F0">return</FONT></B> d[T] != N + 1;
}
LL DFS(<B><FONT COLOR="#228B22">int</FONT></B> u, <B><FONT COLOR="#228B22">int</FONT></B> T, LL flow = -1) {
<B><FONT COLOR="#A020F0">if</FONT></B> (u == T || flow == 0) <B><FONT COLOR="#A020F0">return</FONT></B> flow;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> &i = pt[u]; i < g[u].size(); ++i) {
Edge &e = E[g[u][i]];
Edge &oe = E[g[u][i]^1];
<B><FONT COLOR="#A020F0">if</FONT></B> (d[e.v] == d[e.u] + 1) {
LL amt = e.cap - e.flow;
<B><FONT COLOR="#A020F0">if</FONT></B> (flow != -1 && amt > flow) amt = flow;
<B><FONT COLOR="#A020F0">if</FONT></B> (LL pushed = DFS(e.v, T, amt)) {
e.flow += pushed;
oe.flow -= pushed;
<B><FONT COLOR="#A020F0">return</FONT></B> pushed;
}
}
}
<B><FONT COLOR="#A020F0">return</FONT></B> 0;
}
LL MaxFlow(<B><FONT COLOR="#228B22">int</FONT></B> S, <B><FONT COLOR="#228B22">int</FONT></B> T) {
LL total = 0;
<B><FONT COLOR="#A020F0">while</FONT></B> (BFS(S, T)) {
fill(pt.begin(), pt.end(), 0);
<B><FONT COLOR="#A020F0">while</FONT></B> (LL flow = DFS(S, T))
total += flow;
}
<B><FONT COLOR="#A020F0">return</FONT></B> total;
}
};
<I><FONT COLOR="#B22222">// BEGIN CUT
</FONT></I><I><FONT COLOR="#B22222">// The following code solves SPOJ problem #4110: Fast Maximum Flow (FASTFLOW)
</FONT></I>
<B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#0000FF">main</FONT></B>()
{
<B><FONT COLOR="#228B22">int</FONT></B> N, E;
scanf(<B><FONT COLOR="#BC8F8F">"%d%d"</FONT></B>, &N, &E);
Dinic dinic(N);
<B><FONT COLOR="#A020F0">for</FONT></B>(<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < E; i++)
{
<B><FONT COLOR="#228B22">int</FONT></B> u, v;
LL cap;
scanf(<B><FONT COLOR="#BC8F8F">"%d%d%lld"</FONT></B>, &u, &v, &cap);
dinic.AddEdge(u - 1, v - 1, cap);
dinic.AddEdge(v - 1, u - 1, cap);
}
printf(<B><FONT COLOR="#BC8F8F">"%lld\n"</FONT></B>, dinic.MaxFlow(0, N - 1));
<B><FONT COLOR="#A020F0">return</FONT></B> 0;
}
<I><FONT COLOR="#B22222">// END CUT
</FONT></I></PRE>
<HR>
<A NAME="file2">
<H1>code/MinCostMaxFlow.cc 2/35</H1>
[<A HREF="#top">top</A>][<A HREF="#file1">prev</A>][<A HREF="#file3">next</A>]
<PRE>
<I><FONT COLOR="#B22222">// Implementation of min cost max flow algorithm using adjacency
</FONT></I><I><FONT COLOR="#B22222">// matrix (Edmonds and Karp 1972). This implementation keeps track of
</FONT></I><I><FONT COLOR="#B22222">// forward and reverse edges separately (so you can set cap[i][j] !=
</FONT></I><I><FONT COLOR="#B22222">// cap[j][i]). For a regular max flow, set all edge costs to 0.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// Running time, O(|V|^2) cost per augmentation
</FONT></I><I><FONT COLOR="#B22222">// max flow: O(|V|^3) augmentations
</FONT></I><I><FONT COLOR="#B22222">// min cost max flow: O(|V|^4 * MAX_EDGE_COST) augmentations
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// INPUT:
</FONT></I><I><FONT COLOR="#B22222">// - graph, constructed using AddEdge()
</FONT></I><I><FONT COLOR="#B22222">// - source
</FONT></I><I><FONT COLOR="#B22222">// - sink
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// OUTPUT:
</FONT></I><I><FONT COLOR="#B22222">// - (maximum flow value, minimum cost value)
</FONT></I><I><FONT COLOR="#B22222">// - To obtain the actual flow, look at positive values only.
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cmath></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><vector></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><iostream></FONT></B>
using namespace std;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<<B><FONT COLOR="#228B22">int</FONT></B>> VI;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VI> VVI;
<B><FONT COLOR="#228B22">typedef</FONT></B> <B><FONT COLOR="#228B22">long</FONT></B> <B><FONT COLOR="#228B22">long</FONT></B> L;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<L> VL;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VL> VVL;
<B><FONT COLOR="#228B22">typedef</FONT></B> pair<<B><FONT COLOR="#228B22">int</FONT></B>, <B><FONT COLOR="#228B22">int</FONT></B>> PII;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<PII> VPII;
<B><FONT COLOR="#228B22">const</FONT></B> L INF = numeric_limits<L>::max() / 4;
<B><FONT COLOR="#228B22">struct</FONT></B> MinCostMaxFlow {
<B><FONT COLOR="#228B22">int</FONT></B> N;
VVL cap, flow, cost;
VI found;
VL dist, pi, width;
VPII dad;
MinCostMaxFlow(<B><FONT COLOR="#228B22">int</FONT></B> N) :
N(N), cap(N, VL(N)), flow(N, VL(N)), cost(N, VL(N)),
found(N), dist(N), pi(N), width(N), dad(N) {}
<B><FONT COLOR="#228B22">void</FONT></B> AddEdge(<B><FONT COLOR="#228B22">int</FONT></B> from, <B><FONT COLOR="#228B22">int</FONT></B> to, L cap, L cost) {
<B><FONT COLOR="#A020F0">this</FONT></B>->cap[from][to] = cap;
<B><FONT COLOR="#A020F0">this</FONT></B>->cost[from][to] = cost;
}
<B><FONT COLOR="#228B22">void</FONT></B> Relax(<B><FONT COLOR="#228B22">int</FONT></B> s, <B><FONT COLOR="#228B22">int</FONT></B> k, L cap, L cost, <B><FONT COLOR="#228B22">int</FONT></B> dir) {
L val = dist[s] + pi[s] - pi[k] + cost;
<B><FONT COLOR="#A020F0">if</FONT></B> (cap && val < dist[k]) {
dist[k] = val;
dad[k] = make_pair(s, dir);
width[k] = min(cap, width[s]);
}
}
L Dijkstra(<B><FONT COLOR="#228B22">int</FONT></B> s, <B><FONT COLOR="#228B22">int</FONT></B> t) {
fill(found.begin(), found.end(), false);
fill(dist.begin(), dist.end(), INF);
fill(width.begin(), width.end(), 0);
dist[s] = 0;
width[s] = INF;
<B><FONT COLOR="#A020F0">while</FONT></B> (s != -1) {
<B><FONT COLOR="#228B22">int</FONT></B> best = -1;
found[s] = true;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k = 0; k < N; k++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (found[k]) <B><FONT COLOR="#A020F0">continue</FONT></B>;
Relax(s, k, cap[s][k] - flow[s][k], cost[s][k], 1);
Relax(s, k, flow[k][s], -cost[k][s], -1);
<B><FONT COLOR="#A020F0">if</FONT></B> (best == -1 || dist[k] < dist[best]) best = k;
}
s = best;
}
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k = 0; k < N; k++)
pi[k] = min(pi[k] + dist[k], INF);
<B><FONT COLOR="#A020F0">return</FONT></B> width[t];
}
pair<L, L> GetMaxFlow(<B><FONT COLOR="#228B22">int</FONT></B> s, <B><FONT COLOR="#228B22">int</FONT></B> t) {
L totflow = 0, totcost = 0;
<B><FONT COLOR="#A020F0">while</FONT></B> (L amt = Dijkstra(s, t)) {
totflow += amt;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> x = t; x != s; x = dad[x].first) {
<B><FONT COLOR="#A020F0">if</FONT></B> (dad[x].second == 1) {
flow[dad[x].first][x] += amt;
totcost += amt * cost[dad[x].first][x];
} <B><FONT COLOR="#A020F0">else</FONT></B> {
flow[x][dad[x].first] -= amt;
totcost -= amt * cost[x][dad[x].first];
}
}
}
<B><FONT COLOR="#A020F0">return</FONT></B> make_pair(totflow, totcost);
}
};
<I><FONT COLOR="#B22222">// BEGIN CUT
</FONT></I><I><FONT COLOR="#B22222">// The following code solves UVA problem #10594: Data Flow
</FONT></I>
<B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#0000FF">main</FONT></B>() {
<B><FONT COLOR="#228B22">int</FONT></B> N, M;
<B><FONT COLOR="#A020F0">while</FONT></B> (scanf(<B><FONT COLOR="#BC8F8F">"%d%d"</FONT></B>, &N, &M) == 2) {
VVL v(M, VL(3));
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < M; i++)
scanf(<B><FONT COLOR="#BC8F8F">"%Ld%Ld%Ld"</FONT></B>, &v[i][0], &v[i][1], &v[i][2]);
L D, K;
scanf(<B><FONT COLOR="#BC8F8F">"%Ld%Ld"</FONT></B>, &D, &K);
MinCostMaxFlow mcmf(N+1);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < M; i++) {
mcmf.AddEdge(<B><FONT COLOR="#228B22">int</FONT></B>(v[i][0]), <B><FONT COLOR="#228B22">int</FONT></B>(v[i][1]), K, v[i][2]);
mcmf.AddEdge(<B><FONT COLOR="#228B22">int</FONT></B>(v[i][1]), <B><FONT COLOR="#228B22">int</FONT></B>(v[i][0]), K, v[i][2]);
}
mcmf.AddEdge(0, 1, D, 0);
pair<L, L> res = mcmf.GetMaxFlow(0, N);
<B><FONT COLOR="#A020F0">if</FONT></B> (res.first == D) {
printf(<B><FONT COLOR="#BC8F8F">"%Ld\n"</FONT></B>, res.second);
} <B><FONT COLOR="#A020F0">else</FONT></B> {
printf(<B><FONT COLOR="#BC8F8F">"Impossible.\n"</FONT></B>);
}
}
<B><FONT COLOR="#A020F0">return</FONT></B> 0;
}
<I><FONT COLOR="#B22222">// END CUT
</FONT></I></PRE>
<HR>
<A NAME="file3">
<H1>code/PushRelabel.cc 3/35</H1>
[<A HREF="#top">top</A>][<A HREF="#file2">prev</A>][<A HREF="#file4">next</A>]
<PRE>
<I><FONT COLOR="#B22222">// Adjacency list implementation of FIFO push relabel maximum flow
</FONT></I><I><FONT COLOR="#B22222">// with the gap relabeling heuristic. This implementation is
</FONT></I><I><FONT COLOR="#B22222">// significantly faster than straight Ford-Fulkerson. It solves
</FONT></I><I><FONT COLOR="#B22222">// random problems with 10000 vertices and 1000000 edges in a few
</FONT></I><I><FONT COLOR="#B22222">// seconds, though it is possible to construct test cases that
</FONT></I><I><FONT COLOR="#B22222">// achieve the worst-case.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// Running time:
</FONT></I><I><FONT COLOR="#B22222">// O(|V|^3)
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// INPUT:
</FONT></I><I><FONT COLOR="#B22222">// - graph, constructed using AddEdge()
</FONT></I><I><FONT COLOR="#B22222">// - source
</FONT></I><I><FONT COLOR="#B22222">// - sink
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// OUTPUT:
</FONT></I><I><FONT COLOR="#B22222">// - maximum flow value
</FONT></I><I><FONT COLOR="#B22222">// - To obtain the actual flow values, look at all edges with
</FONT></I><I><FONT COLOR="#B22222">// capacity > 0 (zero capacity edges are residual edges).
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cmath></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><vector></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><iostream></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><queue></FONT></B>
using namespace std;
<B><FONT COLOR="#228B22">typedef</FONT></B> <B><FONT COLOR="#228B22">long</FONT></B> <B><FONT COLOR="#228B22">long</FONT></B> LL;
<B><FONT COLOR="#228B22">struct</FONT></B> Edge {
<B><FONT COLOR="#228B22">int</FONT></B> from, to, cap, flow, index;
Edge(<B><FONT COLOR="#228B22">int</FONT></B> from, <B><FONT COLOR="#228B22">int</FONT></B> to, <B><FONT COLOR="#228B22">int</FONT></B> cap, <B><FONT COLOR="#228B22">int</FONT></B> flow, <B><FONT COLOR="#228B22">int</FONT></B> index) :
from(from), to(to), cap(cap), flow(flow), index(index) {}
};
<B><FONT COLOR="#228B22">struct</FONT></B> PushRelabel {
<B><FONT COLOR="#228B22">int</FONT></B> N;
vector<vector<Edge> > G;
vector<LL> excess;
vector<<B><FONT COLOR="#228B22">int</FONT></B>> dist, active, count;
queue<<B><FONT COLOR="#228B22">int</FONT></B>> Q;
PushRelabel(<B><FONT COLOR="#228B22">int</FONT></B> N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}
<B><FONT COLOR="#228B22">void</FONT></B> AddEdge(<B><FONT COLOR="#228B22">int</FONT></B> from, <B><FONT COLOR="#228B22">int</FONT></B> to, <B><FONT COLOR="#228B22">int</FONT></B> cap) {
G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
<B><FONT COLOR="#A020F0">if</FONT></B> (from == to) G[from].back().index++;
G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
}
<B><FONT COLOR="#228B22">void</FONT></B> Enqueue(<B><FONT COLOR="#228B22">int</FONT></B> v) {
<B><FONT COLOR="#A020F0">if</FONT></B> (!active[v] && excess[v] > 0) { active[v] = true; Q.push(v); }
}
<B><FONT COLOR="#228B22">void</FONT></B> Push(Edge &e) {
<B><FONT COLOR="#228B22">int</FONT></B> amt = <B><FONT COLOR="#228B22">int</FONT></B>(min(excess[e.from], LL(e.cap - e.flow)));
<B><FONT COLOR="#A020F0">if</FONT></B> (dist[e.from] <= dist[e.to] || amt == 0) <B><FONT COLOR="#A020F0">return</FONT></B>;
e.flow += amt;
G[e.to][e.index].flow -= amt;
excess[e.to] += amt;
excess[e.from] -= amt;
Enqueue(e.to);
}
<B><FONT COLOR="#228B22">void</FONT></B> Gap(<B><FONT COLOR="#228B22">int</FONT></B> k) {
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> v = 0; v < N; v++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (dist[v] < k) <B><FONT COLOR="#A020F0">continue</FONT></B>;
count[dist[v]]--;
dist[v] = max(dist[v], N+1);
count[dist[v]]++;
Enqueue(v);
}
}
<B><FONT COLOR="#228B22">void</FONT></B> Relabel(<B><FONT COLOR="#228B22">int</FONT></B> v) {
count[dist[v]]--;
dist[v] = 2*N;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < G[v].size(); i++)
<B><FONT COLOR="#A020F0">if</FONT></B> (G[v][i].cap - G[v][i].flow > 0)
dist[v] = min(dist[v], dist[G[v][i].to] + 1);
count[dist[v]]++;
Enqueue(v);
}
<B><FONT COLOR="#228B22">void</FONT></B> Discharge(<B><FONT COLOR="#228B22">int</FONT></B> v) {
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; excess[v] > 0 && i < G[v].size(); i++) Push(G[v][i]);
<B><FONT COLOR="#A020F0">if</FONT></B> (excess[v] > 0) {
<B><FONT COLOR="#A020F0">if</FONT></B> (count[dist[v]] == 1)
Gap(dist[v]);
<B><FONT COLOR="#A020F0">else</FONT></B>
Relabel(v);
}
}
LL GetMaxFlow(<B><FONT COLOR="#228B22">int</FONT></B> s, <B><FONT COLOR="#228B22">int</FONT></B> t) {
count[0] = N-1;
count[N] = 1;
dist[s] = N;
active[s] = active[t] = true;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < G[s].size(); i++) {
excess[s] += G[s][i].cap;
Push(G[s][i]);
}
<B><FONT COLOR="#A020F0">while</FONT></B> (!Q.empty()) {
<B><FONT COLOR="#228B22">int</FONT></B> v = Q.front();
Q.pop();
active[v] = false;
Discharge(v);
}
LL totflow = 0;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;
<B><FONT COLOR="#A020F0">return</FONT></B> totflow;
}
};
<I><FONT COLOR="#B22222">// BEGIN CUT
</FONT></I><I><FONT COLOR="#B22222">// The following code solves SPOJ problem #4110: Fast Maximum Flow (FASTFLOW)
</FONT></I>
<B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#0000FF">main</FONT></B>() {
<B><FONT COLOR="#228B22">int</FONT></B> n, m;
scanf(<B><FONT COLOR="#BC8F8F">"%d%d"</FONT></B>, &n, &m);
PushRelabel pr(n);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < m; i++) {
<B><FONT COLOR="#228B22">int</FONT></B> a, b, c;
scanf(<B><FONT COLOR="#BC8F8F">"%d%d%d"</FONT></B>, &a, &b, &c);
<B><FONT COLOR="#A020F0">if</FONT></B> (a == b) <B><FONT COLOR="#A020F0">continue</FONT></B>;
pr.AddEdge(a-1, b-1, c);
pr.AddEdge(b-1, a-1, c);
}
printf(<B><FONT COLOR="#BC8F8F">"%Ld\n"</FONT></B>, pr.GetMaxFlow(0, n-1));
<B><FONT COLOR="#A020F0">return</FONT></B> 0;
}
<I><FONT COLOR="#B22222">// END CUT
</FONT></I></PRE>
<HR>
<A NAME="file4">
<H1>code/MinCostMatching.cc 4/35</H1>
[<A HREF="#top">top</A>][<A HREF="#file3">prev</A>][<A HREF="#file5">next</A>]
<PRE>
<I><FONT COLOR="#B22222">//////////////////////////////////////////////////////////////////////
</FONT></I><I><FONT COLOR="#B22222">// Min cost bipartite matching via shortest augmenting paths
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// This is an O(n^3) implementation of a shortest augmenting path
</FONT></I><I><FONT COLOR="#B22222">// algorithm for finding min cost perfect matchings in dense
</FONT></I><I><FONT COLOR="#B22222">// graphs. In practice, it solves 1000x1000 problems in around 1
</FONT></I><I><FONT COLOR="#B22222">// second.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// cost[i][j] = cost for pairing left node i with right node j
</FONT></I><I><FONT COLOR="#B22222">// Lmate[i] = index of right node that left node i pairs with
</FONT></I><I><FONT COLOR="#B22222">// Rmate[j] = index of left node that right node j pairs with
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// The values in cost[i][j] may be positive or negative. To perform
</FONT></I><I><FONT COLOR="#B22222">// maximization, simply negate the cost[][] matrix.
</FONT></I><I><FONT COLOR="#B22222">//////////////////////////////////////////////////////////////////////
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><algorithm></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cstdio></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cmath></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><vector></FONT></B>
using namespace std;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<<B><FONT COLOR="#228B22">double</FONT></B>> VD;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VD> VVD;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<<B><FONT COLOR="#228B22">int</FONT></B>> VI;
<B><FONT COLOR="#228B22">double</FONT></B> <B><FONT COLOR="#0000FF">MinCostMatching</FONT></B>(<B><FONT COLOR="#228B22">const</FONT></B> VVD &cost, VI &Lmate, VI &Rmate) {
<B><FONT COLOR="#228B22">int</FONT></B> n = <B><FONT COLOR="#228B22">int</FONT></B>(cost.size());
<I><FONT COLOR="#B22222">// construct dual feasible solution
</FONT></I> VD u(n);
VD v(n);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < n; i++) {
u[i] = cost[i][0];
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 1; j < n; j++) u[i] = min(u[i], cost[i][j]);
}
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < n; j++) {
v[j] = cost[0][j] - u[0];
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 1; i < n; i++) v[j] = min(v[j], cost[i][j] - u[i]);
}
<I><FONT COLOR="#B22222">// construct primal solution satisfying complementary slackness
</FONT></I> Lmate = VI(n, -1);
Rmate = VI(n, -1);
<B><FONT COLOR="#228B22">int</FONT></B> mated = 0;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < n; i++) {
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < n; j++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (Rmate[j] != -1) <B><FONT COLOR="#A020F0">continue</FONT></B>;
<B><FONT COLOR="#A020F0">if</FONT></B> (fabs(cost[i][j] - u[i] - v[j]) < 1e-10) {
Lmate[i] = j;
Rmate[j] = i;
mated++;
<B><FONT COLOR="#A020F0">break</FONT></B>;
}
}
}
VD dist(n);
VI dad(n);
VI seen(n);
<I><FONT COLOR="#B22222">// repeat until primal solution is feasible
</FONT></I> <B><FONT COLOR="#A020F0">while</FONT></B> (mated < n) {
<I><FONT COLOR="#B22222">// find an unmatched left node
</FONT></I> <B><FONT COLOR="#228B22">int</FONT></B> s = 0;
<B><FONT COLOR="#A020F0">while</FONT></B> (Lmate[s] != -1) s++;
<I><FONT COLOR="#B22222">// initialize Dijkstra
</FONT></I> fill(dad.begin(), dad.end(), -1);
fill(seen.begin(), seen.end(), 0);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k = 0; k < n; k++)
dist[k] = cost[s][k] - u[s] - v[k];
<B><FONT COLOR="#228B22">int</FONT></B> j = 0;
<B><FONT COLOR="#A020F0">while</FONT></B> (true) {
<I><FONT COLOR="#B22222">// find closest
</FONT></I> j = -1;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k = 0; k < n; k++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (seen[k]) <B><FONT COLOR="#A020F0">continue</FONT></B>;
<B><FONT COLOR="#A020F0">if</FONT></B> (j == -1 || dist[k] < dist[j]) j = k;
}
seen[j] = 1;
<I><FONT COLOR="#B22222">// termination condition
</FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> (Rmate[j] == -1) <B><FONT COLOR="#A020F0">break</FONT></B>;
<I><FONT COLOR="#B22222">// relax neighbors
</FONT></I> <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> i = Rmate[j];
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k = 0; k < n; k++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (seen[k]) <B><FONT COLOR="#A020F0">continue</FONT></B>;
<B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">double</FONT></B> new_dist = dist[j] + cost[i][k] - u[i] - v[k];
<B><FONT COLOR="#A020F0">if</FONT></B> (dist[k] > new_dist) {
dist[k] = new_dist;
dad[k] = j;
}
}
}
<I><FONT COLOR="#B22222">// update dual variables
</FONT></I> <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k = 0; k < n; k++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (k == j || !seen[k]) <B><FONT COLOR="#A020F0">continue</FONT></B>;
<B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> i = Rmate[k];
v[k] += dist[k] - dist[j];
u[i] -= dist[k] - dist[j];
}
u[s] += dist[j];
<I><FONT COLOR="#B22222">// augment along path
</FONT></I> <B><FONT COLOR="#A020F0">while</FONT></B> (dad[j] >= 0) {
<B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> d = dad[j];
Rmate[j] = Rmate[d];
Lmate[Rmate[j]] = j;
j = d;
}
Rmate[j] = s;
Lmate[s] = j;
mated++;
}
<B><FONT COLOR="#228B22">double</FONT></B> value = 0;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < n; i++)
value += cost[i][Lmate[i]];
<B><FONT COLOR="#A020F0">return</FONT></B> value;
}
</PRE>
<HR>
<A NAME="file5">
<H1>code/MaxBipartiteMatching.cc 5/35</H1>
[<A HREF="#top">top</A>][<A HREF="#file4">prev</A>][<A HREF="#file6">next</A>]
<PRE>
<I><FONT COLOR="#B22222">// This code performs maximum bipartite matching.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// Running time: O(|E| |V|) -- often much faster in practice
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// INPUT: w[i][j] = edge between row node i and column node j
</FONT></I><I><FONT COLOR="#B22222">// OUTPUT: mr[i] = assignment for row node i, -1 if unassigned
</FONT></I><I><FONT COLOR="#B22222">// mc[j] = assignment for column node j, -1 if unassigned
</FONT></I><I><FONT COLOR="#B22222">// function returns number of matches made
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><vector></FONT></B>
using namespace std;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<<B><FONT COLOR="#228B22">int</FONT></B>> VI;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VI> VVI;
<B><FONT COLOR="#228B22">bool</FONT></B> <B><FONT COLOR="#0000FF">FindMatch</FONT></B>(<B><FONT COLOR="#228B22">int</FONT></B> i, <B><FONT COLOR="#228B22">const</FONT></B> VVI &w, VI &mr, VI &mc, VI &seen) {
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < w[i].size(); j++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (w[i][j] && !seen[j]) {
seen[j] = true;
<B><FONT COLOR="#A020F0">if</FONT></B> (mc[j] < 0 || FindMatch(mc[j], w, mr, mc, seen)) {
mr[i] = j;
mc[j] = i;
<B><FONT COLOR="#A020F0">return</FONT></B> true;
}
}
}
<B><FONT COLOR="#A020F0">return</FONT></B> false;
}
<B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#0000FF">BipartiteMatching</FONT></B>(<B><FONT COLOR="#228B22">const</FONT></B> VVI &w, VI &mr, VI &mc) {
mr = VI(w.size(), -1);
mc = VI(w[0].size(), -1);
<B><FONT COLOR="#228B22">int</FONT></B> ct = 0;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < w.size(); i++) {
VI seen(w[0].size());
<B><FONT COLOR="#A020F0">if</FONT></B> (FindMatch(i, w, mr, mc, seen)) ct++;
}
<B><FONT COLOR="#A020F0">return</FONT></B> ct;
}
</PRE>
<HR>
<A NAME="file6">
<H1>code/MinCut.cc 6/35</H1>
[<A HREF="#top">top</A>][<A HREF="#file5">prev</A>][<A HREF="#file7">next</A>]
<PRE>
<I><FONT COLOR="#B22222">// Adjacency matrix implementation of Stoer-Wagner min cut algorithm.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// Running time:
</FONT></I><I><FONT COLOR="#B22222">// O(|V|^3)
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// INPUT:
</FONT></I><I><FONT COLOR="#B22222">// - graph, constructed using AddEdge()
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// OUTPUT:
</FONT></I><I><FONT COLOR="#B22222">// - (min cut value, nodes in half of min cut)
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cmath></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><vector></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><iostream></FONT></B>
using namespace std;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<<B><FONT COLOR="#228B22">int</FONT></B>> VI;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VI> VVI;
<B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> INF = 1000000000;
pair<<B><FONT COLOR="#228B22">int</FONT></B>, VI> GetMinCut(VVI &weights) {
<B><FONT COLOR="#228B22">int</FONT></B> N = weights.size();
VI used(N), cut, best_cut;
<B><FONT COLOR="#228B22">int</FONT></B> best_weight = -1;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> phase = N-1; phase >= 0; phase--) {
VI w = weights[0];
VI added = used;
<B><FONT COLOR="#228B22">int</FONT></B> prev, last = 0;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < phase; i++) {
prev = last;
last = -1;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 1; j < N; j++)
<B><FONT COLOR="#A020F0">if</FONT></B> (!added[j] && (last == -1 || w[j] > w[last])) last = j;
<B><FONT COLOR="#A020F0">if</FONT></B> (i == phase-1) {
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < N; j++) weights[prev][j] += weights[last][j];
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < N; j++) weights[j][prev] = weights[prev][j];
used[last] = true;
cut.push_back(last);
<B><FONT COLOR="#A020F0">if</FONT></B> (best_weight == -1 || w[last] < best_weight) {
best_cut = cut;
best_weight = w[last];
}
} <B><FONT COLOR="#A020F0">else</FONT></B> {
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < N; j++)
w[j] += weights[last][j];
added[last] = true;
}
}
}
<B><FONT COLOR="#A020F0">return</FONT></B> make_pair(best_weight, best_cut);
}
<I><FONT COLOR="#B22222">// BEGIN CUT
</FONT></I><I><FONT COLOR="#B22222">// The following code solves UVA problem #10989: Bomb, Divide and Conquer
</FONT></I><B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#0000FF">main</FONT></B>() {
<B><FONT COLOR="#228B22">int</FONT></B> N;
cin >> N;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < N; i++) {
<B><FONT COLOR="#228B22">int</FONT></B> n, m;
cin >> n >> m;
VVI weights(n, VI(n));
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < m; j++) {
<B><FONT COLOR="#228B22">int</FONT></B> a, b, c;
cin >> a >> b >> c;
weights[a-1][b-1] = weights[b-1][a-1] = c;
}
pair<<B><FONT COLOR="#228B22">int</FONT></B>, VI> res = GetMinCut(weights);
cout << <B><FONT COLOR="#BC8F8F">"Case #"</FONT></B> << i+1 << <B><FONT COLOR="#BC8F8F">": "</FONT></B> << res.first << endl;
}
}
<I><FONT COLOR="#B22222">// END CUT
</FONT></I></PRE>
<HR>
<A NAME="file7">
<H1>code/GraphCutInference.cc 7/35</H1>
[<A HREF="#top">top</A>][<A HREF="#file6">prev</A>][<A HREF="#file8">next</A>]
<PRE>
<I><FONT COLOR="#B22222">// Special-purpose {0,1} combinatorial optimization solver for
</FONT></I><I><FONT COLOR="#B22222">// problems of the following by a reduction to graph cuts:
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// minimize sum_i psi_i(x[i])
</FONT></I><I><FONT COLOR="#B22222">// x[1]...x[n] in {0,1} + sum_{i < j} phi_{ij}(x[i], x[j])
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// where
</FONT></I><I><FONT COLOR="#B22222">// psi_i : {0, 1} --> R
</FONT></I><I><FONT COLOR="#B22222">// phi_{ij} : {0, 1} x {0, 1} --> R
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// such that
</FONT></I><I><FONT COLOR="#B22222">// phi_{ij}(0,0) + phi_{ij}(1,1) <= phi_{ij}(0,1) + phi_{ij}(1,0) (*)
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// This can also be used to solve maximization problems where the
</FONT></I><I><FONT COLOR="#B22222">// direction of the inequality in (*) is reversed.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// INPUT: phi -- a matrix such that phi[i][j][u][v] = phi_{ij}(u, v)
</FONT></I><I><FONT COLOR="#B22222">// psi -- a matrix such that psi[i][u] = psi_i(u)
</FONT></I><I><FONT COLOR="#B22222">// x -- a vector where the optimal solution will be stored
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// OUTPUT: value of the optimal solution
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// To use this code, create a GraphCutInference object, and call the
</FONT></I><I><FONT COLOR="#B22222">// DoInference() method. To perform maximization instead of minimization,
</FONT></I><I><FONT COLOR="#B22222">// ensure that #define MAXIMIZATION is enabled.
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><vector></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><iostream></FONT></B>
using namespace std;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<<B><FONT COLOR="#228B22">int</FONT></B>> VI;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VI> VVI;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VVI> VVVI;
<B><FONT COLOR="#228B22">typedef</FONT></B> vector<VVVI> VVVVI;
<B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> INF = 1000000000;
<I><FONT COLOR="#B22222">// comment out following line for minimization
</FONT></I>#<B><FONT COLOR="#5F9EA0">define</FONT></B> <FONT COLOR="#B8860B">MAXIMIZATION</FONT>
<B><FONT COLOR="#228B22">struct</FONT></B> GraphCutInference {
<B><FONT COLOR="#228B22">int</FONT></B> N;
VVI cap, flow;
VI reached;
<B><FONT COLOR="#228B22">int</FONT></B> Augment(<B><FONT COLOR="#228B22">int</FONT></B> s, <B><FONT COLOR="#228B22">int</FONT></B> t, <B><FONT COLOR="#228B22">int</FONT></B> a) {
reached[s] = 1;
<B><FONT COLOR="#A020F0">if</FONT></B> (s == t) <B><FONT COLOR="#A020F0">return</FONT></B> a;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> k = 0; k < N; k++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (reached[k]) <B><FONT COLOR="#A020F0">continue</FONT></B>;
<B><FONT COLOR="#A020F0">if</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> aa = min(a, cap[s][k] - flow[s][k])) {
<B><FONT COLOR="#A020F0">if</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> b = Augment(k, t, aa)) {
flow[s][k] += b;
flow[k][s] -= b;
<B><FONT COLOR="#A020F0">return</FONT></B> b;
}
}
}
<B><FONT COLOR="#A020F0">return</FONT></B> 0;
}
<B><FONT COLOR="#228B22">int</FONT></B> GetMaxFlow(<B><FONT COLOR="#228B22">int</FONT></B> s, <B><FONT COLOR="#228B22">int</FONT></B> t) {
N = cap.size();
flow = VVI(N, VI(N));
reached = VI(N);
<B><FONT COLOR="#228B22">int</FONT></B> totflow = 0;
<B><FONT COLOR="#A020F0">while</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> amt = Augment(s, t, INF)) {
totflow += amt;
fill(reached.begin(), reached.end(), 0);
}
<B><FONT COLOR="#A020F0">return</FONT></B> totflow;
}
<B><FONT COLOR="#228B22">int</FONT></B> DoInference(<B><FONT COLOR="#228B22">const</FONT></B> VVVVI &phi, <B><FONT COLOR="#228B22">const</FONT></B> VVI &psi, VI &x) {
<B><FONT COLOR="#228B22">int</FONT></B> M = phi.size();
cap = VVI(M+2, VI(M+2));
VI b(M);
<B><FONT COLOR="#228B22">int</FONT></B> c = 0;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < M; i++) {
b[i] += psi[i][1] - psi[i][0];
c += psi[i][0];
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = 0; j < i; j++)
b[i] += phi[i][j][1][1] - phi[i][j][0][1];
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = i+1; j < M; j++) {
cap[i][j] = phi[i][j][0][1] + phi[i][j][1][0] - phi[i][j][0][0] - phi[i][j][1][1];
b[i] += phi[i][j][1][0] - phi[i][j][0][0];
c += phi[i][j][0][0];
}
}
#<B><FONT COLOR="#5F9EA0">ifdef</FONT></B> <FONT COLOR="#B8860B">MAXIMIZATION</FONT>
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < M; i++) {
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> j = i+1; j < M; j++)
cap[i][j] *= -1;
b[i] *= -1;
}
c *= -1;
#<B><FONT COLOR="#5F9EA0">endif</FONT></B>
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < M; i++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (b[i] >= 0) {
cap[M][i] = b[i];
} <B><FONT COLOR="#A020F0">else</FONT></B> {
cap[i][M+1] = -b[i];
c += b[i];
}
}
<B><FONT COLOR="#228B22">int</FONT></B> score = GetMaxFlow(M, M+1);
fill(reached.begin(), reached.end(), 0);
Augment(M, M+1, INF);
x = VI(M);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < M; i++) x[i] = reached[i] ? 0 : 1;
score += c;
#<B><FONT COLOR="#5F9EA0">ifdef</FONT></B> <FONT COLOR="#B8860B">MAXIMIZATION</FONT>
score *= -1;
#<B><FONT COLOR="#5F9EA0">endif</FONT></B>
<B><FONT COLOR="#A020F0">return</FONT></B> score;
}
};
<B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#0000FF">main</FONT></B>() {
<I><FONT COLOR="#B22222">// solver for "Cat vs. Dog" from NWERC 2008
</FONT></I>
<B><FONT COLOR="#228B22">int</FONT></B> numcases;
cin >> numcases;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> caseno = 0; caseno < numcases; caseno++) {
<B><FONT COLOR="#228B22">int</FONT></B> c, d, v;
cin >> c >> d >> v;
VVVVI phi(c+d, VVVI(c+d, VVI(2, VI(2))));
VVI psi(c+d, VI(2));
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < v; i++) {
<B><FONT COLOR="#228B22">char</FONT></B> p, q;
<B><FONT COLOR="#228B22">int</FONT></B> u, v;
cin >> p >> u >> q >> v;
u--; v--;
<B><FONT COLOR="#A020F0">if</FONT></B> (p == <B><FONT COLOR="#BC8F8F">'C'</FONT></B>) {
phi[u][c+v][0][0]++;
phi[c+v][u][0][0]++;
} <B><FONT COLOR="#A020F0">else</FONT></B> {
phi[v][c+u][1][1]++;
phi[c+u][v][1][1]++;
}
}
GraphCutInference graph;
VI x;
cout << graph.DoInference(phi, psi, x) << endl;
}
<B><FONT COLOR="#A020F0">return</FONT></B> 0;
}
</PRE>
<HR>
<A NAME="file8">
<H1>code/ConvexHull.cc 8/35</H1>
[<A HREF="#top">top</A>][<A HREF="#file7">prev</A>][<A HREF="#file9">next</A>]
<PRE>
<I><FONT COLOR="#B22222">// Compute the 2D convex hull of a set of points using the monotone chain
</FONT></I><I><FONT COLOR="#B22222">// algorithm. Eliminate redundant points from the hull if REMOVE_REDUNDANT is
</FONT></I><I><FONT COLOR="#B22222">// #defined.
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// Running time: O(n log n)
</FONT></I><I><FONT COLOR="#B22222">//
</FONT></I><I><FONT COLOR="#B22222">// INPUT: a vector of input points, unordered.
</FONT></I><I><FONT COLOR="#B22222">// OUTPUT: a vector of points in the convex hull, counterclockwise, starting
</FONT></I><I><FONT COLOR="#B22222">// with bottommost/leftmost point
</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cstdio></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cassert></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><vector></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><algorithm></FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><cmath></FONT></B>
<I><FONT COLOR="#B22222">// BEGIN CUT
</FONT></I>#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F"><map></FONT></B>
<I><FONT COLOR="#B22222">// END CUT
</FONT></I>
using namespace std;
#<B><FONT COLOR="#5F9EA0">define</FONT></B> <FONT COLOR="#B8860B">REMOVE_REDUNDANT</FONT>
<B><FONT COLOR="#228B22">typedef</FONT></B> <B><FONT COLOR="#228B22">double</FONT></B> T;
<B><FONT COLOR="#228B22">const</FONT></B> T EPS = 1e-7;
<B><FONT COLOR="#228B22">struct</FONT></B> PT {
T x, y;
PT() {}
PT(T x, T y) : x(x), y(y) {}
<B><FONT COLOR="#228B22">bool</FONT></B> <B><FONT COLOR="#A020F0">operator</FONT></B><(<B><FONT COLOR="#228B22">const</FONT></B> PT &rhs) <B><FONT COLOR="#228B22">const</FONT></B> { <B><FONT COLOR="#A020F0">return</FONT></B> make_pair(y,x) < make_pair(rhs.y,rhs.x); }
<B><FONT COLOR="#228B22">bool</FONT></B> <B><FONT COLOR="#A020F0">operator</FONT></B>==(<B><FONT COLOR="#228B22">const</FONT></B> PT &rhs) <B><FONT COLOR="#228B22">const</FONT></B> { <B><FONT COLOR="#A020F0">return</FONT></B> make_pair(y,x) == make_pair(rhs.y,rhs.x); }
};
T cross(PT p, PT q) { <B><FONT COLOR="#A020F0">return</FONT></B> p.x*q.y-p.y*q.x; }
T area2(PT a, PT b, PT c) { <B><FONT COLOR="#A020F0">return</FONT></B> cross(a,b) + cross(b,c) + cross(c,a); }
#<B><FONT COLOR="#5F9EA0">ifdef</FONT></B> <FONT COLOR="#B8860B">REMOVE_REDUNDANT</FONT>
<B><FONT COLOR="#228B22">bool</FONT></B> <B><FONT COLOR="#0000FF">between</FONT></B>(<B><FONT COLOR="#228B22">const</FONT></B> PT &a, <B><FONT COLOR="#228B22">const</FONT></B> PT &b, <B><FONT COLOR="#228B22">const</FONT></B> PT &c) {
<B><FONT COLOR="#A020F0">return</FONT></B> (fabs(area2(a,b,c)) < EPS && (a.x-b.x)*(c.x-b.x) <= 0 && (a.y-b.y)*(c.y-b.y) <= 0);
}
#<B><FONT COLOR="#5F9EA0">endif</FONT></B>
<B><FONT COLOR="#228B22">void</FONT></B> <B><FONT COLOR="#0000FF">ConvexHull</FONT></B>(vector<PT> &pts) {
sort(pts.begin(), pts.end());
pts.erase(unique(pts.begin(), pts.end()), pts.end());
vector<PT> up, dn;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < pts.size(); i++) {
<B><FONT COLOR="#A020F0">while</FONT></B> (up.size() > 1 && area2(up[up.size()-2], up.back(), pts[i]) >= 0) up.pop_back();
<B><FONT COLOR="#A020F0">while</FONT></B> (dn.size() > 1 && area2(dn[dn.size()-2], dn.back(), pts[i]) <= 0) dn.pop_back();
up.push_back(pts[i]);
dn.push_back(pts[i]);
}
pts = dn;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = (<B><FONT COLOR="#228B22">int</FONT></B>) up.size() - 2; i >= 1; i--) pts.push_back(up[i]);
#<B><FONT COLOR="#5F9EA0">ifdef</FONT></B> <FONT COLOR="#B8860B">REMOVE_REDUNDANT</FONT>
<B><FONT COLOR="#A020F0">if</FONT></B> (pts.size() <= 2) <B><FONT COLOR="#A020F0">return</FONT></B>;
dn.clear();
dn.push_back(pts[0]);
dn.push_back(pts[1]);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 2; i < pts.size(); i++) {
<B><FONT COLOR="#A020F0">if</FONT></B> (between(dn[dn.size()-2], dn[dn.size()-1], pts[i])) dn.pop_back();
dn.push_back(pts[i]);
}
<B><FONT COLOR="#A020F0">if</FONT></B> (dn.size() >= 3 && between(dn.back(), dn[0], dn[1])) {
dn[0] = dn.back();
dn.pop_back();
}
pts = dn;
#<B><FONT COLOR="#5F9EA0">endif</FONT></B>
}
<I><FONT COLOR="#B22222">// BEGIN CUT
</FONT></I><I><FONT COLOR="#B22222">// The following code solves SPOJ problem #26: Build the Fence (BSHEEP)
</FONT></I>
<B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#0000FF">main</FONT></B>() {
<B><FONT COLOR="#228B22">int</FONT></B> t;
scanf(<B><FONT COLOR="#BC8F8F">"%d"</FONT></B>, &t);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> caseno = 0; caseno < t; caseno++) {
<B><FONT COLOR="#228B22">int</FONT></B> n;
scanf(<B><FONT COLOR="#BC8F8F">"%d"</FONT></B>, &n);
vector<PT> v(n);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < n; i++) scanf(<B><FONT COLOR="#BC8F8F">"%lf%lf"</FONT></B>, &v[i].x, &v[i].y);
vector<PT> h(v);
map<PT,<B><FONT COLOR="#228B22">int</FONT></B>> index;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = n-1; i >= 0; i--) index[v[i]] = i+1;
ConvexHull(h);
<B><FONT COLOR="#228B22">double</FONT></B> len = 0;
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < h.size(); i++) {
<B><FONT COLOR="#228B22">double</FONT></B> dx = h[i].x - h[(i+1)%h.size()].x;
<B><FONT COLOR="#228B22">double</FONT></B> dy = h[i].y - h[(i+1)%h.size()].y;
len += sqrt(dx*dx+dy*dy);
}
<B><FONT COLOR="#A020F0">if</FONT></B> (caseno > 0) printf(<B><FONT COLOR="#BC8F8F">"\n"</FONT></B>);
printf(<B><FONT COLOR="#BC8F8F">"%.2f\n"</FONT></B>, len);
<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i = 0; i < h.size(); i++) {