Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9129 | Accepted: 2742 |
Description
Input
Output
Sample Input
7 9 2 1 2 2 2 3 5 3 7 5 1 4 1 4 3 1 4 5 7 5 7 1 1 6 3 6 7 3
Sample Output
5
Hint
Source
1 #include <iostream> 2 #include <queue> 3 #include <cstdio> 4 #include <cstring> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 40005; 9 const int INF = 1000000000; 10 11 struct Edge { 12 int from, to, cap, flow; 13 }; 14 15 struct Dinic 16 { 17 int n, m, s, t; 18 vector<Edge> edges; 19 vector<int> G[maxn]; 20 bool vis[maxn]; 21 int d[maxn]; 22 int cur[maxn]; 23 24 void ClearAll(int n) { 25 for(int i = 0; i <= n; i++) G[i].clear(); 26 edges.clear(); 27 } 28 29 void AddEdge(int from, int to, int cap) { 30 edges.push_back((Edge) { from, to, cap, 0}); 31 edges.push_back((Edge) { to, from, 0, 0} ); 32 m = edges.size(); 33 G[from].push_back(m - 2); 34 G[to].push_back(m - 1); 35 } 36 37 bool BFS() { 38 memset(vis, 0, sizeof(vis)); 39 queue<int> Q; 40 Q.push(s); 41 d[s] = 0; 42 vis[s] = 1; 43 while(!Q.empty()) { 44 int x = Q.front(); Q.pop(); 45 for(int i = 0; i < G[x].size(); i++) { 46 Edge& e = edges[G[x][i]]; 47 if(!vis[e.to] && e.cap > e.flow) { 48 vis[e.to] = 1; 49 d[e.to] = d[x] + 1; 50 Q.push(e.to); 51 } 52 } 53 } 54 return vis[t]; 55 } 56 57 int DFS(int x, int a) { 58 if(x == t || a == 0) return a; 59 int flow = 0, f; 60 for(int& i = cur[x]; i < G[x].size(); i++) { 61 Edge& e = edges[G[x][i]]; 62 if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) { 63 e.flow += f; 64 edges[G[x][i]^1].flow -= f; 65 flow += f; 66 a -= f; 67 if(a == 0) break; 68 } 69 } 70 return flow; 71 } 72 73 int MaxFlow(int s, int t) { 74 this -> s = s; this -> t = t; 75 int flow = 0; 76 while(BFS()) { 77 memset(cur, 0, sizeof(cur)); 78 flow += DFS(s, INF); 79 } 80 return flow; 81 } 82 }; 83 84 Dinic g; 85 86 int N, P, T, s, t; 87 88 struct Point { 89 int x, y, z; 90 }point[maxn]; 91 92 bool check(int mid) { 93 //printf("*%d\n",mid); 94 g.ClearAll(maxn); 95 g.AddEdge(s, 1, T); 96 g.AddEdge(N, t, T); 97 for(int i = 0; i < P; i++) { 98 if(point[i].z <= mid) { 99 g.AddEdge(point[i].x, point[i].y, 1); 100 g.AddEdge(point[i].y, point[i].x, 1); 101 //printf("#%d %d %d\n",point[i].x, point[i].y, point[i].z); 102 } 103 } 104 int ans = g.MaxFlow(s, t); 105 //printf("%d\n",ans); 106 if(ans != T) return true; 107 else return false; 108 } 109 110 int main() { 111 //freopen("a.txt","r",stdin); 112 while(EOF != scanf("%d %d %d",&N, &P, &T)) { 113 s = 0; t = N + 1; 114 //printf("%d %d\n",s, t); 115 int max_num = -INF, min_num = INF; 116 for(int i = 0; i < P; i++) { 117 scanf("%d %d %d",&point[i].x, &point[i].y, &point[i].z); 118 if(max_num < point[i].z) max_num = point[i].z; 119 if(min_num > point[i].z) min_num = point[i].z; 120 } 121 122 int low = min_num; int high = max_num; 123 while(low <= high) { 124 int mid = (low + high) >> 1; 125 //printf("*%d\n",mid); 126 if(check(mid)) 127 low = mid + 1; 128 else 129 high = mid - 1; 130 } 131 printf("%d\n",high + 1); 132 } 133 return 0; 134 }
poj2455Secret Milking Machine【二分 + 最大流】,布布扣,bubuko.com
poj2455Secret Milking Machine【二分 + 最大流】
原文:http://www.cnblogs.com/zhanzhao/p/3755754.html