Question link:Http://poj.org/problem? Id = 3281
Theme: There are some cows, a pile of food, and a pile of drinks. If a cow eats a food and drinks a drink, it is satisfying. In addition, the ox is fond of certain foods and drinks and asks how many cows are satisfied at most.
Solutions:
There is no matching maximum flow question for the fee.
At the beginning, I thought so. S-> ox-> food-> drinks-> T, and cap are all 1, aha. It's easy.
This is wrong. Because the ox is picky, suppose the OX 1 prefers food 1 rather than drink 1, then the food 1 and drink 1 are not connected?
Don't connect. What about other cows?
What should I do with niu 1?
You cannot create a chart in this way. Then we changed it to s-> food-> ox-> drinks-> T, which solved the problem of ambiguous relationship between food and drinks.
But the new problem comes again. Suppose there is food 1> OX 1> drink 1, then there will be food 2> OX 1> drink 2, in this way, a cow swallowed up more food and drinks.
The solution is to split the point and insert the ox into the ox-> ox, Cap = 1, so that a cow will only be used once.
The final scheme for creating images: S-> food-> ox-> drinks-> T.
#include "cstdio"#include "vector"#include "cstring"#include "queue"using namespace std;#define maxn 405#define inf 100000000struct Edge{ int from,to,cap,flow; Edge(int FROM,int TO,int CAP,int FLOW):from(FROM),to(TO),cap(CAP),flow(FLOW) {}};int d[maxn],p[maxn],gap[maxn],cur[maxn];bool vis[maxn];vector<int> G[maxn],food[105],drink[105];vector<Edge> edges;void addedge(int from,int to,int cap){ edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0)); int m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1);}void bfs(int s,int t){ memset(vis,false,sizeof(vis)); memset(d,0,sizeof(d)); memset(p,0,sizeof(p)); d[t]=0;vis[t]=true; queue<int> Q;Q.push(t); while(!Q.empty()) { int u=Q.front();Q.pop(); for(int v=0;v<G[u].size();v++) { Edge e=edges[G[u][v]^1]; if(!vis[e.from]&&e.cap>e.flow) { vis[e.from]=true; d[e.from]=d[u]+1; Q.push(e.from); } } }}int augment(int s,int t){ int x=t,a=inf; while(x!=s) { Edge e=edges[p[x]]; a=min(a,e.cap-e.flow); x=e.from; } x=t; while(x!=s) { edges[p[x]].flow+=a; edges[p[x]^1].flow-=a; x=edges[p[x]].from; } return a;}int maxflow(int s,int t){ int flow=0,u=s; bfs(s,t); memset(gap,0,sizeof(gap)); memset(cur,0,sizeof(cur)); for(int i=0;i<=t;i++) gap[d[i]]++; while(d[s]<t+1) { if(u==t) { flow+=augment(s,t); u=s; } bool flag=false; for(int v=cur[u];v<G[u].size();v++) //Advance { Edge e=edges[G[u][v]]; if(e.cap>e.flow&&d[u]==d[e.to]+1) { flag=true; p[e.to]=G[u][v]; cur[u]=v; u=e.to; break; } } if(!flag) //Retreat { int m=t+1; for(int v=0;v<G[u].size();v++) { Edge e=edges[G[u][v]]; if(e.cap>e.flow) m=min(m,d[e.to]); } if(--gap[d[u]]==0) break; gap[d[u]=m+1]++; cur[u]=0; if(u!=s) u=edges[p[u]].from; } } return flow;}int main(){ int N,M,K,f,d,t; while(scanf("%d%d%d",&N,&M,&K)!=EOF) { for(int i=1;i<=M;i++) addedge(0,i,1); //S-food for(int i=1;i<=K;i++) addedge(i+M+N*2,K+M+N*2+1,1); //drink-T for(int i=1;i<=N;i++) { addedge(i+M,i+M+N,1); //cow-cow‘ scanf("%d%d",&f,&d); for(int j=1; j<=f; j++) { scanf("%d",&t); addedge(t,i+M,1); //food-cow } for(int j=1; j<=d; j++) { scanf("%d",&t); addedge(i+M+N,t+M+2*N,1); //cow‘-drink } } printf("%d\n",maxflow(0,2*N+M+K+1)); }}
| 13456393 |
Neopenx |
3281 |
Accepted |
320 k |
16 Ms |
C ++ |
3222b |
00:28:49 |
Poj 3281 (max stream + match + split point)