Educational Codeforces Round 8 F. Bear and Fair Set【最大流】

刚开始超时的建图
1. 先有s和t,s和余数建边,流量是n/5。
2. 对于i从1到b,i和余数建边,流量是1,将i和不超过i的最大限制建边,流量是1。
3. 之后再将限制之间相互建边。
4. 毫无疑问这个是超时的,而且也是不正确的,题目中每个限制区间是严格取的

看了题解以后
1. 我第一写的1到b的建边其实是没有太大意义的
2. 之后只需要将数轴分区间就好了,比如限制是[1,n1],[1,n2],我们只需要转换成:[1,n1]和[n1+1,n2]。然后对于每个区间计算各种余数有多少个,即为和前面的余数点的流量,最后每个区间再和t点连边,流量为这个区间要选择数字的个数

//poj 1273
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int N=30000;
const int inf=1<<24;

struct Edge
{
    int from,to,cap,flow;
};
vector<Edge>edges;
vector<int>G[N];
int s,t;
int vis[N];
int d[N];
int cur[N];

void AddEdge(int from,int to,int cap)
{
    Edge tp;
    tp.from=from,tp.to=to,tp.cap=cap,tp.flow=0;
    edges.push_back(tp);

    tp.from=to,tp.to=from,tp.cap=0,tp.flow=0;
    edges.push_back(tp);

    int g_size=edges.size();
    G[from].push_back(g_size-2);
    G[to].push_back(g_size-1);
}

bool BFS()
{
    memset(vis,0,sizeof(vis));
    queue<int>Q;
    Q.push(s);
    d[s]=0;
    vis[s]=1;
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        for(int i=0; i<G[x].size(); i++)
        {
            Edge &e=edges[G[x][i]];
            if(!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=1;
                d[e.to]=d[x]+1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}

int DFS(int x,int a)
{
    if(x==t||a==0) return a;
    int flow=0,f;
    for(int &i=cur[x]; i<G[x].size(); i++)
    {
        Edge &e=edges[G[x][i]];
        if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0)
        {
            e.flow+=f;
            edges[G[x][i]^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}

int Maxflow(int st,int ed)
{
    int flow=0;
    while(BFS())
    {
        memset(cur,0,sizeof(cur));
        flow+=DFS(st,inf);
    }
    return flow;
}
struct node
{
    int up,num,id;
} p[N];

int cmp(node a,node b)
{
    return a.up<b.up;
}

int main()
{
    int n,b,u,v,c,q,m[10];
    while(~scanf("%d%d%d",&n,&b,&q))
    {
        edges.clear();
        for(int i=0; i<N; i++) G[i].clear();
        for(int i=0; i<q; i++)
        {
            scanf("%d%d",&p[i].up,&p[i].num);
        }
        p[q].up=b;
        p[q].num=n;
        q++;
        s=0;
        t=q+5+1;
        sort(p,p+q,cmp);
        for(int i=0; i<5; i++)
        {
            m[i]=q+i+1;
            AddEdge(s,m[i],n/5);
        }
        for(int i=0; i<q; i++)
        {
            p[i].id=i+1;
             if(p[i].num>p[i+1].num&&i+1<q)
            {
                puts("unfair");
                return 0;
            }
        }

        AddEdge(p[0].id,t,p[0].num);
        for(int j=0;j<5;j++)
        {
            int n2=p[0].up/5;
            if(p[0].up%5>j) n2++;
            AddEdge(m[j],p[0].id,n2);
        }

        for(int i=1;i<q;i++)
        {
            AddEdge(p[i].id,t,p[i].num-p[i-1].num);
            //printf("%d %d %d\n",p[i].id,t,p[i].num);
            for(int j=0;j<5;j++)
            {
                int n1=p[i-1].up/5;
                int n2=p[i].up/5;
                if(p[i-1].up%5>j) n1++;
                if(p[i].up%5>j) n2++;
                AddEdge(m[j],p[i].id,n2-n1);
            }
        }
        //printf("%d\n",Maxflow(s,t));
        if(Maxflow(s,t)==n) puts("fair");
        else puts("unfair");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值