(intermediate) 网络流(时序模型+离散化) UVA 11167 - Monkeys in the Emei Mountain

Problem C
Monkeys in the Emei Mountain
Input:
Standard Input

Output: Standard Output

 

Xuexue is a pretty monkey living in the Emei mountain. She is extremely thirsty during time 2 and time 9 everyday, so she must drink 2 units water during this period. She may drink water more than once, as long as the total amount of water she drinks is exactly 2 - she never drinks more than she needs.  Xuexue drinks 1 unit water in one time unit, so she may drinks from time 2 to time 4, or from 3 to 5, … , or from 7 to 9, or even drinks twice: first from 2 to 3, then from 8 to 9. But she can't drink from 1 to 3 since she's not thirsty at time 1, and she can't drink from 8 to 10, since she must finish at time 9.

 

There are many monkeys like Xuexue: we use a triple (v, a, b) to describe a monkey who is thirsty during time a and b, and must drink exactlyv units of water during that period. Every monkey drinks at the same speed (i.e. one unit water per unit time).

 

Unfortunately, people keep on doing something bad on the environment in Emei Mountain. Eventually, there are onlyone unpolluted places for monkeys to drink. Further more, the place is so small that at mostm monkeys can drink water together. Monkeys like to help each other, so they want to find a way to satisfy all the monkeys' need. Could you help them?

 

Input

The input consists of several test cases. Each case contains two integers n and m (1 £ n £ 100, 1 £ m £ 5), followed by n lines of three integer numbers (v, a, b), where 0 £v, a, b £ 50,000, a < b, v £ b -a. The last test case is followed by a single zero, which should not be processed. There are at most 50 test cases.

 

Output

For each test case, print the case number and whether there is a solution. If there is a solution, the followingn lines describe the solution: one monkey on a separate line. The first numberk means the monkey drinks water for k times. The following k pairs (ai, bi) means the monkey drinks fromai to bi (ai< bi). The pairs should be sorted in ascending order, andai should not be equal to ai+1 for 1£i £ k-1 (otherwise these two drinking periods could be combined). If more than one solution exists, any one is acceptable. Note that there should be exactly one space betweenk and pairs (ai,bi), but no space within each pair.

 

Sample Input                               Output for Sample Input

3 1

2 2 9

2 3 5

3 5 8

2 1

4 5 9

4 8 12

5 2

2 1 3

2 3 5

2 5 7

2 1 7

4 2 6

0

Case 1: Yes

2 (2,3) (8,9)

1 (3,5)

1 (5,8)

Case 2: No

Case 3: Yes

1 (1,3)

1 (3,5)

1 (5,7)

2 (1,2) (6,7)

1 (2,6)


Original Author: Dong Zhou

Adapted by Rujia Liu (Problem description, Special judge)

Alternative Solution: Huayang Guo

 



题意:有n个猴子,他们要在某个时间段内喝够v个小时的水,但是池子一个小时只能容纳m个猴子,问怎么样安排能够满足所有猴子的需要。


思路:很容易想到,对于一个小时,它指向汇点的容量应该是m,而能够在这个时间喝水的猴子就连一条容量为1的边过来,套个网络流就能知道能不能满足以及猴子们是怎么选择时间的。但是时间最大是50000,这样子会超时。。。所以我们要进行优化,仔细思考后能发现,其实时间不需要每个时间都是一个小时一个小时的算,我们可以一段一段的算,所以我们一开始根据猴子的喝水时间对时间进行离散化处理,那么每个点代表的是这个时间点到下一个时间点之间这段时间,建一条容量为m*(时间差)的边到汇点。猴子们引一条容量为时间差的边到这个点,但是这样输出方案其实比较难搞的。。比较容易错。怎么输出呢?我们只需要按顺序的填满这个时间段,然后回到这个时间段的开头,继续往后填,这样的安排不会有矛盾。其实很难解释,可以看代码。


代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 50000+100+10;
const int inf = 1e8;
struct Edge
{
    int u,v;
    int flow,cap;
    Edge(int u,int v,int cap,int flow)
    :u(u),v(v),cap(cap),flow(flow) {}
};

vector<Edge> edges;
vector<int> G[maxn];

void add(int u,int v,int cap)
{
    edges.push_back(Edge(u,v,cap,0));
    edges.push_back(Edge(v,u,0,0));
    int m=edges.size();
    G[u].push_back(m-2);
    G[v].push_back(m-1);
}

struct ISAP
{
    int d[maxn],cur[maxn];
    int p[maxn],num[maxn];
    int n,s,t;
    void init(int n)
    {
        this->n=n;
    }
    void bfs()
    {
        queue<int> q;
        for(int i=0;i<n;++i)d[i]=inf;
        d[t]=0;
        q.push(t);
        while(q.size())
        {
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();++i)
            {
                Edge&e=edges[G[x][i]];
                if(e.cap>0||d[e.v]<=d[x]+1) continue;
                d[e.v]=d[x]+1;
                q.push(e.v);
            }
        }
    }
    int Augment()
    {
        int x=t,a=inf;
        while(x!=s)
        {
            Edge&e=edges[p[x]];
            a=min(a,e.cap-e.flow);
            x=e.u;
        }
        x=t;
        while(x!=s)
        {
            edges[p[x]].flow+=a;
            edges[p[x]^1].flow-=a;
            x=edges[p[x]].u;
        }
        return a;
    }

    int maxflow(int s,int t)
    {
        this->s=s,this->t=t;
        bfs();
        memset(cur,0,sizeof(cur));
        memset(num,0,sizeof(num));
        for(int i=0;i<n;++i)
            if(d[i]!=inf) ++num[d[i]];
        int x=s,flow=0;
        while(d[s]<n)
        {
            if(x==t)
            {
                flow+=Augment();
                x=s;
            }
            int ok=0;
            for(int i=cur[x];i<G[x].size();++i)
            {
                Edge&e=edges[G[x][i]];
                if(e.cap>e.flow&&d[x]==d[e.v]+1)
                {
                    cur[x]=i;
                    p[e.v]=G[x][i];
                    ok=1;
                    x=e.v;
                    break;
                }
            }
            if(!ok)
            {
                int m=n-1;
                for(int i=0;i<G[x].size();++i)
                {
                    Edge&e=edges[G[x][i]];
                    if(e.cap>e.flow) m=min(m,d[e.v]);
                }
                if(--num[d[x]]==0) break;
                num[d[x]=m+1]++;
                cur[x]=0;
                if(x!=s) x=edges[p[x]].u;
            }
        }
        return flow;
    }
}solver;

int n,m,sum;
vector<int> T;
struct Monkey
{
    int v;
    int a,b;
}mk[110];

typedef vector<int>::iterator Iter;
void input()
{
    sum=0;
    T.clear();
    for(int i=0;i<n;++i)
    {
        scanf("%d%d%d",&mk[i].v,&mk[i].a,&mk[i].b);
        sum+=mk[i].v;
        T.push_back(mk[i].a);
        T.push_back(mk[i].b);
    }
    sort(T.begin(),T.end());
    Iter it=unique(T.begin(),T.end());
    T.erase(it,T.end());
}

int ID(int x)
{
    return lower_bound(T.begin(),T.end(),x)-T.begin();
}

void buildgraph()
{
    edges.clear();
    for(int i=0;i<maxn;++i) G[i].clear();
    for(int i=0;i<n;++i)
    {
        add(0,i+1,mk[i].v);
        int a=ID(mk[i].a),b=ID(mk[i].b);
        for(int j=a;j<b;++j)
        {
            int diff=T[j+1]-T[j];
            add(i+1,n+1+j,diff);
        }
    }
    for(int j=0;j+1<T.size();++j)
    {
        int diff=T[j+1]-T[j];
        add(n+1+j,n+T.size()+1,m*diff);
    }
}

int s[maxn];

void solve()
{
    solver.init(1+n+T.size()+1);
    int ans=solver.maxflow(0,n+T.size()+1);
    if(ans!=sum)
    {
        printf("No\n");
        return;
    }
    printf("Yes\n");
    for(int j=0;j<T.size();++j)
        s[j]=T[j];
    for(int i=1;i<=n;++i)
    {
        vector<int> ret;
        for(int j=0;j<G[i].size();++j)
        {
            Edge&e=edges[G[i][j]];
            if(e.flow<=0) continue;
            int x=e.v-n-1;
      //      cout<<x<<endl;
            ret.push_back(s[x]);
            ret.push_back(min(T[x+1],s[x]+e.flow));
            s[x]+=e.flow;
            if(s[x]>=T[x+1])
            {
                s[x]=T[x]+s[x]-T[x+1];
                if(s[x]>T[x])
                {
                    ret.push_back(T[x]);
                    ret.push_back(s[x]);
                }
            }
        }
        sort(ret.begin(),ret.end());
        for(int j=0;j+1<ret.size();)
        {
            if(ret[j]==ret[j+1])
            {
                ret.erase(ret.begin()+j);
                ret.erase(ret.begin()+j);
            }
            else ++j;
        }
        while(ret.size()%2);
        printf("%d",ret.size()/2);
        for(int j=0;j<ret.size();j+=2)
            printf(" (%d,%d)",ret[j],ret[j+1]);
        printf("\n");
    }
}

int main()
{
   // freopen("in.txt","r",stdin);
    int Cas=0;
    while(scanf("%d",&n)&&n)
    {
        scanf("%d",&m);
        input();
        buildgraph();
        ++Cas;
        printf("Case %d: ",Cas);
        solve();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值