POJ 3614 Sunscreen 最大流、多重匹配、贪心

 
Sunscreen
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1966   Accepted: 688

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. Cowi has a minimum and maximumSPF rating (1 ≤ minSPFi ≤ 1,000;minSPFimaxSPFi ≤ 1,000) that will work. If theSPF rating is too low, the cow suffers sunburn; if theSPF 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 bottlei with anSPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottlei can covercoveri 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 andmaxSPFi
* Lines C+2..C+L+1: Line i+C+1 describes a sunscreen lotion bottlei 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 2
3 10
2 5
1 5
6 2
4 1

Sample Output

2

Source

USACO 2007 November Gold
 
 
数据弱用网络流水过,Dinic只用了125ms,二分图多重匹配也行。
建图:s到每头牛连边,边权为1
每个防晒霜到t连边,边权为使用次数
每头牛到能使用的防晒霜连边,边权为1
 
 
标准解是贪心:
题解:
贪心即可。
先将翻晒霜按spf值排序,从小到大处理。
对于当前处理的防晒霜,找到能使用它且spf上界最小的牛,将防晒霜供其使用。
因为上界越大,选择越多,在之后可能也可以找到匹配的防晒霜,
而由于从小到大处理,下界已经没有意义(不可能找到比当前spf更小的匹配),这是贪心的原则。
复杂度:O(l*logl+n*l)
转载自
http://blog.163.com/benz_/blog/static/18684203020115612011262/
 
最大流代码:
#include
#include
#define N 5005
#define M 2000005
#define inf 999999999
#include
using namespace std;

int n,m,s,t,num,adj[N],dis[N],q[N];
pair a[2505];
struct edge
{
	int v,w,pre;
}e[M];
void insert(int u,int v,int w)
{
	e[num]=(edge){v,w,adj[u]};
	adj[u]=num++;
	e[num]=(edge){u,0,adj[v]};//有向图
	adj[v]=num++;
}
int bfs()
{
	int i,x,v,head=0,tail=0;
	memset(dis,0,sizeof(dis));
	dis[s]=1;
	q[++tail]=s;
	while(head!=tail)
	{
		x=q[head=(head+1)%N];
		for(i=adj[x];~i;i=e[i].pre)
			if(e[i].w&&!dis[v=e[i].v])
			{
				dis[v]=dis[x]+1;
				if(v==t)
					return 1;
				q[tail=(tail+1)%N]=v;
			}
	}
	return 0;
}
int dfs(int x,int limit)
{
	if(x==t)
		return limit;
	int i,v,tmp,cost=0;
	for(i=adj[x];~i&&costu)
					break;
			insert(n+i,t,v);
		}
		printf("%d\n",Dinic());
	}
}

二分图多重匹配:
#include
#include
#include
#include
using namespace std;

int n,m,f[2505],size[2505];
vector adj[2505],mat[2505];
pair a[2505];
int dfs(int x)
{
	int i,j,k,u,v;
	for(i=0;iu)
					break;
		}
		printf("%d\n",ok());
	}
}

你可能感兴趣的:(ACM题解)