-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
3133 lines (3119 loc) · 72.8 KB
/
graph.cpp
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
//1.链式前向星
void ins(int u, int v, int w)
{
cnt++;
e[cnt].to = v;
e[cnt].next = head[u];
e[cnt].w = w;
head[u] = cnt;
}
for (int i = head[x]; i; i = e[i].next)
{
v = e[i].to;
u = x;
w = e[i].w;
}
//2.并查集(不要忘记初始化)
//①普通并查集
int findset(int x) { reuturn pa[x] != x ? pa[x] = findset(pa[x]) : x; }
//②使用补集思想(团伙、食物链)
x = e[i].a;
y = e[i].b;
v = e[i].c;
x = findf(x);
y = findf(y);
if (x == y)
{
cout << v;
return 0;
}
fa[x] = findf(e[i].b + n);
fa[y] = findf(e[i].a + n);
//③ 带权并查集 (银河英雄传说)
int findf(int x)
{
if (x != f[x])
{
int r = findf(f[x]);
d[x] += d[f[x]];
f[x] = r;
}
return f[x];
}
for (int i = 1; i <= 30000; i++)
{
f[i] = i;
sz[i] = 1;
}
if (s == 'M')
{
scanf("%d%d", &u, &v);
u = findf(u);
v = findf(v);
f[u] = v;
d[u] = sz[v];
sz[v] += sz[u];
}
else if (s == 'C')
{
scanf("%d%d", &u, &v);
ur = findf(u);
vr = findf(v);
if (ur == vr)
printf("%d\n", abs(d[u] - d[v]) - 1);
else
printf("-1\n");
}
//3.最短路
//①floyd
void floyd()
{
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
if (k == i)
continue;
for (int j = 1; j <= n; j++)
{
if (k != i && k != j && i != j)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
}
//②spfa
void spfa(int u)
{
int i, v, w;
for (i = 1; i <= n; i++)
{
dist[i] = inf;
inq[i] = false;
}
queue<int> q;
q.push(u);
inq[u] = true;
dist[u] = 0;
while (!q.empty())
{
u = q.front();
q.pop();
inq[u] = false;
for (i = 0; i < g[u].size(); i++)
{
v = g[u][i].v;
w = g[u][i].w;
if (dist[u] + w < dist[v])
{
dist[v] = dist[u] + w;
if (!inq[v])
{
q.push(v);
inq[v] = true;
}
}
}
}
}
//③普通dij(n^2)
int Dijkstra(int s, int e)
{
d[s] = 0;
for (int v = -1; 1; v = -1)
{
for (int i = 1; i <= V; i++)
if (!used[i] && (v == -1 || d[i] < d[v]))
v = i;
if (v == -1)
break;
used[v] = 1;
for (int i = 1; i <= V; i++)
d[i] = min(d[i], d[v] + graph[v][i]);
}
return d[e];
}
//④堆优化dij(sth版)
struct orz
{
int d, p;
friend bool operator<(orz a, orz b) { return a.d > b.d; } //堆和set里面都只有小于号,所以要用小根堆的话要将<重定向为>
};
struct Edge
{
int to;
int w;
};
priority_queue<orz> ss;
void dij(int s)
{
d[s] = 0;
orz tmp;
tmp.d = 0, tmp.p = s;
ss.push(tmp);
flag++;
int x, dd;
Edge j;
while (!ss.empty()) //不能只做n次,要一直做到堆空
{
tmp = ss.top();
ss.pop();
x = tmp.p, dd = tmp.d;
if (v[x] == flag)
continue; //这里一定要判断!!!
v[x] = flag;
for (int i = 0; i < edge[x].size(); i++)
{
j = edge[x][i];
if (d[j.to] > dd + j.w)
{
d[j.to] = dd + j.w;
tmp.d = dd + j.w, tmp.p = j.to;
ss.push(tmp);
}
}
}
}
//⑤求最短路径条数(社交网络)
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
a[u][v] = a[v][u] = w;
cnts[u][v] = cnts[v][u] = 1;
}
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (k != i && k != j && i != j)
{
if (a[i][j] > a[i][k] + a[k][j])
{
a[i][j] = a[i][k] + a[k][j];
cnts[i][j] = cnts[i][k] * cnts[k][j];
}
else if (a[i][j] == a[i][k] + a[k][j])
{
cnts[i][j] += cnts[i][k] * cnts[k][j];
}
}
}
}
}
//4.最小生成树
//poj 1287
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define ll long long
#define mx 100005
using namespace std;
struct edge
{
int u;
int v;
long long w;
};
int n, k, m, f[mx], vis[mx], pre[mx], fa, fb;
ll ans;
edge g[mx], tmp;
int findf(int x)
{
return x == f[x] ? x : f[x] = findf(f[x]);
}
bool cmp(edge a, edge b)
{
return a.w < b.w;
}
int main()
{
while (1)
{
ans = 0;
cin >> n;
if (n == 0)
break;
cin >> m;
long long u, v, w, ans = 0, sum = 0;
for (int i = 1; i <= n; i++)
{
f[i] = i;
}
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> w;
tmp.u = u;
tmp.v = v;
tmp.w = w;
g[i] = tmp;
}
sort(g + 1, g + 1 + m, cmp);
for (int i = 1; i <= m; i++)
{
fa = findf(g[i].u);
fb = findf(g[i].v);
if (fa == fb)
continue;
f[fa] = fb;
ans += g[i].w;
}
cout << ans << endl;
}
return 0;
}
//5.欧拉回路
//hdu 1878
//是否存在欧拉回路
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define fd(i, l, r) for (int i = r; i >= l; i--)
using namespace std;
const int maxn = 1050;
ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while (!(ch >= '0' && ch <= '9'))
{
if (ch == '-')
f = -1;
ch = getchar();
};
while (ch >= '0' && ch <= '9')
{
x = x * 10 + (ch - '0');
ch = getchar();
};
return x * f;
}
int n, m, u, v, d[maxn];
bool vis[maxn];
vector<int> g[maxn];
void dfs(int x)
{
vis[x] = true;
for (int i = 0; i < g[x].size(); i++)
{
if (!vis[g[x][i]])
{
dfs(g[x][i]);
}
}
return;
}
int main()
{
while (1)
{
n = read();
if (n == 0)
break;
m = read();
memset(d, 0, sizeof(d));
fo(i, 1, n) g[i].clear();
fo(i, 1, m)
{
u = read();
v = read();
d[u]++;
d[v]++;
g[u].push_back(v);
g[v].push_back(u);
}
memset(vis, 0, sizeof(vis));
dfs(1);
bool ok = true;
fo(i, 1, n)
{
if (!vis[i])
{
cout << 0 << endl;
ok = false;
break;
}
}
if (!ok)
continue;
fo(i, 1, n)
{
if (d[i] % 2 != 0)
{
cout << 0 << endl;
ok = false;
break;
}
}
if (ok)
cout << 1 << endl;
}
return 0;
}
//HRBUST - 2310
//存在多少条欧拉路径?
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define fd(i, l, r) for (int i = r; i >= l; i--)
using namespace std;
const int maxn = 100500;
ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while (!(ch >= '0' && ch <= '9'))
{
if (ch == '-')
f = -1;
ch = getchar();
};
while (ch >= '0' && ch <= '9')
{
x = x * 10 + (ch - '0');
ch = getchar();
};
return x * f;
}
int n, m, u, v, d[maxn], f[maxn];
int main()
{
int T;
cin >> T;
while (T--)
{
memset(f, 0, sizeof(f));
memset(d, 0, sizeof(d));
n = read();
fo(i, 1, n - 1)
{
u = read();
v = read();
d[u]++;
d[v]++;
}
int ans = 0;
fo(i, 1, n)
{
if (d[i] % 2 == 1)
ans++;
}
cout << ans / 2 << endl;
}
return 0;
}
//6.差分约束
//HYSBZ - 2330 糖果
/*
如果X=1, 表示第A个小朋友分到的糖果必须和第B个小朋友分到的糖果一样多;
如果X=2, 表示第A个小朋友分到的糖果必须少于第B个小朋友分到的糖果;
如果X=3, 表示第A个小朋友分到的糖果必须不少于第B个小朋友分到的糖果;
如果X=4, 表示第A个小朋友分到的糖果必须多于第B个小朋友分到的糖果;
如果X=5, 表示第A个小朋友分到的糖果必须不多于第B个小朋友分到的糖果;
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;
const int maxn = 100050;
struct edge
{
int v;
int w;
int nxt;
} e[maxn * 3];
int n, k;
int cnt, head[maxn];
int rd[maxn];
bool vis[maxn];
ll dis[maxn];
int read()
{
char ch = getchar();
int f = 1, x = 0;
while (!(ch >= '0' && ch <= '9'))
{
if (ch == '-')
f = -1;
ch = getchar();
};
while (ch >= '0' && ch <= '9')
{
x = x * 10 + (ch - '0');
ch = getchar();
};
return x * f;
}
int ins(int u, int v, int w)
{
cnt++;
e[cnt].v = v;
e[cnt].w = w;
e[cnt].nxt = head[u];
head[u] = cnt;
}
bool spfa()
{
int now, nt;
queue<int> q;
for (int i = 1; i <= n; i++)
{
q.push(i);
dis[i] = rd[i] = 1;
}
while (!q.empty())
{
now = q.front();
q.pop();
for (int i = head[now]; i; i = e[i].nxt)
{
nt = e[i].v;
if (dis[nt] < dis[now] + e[i].w)
{
dis[nt] = dis[now] + e[i].w;
if (!vis[nt])
{
vis[nt] = true;
q.push(nt);
rd[nt]++;
if (rd[nt] > n)
return false;
}
}
}
vis[now] = false;
}
return true;
}
int main()
{
n = read();
k = read();
int x, a, b;
for (int i = 1; i <= k; i++)
{
x = read();
a = read();
b = read();
if (x == 1)
{
ins(a, b, 0);
ins(b, a, 0);
}
else if (x == 2)
{
if (a == b)
{
cout << -1;
return 0;
}
ins(a, b, 1);
}
else if (x == 3)
{
ins(b, a, 0);
}
else if (x == 4)
{
if (a == b)
{
cout << -1;
return 0;
}
ins(b, a, 1);
}
else
{
ins(a, b, 0);
}
}
ll ans = 0;
if (spfa())
{
for (int i = 1; i <= n; i++)
ans += dis[i];
cout << ans;
}
else
{
cout << -1;
}
return 0;
}
//7.拓扑排序
//①dfs(lrj版)
vector<int> G[mx]; //邻接表存储图
int c[mx], topo[mx], t, n, m; //c表示是否访问过(1为访问过,0为未访问,-1为正访问),topo表示拓扑序,t为倒序的排位指针,n为节点数,m为边数
bool dfs(int u)
{
c[u] = -1; // 访问标志
for (int i = 0, v; i < G[u].size(); i++)
{
v = G[u][i]; //遍历出边
if (c[v] < 0)
return false; //存在有向环,失败退出
else if (!c[v] && !dfs(v))
return false; //如果已经访问,或者自身往下形不成拓扑序,失败退出;
}
c[u] = 1;
topo[--t] = u;
return true;
}
bool topsort()
{
t = n;
memset(c, 0, sizeof(c));
for (int u = 0; u < n; u++)
if (!c[u])
if (!dfs(u))
return false;
return true;
}
void output()
{
if (topsort())
{
for (int i = 0; i < n; i++)
cout << topo[i] << " "; //输出拓扑序列
cout << endl;
}
else
{
cout << "No way!" << endl;
}
}
//②有向无环图上的最长链
void dfs(int x)
{
if (!g[x].size())
{
f[x] = 1;
return;
}
for (int i = 0; i < g[x].size(); i++)
{
if (!f[g[x][i]])
dfs(g[x][i]);
f[x] = (f[x] + f[g[x][i]]) % mod;
}
}
void dp()
{
for (int i = 1; i <= n; i++)
{
if (!f[topo[i]])
dfs(topo[i]);
if (!ind[topo[i]])
ans = (ans + f[topo[i]]) % mod;
}
cout << ans;
}
//8.二分图匹配
//①一般匹配
/* *******************************
* 二分图匹配(Hopcroft-Karp 算法)
* 复杂度 O(sqrt(n)*E)
* 邻接表存图, vector 实现
* vector 先初始化,然后假如边
* uN 为左端的顶点数,使用前赋值 (点编号 0 开始)
*/
const int MAXN = 3000;
const int INF = 0x3f3f3f3f;
vector<int> G[MAXN];
int uN;
int Mx[MAXN], My[MAXN];
int dx[MAXN], dy[MAXN];
int dis;
bool used[MAXN];
bool SearchP()
{
queue<int> Q;
dis = INF;
memset(dx, -1, sizeof(dx));
memset(dy, -1, sizeof(dy));
for (int i = 0; i < uN; i++)
if (Mx[i] == -1)
{
Q.push(i);
dx[i] = 0;
}
while (!Q.empty())
{
int u = Q.front();
Q.pop();
if (dx[u] > dis)
break;
int sz = G[u].size();
for (int i = 0; i < sz; i++)
{
int v = G[u][i];
if (dy[v] == -1)
{
dy[v] = dx[u] + 1;
if (My[v] == -1)
dis = dy[v];
else
{
dx[My[v]] = dy[v] + 1;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
bool DFS(int u)
{
int sz = G[u].size();
for (int i = 0; i < sz; i++)
{
int v = G[u][i];
if (!used[v] && dy[v] == dx[u] + 1)
{
used[v] = true;
if (My[v] != -1 && dy[v] == dis)
continue;
if (My[v] == -1 || DFS(My[v]))
{
My[v] = u;
Mx[u] = v;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int res = 0;
memset(Mx, -1, sizeof(Mx));
memset(My, -1, sizeof(My));
while (SearchP())
{
memset(used, false, sizeof(used));
for (int i = 0; i < uN; i++)
if (Mx[i] == -1 && DFS(i))
res++;
}
return res;
}
/*
* 匈牙利算法邻接表形式
* 使用前用 init() 进行初始化,给 uN 赋值
* 加边使用函数 addedge(u,v)
*
*/
const int MAXN = 5010; //点数的最大值
const int MAXM = 50010; //边数的最大值
struct Edge
{
int to, next;
} edge[MAXM];
int head[MAXN], tot;
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
int linker[MAXN];
bool used[MAXN];
int uN;
bool dfs(int u)
{
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (!used[v])
{
used[v] = true;
if (linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker, -1, sizeof(linker));
//点的编号 0∼uN-1
for (int u = 0; u < uN; u++)
{
memset(used, false, sizeof(used));
if (dfs(u))
res++;
}
return res;
}
/* ***********************************************************
//二分图匹配(匈牙利算法的 DFS 实现) (邻接矩阵形式)
//初始化: g[][] 两边顶点的划分情况
//建立 g[i][j] 表示 i->j 的有向边就可以了,是左边向右边的匹配
//g 没有边相连则初始化为 0
//uN 是匹配左边的顶点数, vN 是匹配右边的顶点数
//调用: res=hungary(); 输出最大匹配数
//优点:适用于稠密图, DFS 找增广路,实现简洁易于理解
//时间复杂度:O(VE)
//*************************************************************/
//顶点编号从 0 开始的
const int MAXN = 510;
int uN, vN; //u,v 的数目,使用前面必须赋值
int g[MAXN][MAXN]; //邻接矩阵
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
for (int v = 0; v < vN; v++)
if (g[u][v] && !used[v])
{
used[v] = true;
if (linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker, -1, sizeof(linker));
for (int u = 0; u < uN; u++)
{
memset(used, false, sizeof(used));
if (dfs(u))
res++;
}
return res;
}
//②多重匹配
const int MAXN = 1010;
const int MAXM = 510;
int uN, vN;
int g[MAXN][MAXM];
int linker[MAXM][MAXN];
bool used[MAXM];
int num[MAXM]; //右边最大的匹配数
bool dfs(int u)
{
for (int v = 0; v < vN; v++)
if (g[u][v] && !used[v])
{
used[v] = true;
if (linker[v][0] < num[v])
{
linker[v][++linker[v][0]] = u;
return true;
}
for (int i = 1; i <= num[v]; i++)
if (dfs(linker[v][i]))
{
linker[v][i] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = 0;
for (int i = 0; i < vN; i++)
linker[i][0] = 0;
for (int u = 0; u < uN; u++)
{
memset(used, false, sizeof(used));
if (dfs(u))
res++;
}
return res;
}
//③二分图最大权匹配
/* KM 算法
* 复杂度 O(nx*nx*ny)
* 求最大权匹配
* 若求最小权匹配,可将权值取相反数,结果取相反数
* 点的编号从 0 开始
*/
const int N = 310;
const int INF = 0x3f3f3f3f;
int nx, ny; //两边的点数
int g[N][N]; //二分图描述
int linker[N], lx[N], ly[N]; //y 中各点匹配状态, x,y 中的点标号
int slack[N];
bool visx[N], visy[N];
bool DFS(int x)
{
visx[x] = true;
for (int y = 0; y < ny; y++)
{
if (visy[y])
continue;
int tmp = lx[x] + ly[y] - g[x][y];
if (tmp == 0)
{
visy[y] = true;
if (linker[y] == -1 || DFS(linker[y]))
{
linker[y] = x;
return true;
}
}
else if (slack[y] > tmp)
slack[y] = tmp;
}
return false;
}
int KM()
{
memset(linker, -1, sizeof(linker));
memset(ly, 0, sizeof(ly));
for (int i = 0; i < nx; i++)
{
lx[i] = -INF;
for (int j = 0; j < ny; j++)
if (g[i][j] > lx[i])
lx[i] = g[i][j];
}
for (int x = 0; x < nx; x++)
{
for (int i = 0; i < ny; i++)
slack[i] = INF;
while (true)
{
memset(visx, false, sizeof(visx));
memset(visy, false, sizeof(visy));
if (DFS(x))
break;
int d = INF;
for (int i = 0; i < ny; i++)
if (!visy[i] && d > slack[i])
d = slack[i];
for (int i = 0; i < nx; i++)
if (visx[i])
lx[i] -= d;
for (int i = 0; i < ny; i++)
{
if (visy[i])
ly[i] += d;
else
slack[i] -= d;
}
}
}
int res = 0;
for (int i = 0; i < ny; i++)
if (linker[i] != -1)
res += g[linker[i]][i];
return res;
}
//④一般图匹配
//HDU 2255
int main()
{
int n;
while (scanf("%d", &n) == 1)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &g[i][j]);
nx = ny = n;
printf("%d\n", KM());
}
return 0;
}
const int MAXN = 250;
int N; //点的个数,点的编号从 1 到 N
bool Graph[MAXN][MAXN];
int Match[MAXN];
bool InQueue[MAXN], InPath[MAXN], InBlossom[MAXN];
int Head, Tail;
int Queue[MAXN];
int Start, Finish;
int NewBase;
int Father[MAXN], Base[MAXN];
int Count; //匹配数,匹配对数是 Count/2
void CreateGraph()
{
int u, v;
memset(Graph, false, sizeof(Graph));
scanf("%d", &N);
while (scanf("%d%d", &u, &v) == 2)
{
Graph[u][v] = Graph[v][u] = true;
}
}
void Push(int u)
{
Queue[Tail] = u;
Tail++;
InQueue[u] = true;
}
int Pop()
{
int res = Queue[Head];
Head++;
return res;
}
int FindCommonAncestor(int u, int v)
{
memset(InPath, false, sizeof(InPath));