POJ3614 Sunscreen【最大流】

Time limit 1000 ms
Memory limit 65536 kB

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


题目分析
其实这题正解贪心,但我偏不写

超源向每个防晒霜连边,容量为防晒霜数量
每个奶牛向超汇连边,容量为1

对与每种防晒霜
向所有适合的奶牛连边,容量为1
跑最大流就好

另外这题当前弧优化竟然玄学快十倍


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int inf=1e9;
const int maxn=10010;
int n,m;
int s,t;
struct edge{int v,f,nxt;}E[5000010];
int head[maxn],tot=1;
struct node{int mi,mx;}cow[maxn];
int lev[maxn],maxf;
int cur[maxn];

void add(int u,int v,int f)
{
    E[++tot].nxt=head[u];
    E[tot].v=v; E[tot].f=f;
    head[u]=tot;
}

bool bfs()
{
    queue<int> q; q.push(s);
    memset(lev,-1,sizeof(lev)); lev[s]=0;
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=head[u];i;i=E[i].nxt)
        {
            int v=E[i].v;
            if(lev[v]==-1&&E[i].f)
            {
                lev[v]=lev[u]+1;
                if(v==t) return true;
                q.push(v);
            } 
        }
    }
    return false;
}

int dfs(int u,int cap)
{
    if(u==t||!cap) return cap;
    int flow=0;
    for(int i=cur[u];i;i=E[i].nxt)
    {
        int v=E[i].v;
        if(lev[v]==lev[u]+1&&E[i].f)
        {
            int f=dfs(v,min(E[i].f,cap-flow));
            E[i].f-=f; E[i^1].f+=f;
            flow+=f;
            if(f) cur[u]=i;
            if(flow==cap) return cap;
        } 
    }
    if(!flow) lev[u]=-1;
    return flow;
}

int main()
{
    //fread(ch,1,ch_top,stdin);
    n=read();m=read(); t=m+n+1;
    for(int i=1;i<=n;++i)
    {
        cow[i].mi=read(),cow[i].mx=read();
        add(i+m,t,1); add(t,i+m,0);//奶牛向超汇
    }
    for(int i=1;i<=m;++i)
    {
        int sp=read(),sum=read();
        add(s,i,sum); add(i,s,0);//超源向防晒霜

        for(int j=1;j<=n;++j)
        if(cow[j].mi<=sp&&sp<=cow[j].mx)
        add(i,j+m,1),add(j+m,i,0);//防晒霜向奶牛
    }

    while(bfs())
    {
        for(int i=0;i<=n+m+1;++i) cur[i]=head[i];
        maxf+=dfs(s,inf);
    }

    cout<<maxf;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/80628616