HDU 2883 kebab

关键是缩点。

//author: CHC
//First Edit Time:	2014-08-10 09:35
//Last Edit Time:	2014-08-10 14:58
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef long long LL;
const int MAXN=1e+4;
const int MAXM=1e+6;
//const int INF = INT_MAX;
//const LL LL_INF = LONG_LONG_MAX;
struct Edge
{
    int from,to;
    LL ci;
    int next;
    Edge(){}
    Edge(int _from,int _to,LL _ci,int _next):from(_from),to(_to),ci(_ci),next(_next){}
}e[MAXM];
int head[MAXN],tot;
int dis[MAXN];
int top,sta[MAXN],cur[MAXN];
int n,m;
inline void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
inline void AddEdge(int u,int v,LL ci0,LL ci1=0){
    e[tot]=Edge(u,v,ci0,head[u]);
    head[u]=tot++;
    e[tot]=Edge(v,u,ci1,head[v]);
    head[v]=tot++;
}
inline bool bfs(int st,int et){
    memset(dis,0,sizeof(dis));
    dis[st]=1;
    queue <int> q;
    q.push(st);
    while(!q.empty()){
        int now=q.front();
        q.pop();
        for(int i=head[now];i!=-1;i=e[i].next){
            int next=e[i].to;
            if(e[i].ci&&!dis[next]){
                dis[next]=dis[now]+1;
                if(next==et)return true;
                q.push(next);
            }
        }
    }
    return false;
}
LL Dinic(int st,int et){
    LL ans=0;
    while(bfs(st,et)){
        //printf("here\n");
        top=0;
        memcpy(cur,head,sizeof(head));
        int u=st,i;
        while(1){
            if(u==et){
                int pos=0;
                LL minn=e[sta[0]].ci;
                //printf("top:%d\n",top);
                for(i=0;i<top;i++)
                {
                    if(minn>e[sta[i]].ci){
                        minn=e[sta[i]].ci;
                        pos=i;
                    }
                    //printf("%d --> %d\n",e[sta[i]].from,e[sta[i]].to);
                }
                for(i=0;i<top;i++){
                    e[sta[i]].ci-=minn;
                    e[sta[i]^1].ci+=minn;
                }
                top=pos;
                u=e[sta[top]].from;
                ans+=minn;
                //printf("minn:%d\n\n",minn);
            }
            for(i=cur[u];i!=-1;cur[u]=i=e[i].next)
                if(e[i].ci&&dis[u]+1==dis[e[i].to])break;
            if(cur[u]!=-1){
                sta[top++]=cur[u];
                u=e[cur[u]].to;
            }
            else {
                if(top==0)break;
                dis[u]=0;
                u=e[sta[--top]].from;
            }
        }
    }
    return ans;
}
struct p0
{
    int si,ni,ei,ti;
}cs[MAXN];
struct p1
{
    int id,x;
    p1(){}
    p1(int _id,int _x):id(_id),x(_x){}
}a[MAXN*2];
bool cmp(p1 x,p1 y){
    return x.x<y.x;
}
set <int> has;
set <int> ::iterator it;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        init();
        has.clear();
        int tos=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d%d",&cs[i].si,&cs[i].ni,&cs[i].ei,&cs[i].ti);
            a[tos++]=p1(i,cs[i].si+1);
            a[tos++]=p1(i,cs[i].ei);
            has.insert(cs[i].si+1);
            has.insert(cs[i].ei);
        }
        sort(a,a+tos,cmp);
        int tos1=n+10;
        for(it=has.begin();it!=has.end();it++){
            //端点
            AddEdge(0,tos1,(LL)m);
            for(int i=1;i<=n;i++)
                if(cs[i].si+1<=*it && *it<=cs[i].ei){
                    AddEdge(tos1,i,(LL)m);
                }
            ++tos1;
        }
        //printf("tos:%d\n",tos);
        for(int i=0;i<tos;i++){
            //区间
            if(i==0||a[i].x==a[i-1].x)continue;
            //时间
            int val=a[i].x-a[i-1].x-1;
            if(!val)continue;
            AddEdge(0,tos1,(LL)m*val);
            for(int j=1;j<=n;j++)
                if(cs[j].si+1<=a[i-1].x && a[i].x<=cs[j].ei){
                    AddEdge(tos1,j,(LL)m*val);
                }
            ++tos1;
        }
        LL ans1,ans2=0;
        for(int i=1;i<=n;i++)
        {
            AddEdge(i,tos1,(LL)cs[i].ni*cs[i].ti);
            ans2+=cs[i].ni*cs[i].ti;
        }
        ans1=Dinic(0,tos1);
        //printf("%I64d\n",Dinic(0,tos1));
        //printf("%I64d %I64d\n",ans1,ans2);
        if(ans1==ans2)puts("Yes");
        else puts("No");
    }
    return 0;
}


你可能感兴趣的:(网络流)