标签:des style blog http color io os java ar
2 3 2 0 3 2 1 1 3 0 3 2 0 2 3 0 1 0 1 2 1 0 2 1 0 0 2 0
2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 500; 18 struct arc { 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = 0) { 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 int K,C,M,S,T,tot,mp[maxn][maxn],head[maxn],d[maxn]; 27 arc e[200000]; 28 void add(int u,int v,int flow) { 29 e[tot] = arc(v,flow,head[u]); 30 head[u] = tot++; 31 e[tot] = arc(u,0,head[v]); 32 head[v] = tot++; 33 } 34 void Floyd() { 35 for(int k = 1; k <= K+C; k++) { 36 for(int i = 1; i <= K+C; i++) { 37 for(int j = 1; mp[i][k] < INF && j <= K+C; j++) 38 if(mp[k][j] < INF && mp[i][j] > mp[i][k]+mp[k][j]) 39 mp[i][j] = mp[i][k] + mp[k][j]; 40 } 41 } 42 } 43 queue<int>q; 44 bool bfs() { 45 memset(d,0,sizeof(d)); 46 while(!q.empty()) q.pop(); 47 d[S] = 1; 48 q.push(S); 49 while(!q.empty()) { 50 int u = q.front(); 51 q.pop(); 52 for(int i = head[u]; ~i; i = e[i].next) { 53 if(e[i].flow > 0 && !d[e[i].to]) { 54 d[e[i].to] = d[u] + 1; 55 q.push(e[i].to); 56 } 57 } 58 } 59 return d[T] > 0; 60 } 61 int dfs(int u,int low) { 62 if(u == T) return low; 63 int tmp = 0,a; 64 for(int i = head[u]; ~i; i = e[i].next) { 65 if(d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(low,e[i].flow)))) { 66 tmp += a; 67 e[i].flow -= a; 68 e[i^1].flow += a; 69 low -= a; 70 if(!low) break; 71 } 72 } 73 return tmp; 74 } 75 int Dinic() { 76 int tmp = 0; 77 while(bfs()) tmp += dfs(S,INF); 78 return tmp; 79 } 80 int main() { 81 int low,high,mid; 82 while(~scanf("%d %d %d",&K,&C,&M)) { 83 low = INF; 84 high = 0; 85 for(int i = 1; i <= K+C; i++) { 86 for(int j = 1; j <= K+C; j++) { 87 scanf("%d",mp[i]+j); 88 if(!mp[i][j]) mp[i][j] = INF; 89 } 90 } 91 Floyd(); 92 S = 0; 93 T = K + C + 1; 94 for(int i = 1; i <= K+C; i++) 95 for(int j = i+1; j <= K+C; j++) 96 if(mp[i][j] < INF) { 97 low = min(low,mp[i][j]); 98 high = max(high,mp[i][j]); 99 } 100 while(low < high) { 101 mid = (low+high)>>1; 102 memset(head,-1,sizeof(head)); 103 for(int i = K+1; i <= K+C; i++) { 104 add(S,i,1); 105 for(int j = 1; j <= K; j++) { 106 if(mp[i][j] <= mid) {add(i,j,INF);} 107 } 108 } 109 for(int i = 1; i <= K; i++) add(i,T,M); 110 int Flow = Dinic(); 111 if(Flow < C) low = mid+1; 112 else high = mid; 113 } 114 printf("%d\n",low); 115 } 116 return 0; 117 }
加速版
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 250; 18 struct arc { 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = 0) { 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 int K,C,M,S,T,tot,mp[maxn][maxn],head[maxn],d[maxn],cur[maxn]; 27 arc e[20000]; 28 void add(int u,int v,int flow) { 29 e[tot] = arc(v,flow,head[u]); 30 head[u] = tot++; 31 e[tot] = arc(u,0,head[v]); 32 head[v] = tot++; 33 } 34 void Floyd() { 35 for(int k = 1; k <= K+C; k++) { 36 for(int i = 1; i <= K+C; i++) { 37 for(int j = 1; mp[i][k] < INF && j <= K+C; j++) 38 if(mp[k][j] < INF && mp[i][j] > mp[i][k]+mp[k][j]) 39 mp[i][j] = mp[i][k] + mp[k][j]; 40 } 41 } 42 } 43 queue<int>q; 44 bool bfs() { 45 memset(d,-1,sizeof(d)); 46 while(!q.empty()) q.pop(); 47 d[S] = 0; 48 q.push(S); 49 while(!q.empty()) { 50 int u = q.front(); 51 q.pop(); 52 for(int i = head[u]; ~i; i = e[i].next) { 53 if(e[i].flow > 0 && d[e[i].to] < 0) { 54 d[e[i].to] = d[u] + 1; 55 q.push(e[i].to); 56 } 57 } 58 } 59 return d[T] > -1; 60 } 61 int dfs(int u,int low) { 62 if(u == T) return low; 63 int tmp = 0,a; 64 for(int &i = cur[u]; ~i; i = e[i].next) { 65 if(e[i].flow && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(low,e[i].flow)))) { 66 tmp += a; 67 e[i].flow -= a; 68 e[i^1].flow += a; 69 low -= a; 70 if(!low) break; 71 } 72 } 73 if(tmp == 0) d[u] = -1; 74 return tmp; 75 } 76 int Dinic() { 77 int tmp = 0; 78 while(bfs()) { 79 for(int i = 0; i <= T; i++) 80 cur[i] = head[i]; 81 tmp += dfs(S,INF); 82 } 83 return tmp; 84 } 85 int main() { 86 int low,high,mid; 87 while(~scanf("%d %d %d",&K,&C,&M)) { 88 low = INF; 89 high = 0; 90 for(int i = 1; i <= K+C; i++) { 91 for(int j = 1; j <= K+C; j++) { 92 scanf("%d",mp[i]+j); 93 if(!mp[i][j]) mp[i][j] = INF; 94 } 95 } 96 Floyd(); 97 S = 0; 98 T = K + C + 1; 99 for(int i = 1; i <= K+C; i++) 100 for(int j = i+1; j <= K+C; j++) 101 if(mp[i][j] < INF) { 102 low = min(low,mp[i][j]); 103 high = max(high,mp[i][j]); 104 } 105 while(low < high) { 106 mid = (low+high)>>1; 107 memset(head,-1,sizeof(head)); 108 tot = 0; 109 for(int i = K+1; i <= K+C; i++) { 110 add(S,i,1); 111 for(int j = 1; j <= K; j++) { 112 if(mp[i][j] <= mid) { 113 add(i,j,INF); 114 } 115 } 116 } 117 for(int i = 1; i <= K; i++) add(i,T,M); 118 if(Dinic() < C) low = mid+1; 119 else high = mid; 120 } 121 printf("%d\n",low); 122 } 123 return 0; 124 }
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3975930.html