标签:
Dinic不同实现的效率果然不同啊。
1 /* 3277 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 const int INF = 0x1f1f1f1f; 43 const int maxn = 255; 44 const int maxm = maxn*maxn; 45 const int maxv = maxn*3; 46 const int maxe = maxv*maxv*4; 47 bool M[maxn][maxn]; 48 int V[maxe], F[maxe], F_[maxe], nxt[maxe]; 49 int dis[maxv], head[maxv], head_[maxv], Q[maxv]; 50 int pre[maxn]; 51 int X[maxm], Y[maxm]; 52 int s, t; 53 int n, m; 54 55 void addEdge(int u, int v, int c) { 56 #ifndef ONLINE_JUDGE 57 printf("u = %d, v = %d, c = %d\n", u, v, c); 58 #endif 59 V[m] = v; 60 F_[m] = c; 61 nxt[m] = head[u]; 62 head[u] = m++; 63 64 V[m] = u; 65 F_[m] = 0; 66 nxt[m] = head[v]; 67 head[v] = m++; 68 } 69 70 int find(int x) { 71 if (x == pre[x]) 72 return x; 73 return pre[x] = find(pre[x]); 74 } 75 76 void merge(int x, int y) { 77 int fx = find(x); 78 int fy = find(y); 79 80 if (fx != fy) 81 pre[fx] = fy; 82 } 83 84 bool bfs() { 85 int l = 0, r = 0; 86 int u, v, k; 87 88 memset(dis, 0, sizeof(dis)); 89 Q[r++] = s; 90 dis[s] = 1; 91 92 while (l < r) { 93 u = Q[l++]; 94 for (k=head[u]; k!=-1; k=nxt[k]) { 95 v = V[k]; 96 if (!dis[v] && F[k]) { 97 dis[v] = dis[u] + 1; 98 if (v == t) 99 return false; 100 Q[r++] = v; 101 } 102 } 103 } 104 105 return true; 106 } 107 108 int dfs(int u, int val) { 109 if (u==t || val==0) 110 return val; 111 112 int ret = 0; 113 int tmp, v; 114 115 for (int& k=head_[u]; k!=-1; k=nxt[k]) { 116 v = V[k]; 117 if (F[k] && dis[v]==dis[u]+1 && (tmp=dfs(v, min(val, F[k])))>0) { 118 F[k] -= tmp; 119 F[k^1] += tmp; 120 ret += tmp; 121 val -= tmp; 122 if (val == 0) 123 break; 124 } 125 } 126 127 return ret; 128 } 129 130 int Dinic() { 131 int ret = 0, tmp; 132 133 while (1) { 134 if (bfs()) 135 break; 136 137 memcpy(head_, head, sizeof(head)); 138 while (1) { 139 tmp = dfs(s, INF); 140 if (tmp == 0) 141 break; 142 ret += tmp; 143 } 144 } 145 146 return ret; 147 } 148 149 int main() { 150 ios::sync_with_stdio(false); 151 #ifndef ONLINE_JUDGE 152 freopen("data.in", "r", stdin); 153 freopen("data.out", "w", stdout); 154 #endif 155 156 int tt; 157 int c, K, f; 158 int x, y; 159 int l, r, mid; 160 int ans, tmp; 161 162 scanf("%d", &tt); 163 while (tt--) { 164 scanf("%d %d %d %d", &n, &c, &K, &f); 165 rep(i, 0, c) 166 scanf("%d %d", &X[i], &Y[i]); 167 rep(i, 1, n+1) 168 pre[i] = i; 169 while (f--) { 170 scanf("%d %d", &x, &y); 171 merge(x, y); 172 } 173 174 m = 0; 175 s = 3*n+1; 176 t = s + 1; 177 memset(head, -1, sizeof(head)); 178 memset(M, false, sizeof(M)); 179 180 rep(i, 1, n+1) { 181 find(i); 182 addEdge(s, i, 0); 183 addEdge(i+n*2, t, 0); 184 } 185 rep(i, 1, n+1) 186 addEdge(i, i+n, K); 187 188 rep(i, 0, c) { 189 x = X[i]; 190 y = Y[i]; 191 rep(j, 1, n+1) { 192 if (pre[x]==pre[j] && !M[j][y]) { 193 M[j][y] = true; 194 addEdge(j, y+n*2, 1); 195 } 196 } 197 } 198 199 rep(i, 1, n+1) { 200 rep(j, 1, n+1) { 201 if (!M[i][j]) { 202 addEdge(i+n, j+n*2, 1); 203 } 204 } 205 } 206 207 l = 0; 208 r = n; 209 ans = 0; 210 211 while (l <= r) { 212 mid = (l + r) >> 1; 213 for (int i=0; i<4*n; i+=4) { 214 F[i] = F[i+2] = mid; 215 F[i+1] = F[i+3] = 0; 216 } 217 rep(i, 4*n, m) 218 F[i] = F_[i]; 219 tmp = Dinic(); 220 #ifndef ONLINE_JUDGE 221 printf("tmp = %d\n", tmp); 222 #endif 223 if (tmp >= mid*n) { 224 ans = mid; 225 l = mid + 1; 226 } else { 227 r = mid - 1; 228 } 229 } 230 231 printf("%d\n", ans); 232 } 233 234 #ifndef ONLINE_JUDGE 235 printf("time = %d.\n", (int)clock()); 236 #endif 237 238 return 0; 239 }
标签:
原文地址:http://www.cnblogs.com/bombe1013/p/4886444.html