标签:printf other icp ssi first connect div namespace min
题目链接:https://vjudge.net/problem/HDU-3488
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3720 Accepted Submission(s): 1777
题解:
代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int INF = 0x3f3f3f3f; 5 const LL LNF = 9e18; 6 const int mod = 1e9+7; 7 const int MAXN = 2e2+10; 8 9 int nx, ny; 10 int g[MAXN][MAXN]; 11 int linker[MAXN], lx[MAXN], ly[MAXN]; 12 int slack[MAXN]; 13 bool visx[MAXN], visy[MAXN]; 14 15 bool DFS(int x) 16 { 17 visx[x] = true; 18 for(int y = 1; y<=ny; y++) 19 { 20 if(visy[y]) continue; 21 int tmp = lx[x] + ly[y] - g[x][y]; 22 if(tmp==0) 23 { 24 visy[y] = true; 25 if(linker[y]==-1 || DFS(linker[y])) 26 { 27 linker[y] = x; 28 return true; 29 } 30 } 31 else 32 slack[y] = min(slack[y], tmp); 33 } 34 return false; 35 } 36 37 int KM() 38 { 39 memset(linker, -1, sizeof(linker)); 40 memset(ly, 0, sizeof(ly)); 41 for(int i = 1; i<=nx; i++) 42 { 43 lx[i] = -INF; 44 for(int j = 1; j<=ny; j++) 45 lx[i] = max(lx[i], g[i][j]); 46 } 47 48 for(int x = 1; x<=nx; x++) 49 { 50 for(int i = 1; i<=ny; i++) 51 slack[i] = INF; 52 while(true) 53 { 54 memset(visx, 0, sizeof(visx)); 55 memset(visy, 0, sizeof(visy)); 56 57 if(DFS(x)) break; 58 int d = INF; 59 for(int i = 1; i<=ny; i++) 60 if(!visy[i]) 61 d = min(d, slack[i]); 62 63 for(int i = 1; i<=nx; i++) 64 if(visx[i]) 65 lx[i] -= d; 66 for(int i = 1; i<=ny; i++) 67 { 68 if(visy[i]) ly[i] += d; 69 else slack[i] -= d; 70 } 71 } 72 } 73 74 int res = 0; 75 for(int i = 1; i<=ny; i++) 76 if(linker[i]!=-1) 77 res += g[linker[i]][i]; 78 return res; 79 } 80 81 int main() 82 { 83 int T, n, m; 84 scanf("%d", &T); 85 while(T--) 86 { 87 scanf("%d%d", &n,&m); 88 nx = ny = n; 89 memset(g, 0, sizeof(g)); 90 for(int i = 1; i<=nx; i++) 91 for(int j = 1; j<=ny; j++) 92 g[i][j] = -INF; 93 for(int i = 1; i<=m; i++) 94 { 95 int u, v, w; 96 scanf("%d%d%d", &u, &v, &w); 97 g[u][v] = max(g[u][v], -w); 98 } 99 100 printf("%d\n", -KM()); 101 } 102 }
标签:printf other icp ssi first connect div namespace min
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7827846.html