poj 3614(最大流)

来源:互联网 发布:java ioc容器 编辑:程序博客网 时间:2024/05/19 23:29
Sunscreen
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6682 Accepted: 2350

Description

To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi≤ maxSPFi ≤ 1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn't tan at all........

The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.

What is the maximum number of cows that can protect themselves while tanning given the available lotions?

Input

* Line 1: Two space-separated integers: C and L
* Lines 2..C+1: Line i describes cow i's lotion requires with two integers: minSPFi and maxSPFi 
* Lines C+2..C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri

Output

A single line with an integer that is the maximum number of cows that can be protected while tanning

Sample Input

3 23 102 51 56 24 1

Sample Output

2

题意:N头牛,第I头需要一个SPF的范围是MinSPF~MaxSPF,m个bottle,每个bottle能给C头牛提供定值为P的SPF,求最多有多少头牛可以得到合适的SPF.

解题思路:最大流,m个bottle与源点相连,容量为c,表示每个bottle能够提供给c头牛,n头牛与汇点连容量为1的边,限制了每头牛只能够一个bottle,每个bottle的SPF只要在牛的SPF范围内,就连一条容量为1的边,表示能提供1头牛使用。

这个题目有点卡模板:

#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn = 5005;const int inf = 0x3f3f3f3f;struct Edge{int to,next,flow;}edge[maxn*maxn];struct Node{int minSPF,maxSPF;}cow[maxn];int n,m,cnt,pre[maxn],layer[maxn];void addedge(int u,int v,int flow){edge[cnt].to = v;edge[cnt].flow = flow;edge[cnt].next = pre[u];pre[u] = cnt++;swap(u,v);edge[cnt].to = v;edge[cnt].flow = 0;edge[cnt].next = pre[u];pre[u] = cnt++;}bool bfs(int s,int t){queue<int> q;memset(layer,-1,sizeof(layer));layer[s] = 0;q.push(s);while(!q.empty()){int u = q.front();q.pop();if(u == t) return true;for(int i = pre[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(layer[v] == -1 && edge[i].flow > 0){layer[v] = layer[u] + 1;q.push(v);}}}return false;}int dfs(int u,int t,int maxflow){if(u == t || maxflow == 0) return maxflow;int uflow = 0;for(int i = pre[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(layer[v] == layer[u] + 1 && edge[i].flow > 0){int flow = min(maxflow - uflow,edge[i].flow);flow = dfs(v,t,flow);if(flow > 0){edge[i].flow -= flow;edge[i^1].flow += flow;uflow += flow;if(uflow == flow) break;}else layer[v] = -1;}}if(uflow == 0) layer[u] = -1;return uflow;}int dinic(int s,int t){int maxflow = 0;while(bfs(s,t) == true)maxflow += dfs(s,t,inf);return maxflow;}int main(){int s,t,w,c;while(scanf("%d%d",&n,&m)!=EOF){s = 0, t = n + m + 1;cnt = 0;memset(pre,-1,sizeof(pre));for(int i = 1; i <= n; i++){scanf("%d%d",&cow[i].minSPF,&cow[i].maxSPF);addedge(m+i,t,1);}for(int i = 1; i <= m; i++){scanf("%d%d",&w,&c);addedge(s,i,c);for(int j = 1; j <= n; j++)if(cow[j].minSPF <= w && w <= cow[j].maxSPF)addedge(i,m+j,1);}printf("%d\n",dinic(s,t));}return 0;}


0 0
原创粉丝点击