Ural1774 Barber of the Army of Mages 最大流

满流判断+方案输出

n个客人,每个客人有一个等待的时间段,在这个时间段,他要被挂两次胡子。。。(O__O"…) ,刮胡子的人一分钟可以刮k个人。。。。如果可行,输出Yes然后按顺序输出第

i个人被刮胡子的两个时间,否则输出No

从0开始到最后一个客人离开的时间lt 每一个点与汇点ed连一条弧容量为k

源点st与每个人连一条弧,容量为2

第i个人有一个灯带的时间区间l[i],r[i],第i个人与这个区间内每一个时间连一条弧,容量为1

最后跑一次最大流,判断是否满流,然后如果可行就输出方案

1774. Barber of the Army of Mages

Time limit: 0.5 second
Memory limit: 64 MB
Petr, elected as a warlord of the army of mages, faced a challenging problem. All magicians recruited in the army had heavy beards, which were quite unacceptable for soldiers. Therefore, Petr ordered all recruits to shave their beards as soon as possible. Of course, all magicians refused to do it, referring to the fact they don't know any shaving spell. Fortunately, a magician Barberian agreed to shave all recruits.
Barberian can cast a “Fusion Power” spell which shaves beards of at most  k magicians in one minute. In order to achieve full effect every magician should be shaved twice: the first spell shaves close, the second spell shaves even closer. For each recruit Petr appointed a time when he should visit Barberian. Unfortunately, the discipline in the new army is still far from perfect, so every magician will come to Barberian in time, but everyone will wait for the shave until his patience is exhausted and will disappear after that.
Determine whether Barberian will be able to shave beards of all magicians before they disappear.

Input

The first line contains two space-separated integers  n and  k  (1 ≤  nk ≤ 100) , which are the number of recruits in the army and the number of magicians Barber can shave simultaneously. The  i-th of the following  n lines contains space-separated integers  ti and  si  (0 ≤  ti ≤ 1000; 2 ≤  si ≤ 1000) , which are the time in minutes, at which the  i-th magician must come to Barberian, and the time in minutes he is ready to spend there, including shaving time.

Output

If Barberian is able to shave beards of all magicians, output “Yes” in the first line. The  i-th of the following  n lines should contain a pair of integers  piqi, which are the moments at which Barberian should cast the spell on the  i-th magician  ( ti ≤  pi <  qi ≤  ti +  si − 1) . If at least one magician disappears before being completely shaved, output a single word “No”.

Samples

input output
3 2
1 3
1 3
1 3

Yes
1 2
1 3
2 3
2 1
1 3
1 3
No


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define MAXN 2200
#define INF 0xFFFFFF

struct edge
{
	int to,c,next;
};

edge e[999999];
int que[MAXN*100];
int dis[MAXN];
int pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed;
int maxflow;
int en;
int n,k,lt;

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=head[b];
	head[b]=en++;
}

bool bfs()
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false;
}

int update()
{
	int p,flow=INF;
    for (int i=pre[ed];i!=-1;i=pre[i])
		if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;
    for (int i=pre[ed];i!=-1;i=pre[i])
		e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;
    maxflow+=flow;
    return p;
}

void dfs()
{
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));
    for(int i=st,j;i!=-1;)
    {
        int flag=false;
        for(int k=head[i];k!=-1;k=e[k].next)
          if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )
          {
                pre[j]=i;
				head2[i]=k;
				i=j;
				flag=true;
                if(i==ed)
					i=update();
                if(flag)
					break;
          }
        if (!flag) dis[i]=-1,i=pre[i];
    }
}

int dinic()
{
	int ans=0;
	maxflow=0;
	while(bfs())
		dfs();
	return maxflow;
}


int l[MAXN],r[MAXN];

void build()
{
    memset(head,-1,sizeof(head)),en=0;
    st=0,ed=n+lt+1+1;
    for(int i=1;i<=n;i++)
        add(st,i,2);
    for(int i=n+1;i<=n+1+lt;i++)
        add(i,ed,k);
    for(int i=0;i<n;i++)
        for(int j=r[i];j>=l[i];j--)
            add(i+1,n+1+j,1);
}

void output(int x)
{
    int cnt=1;
    for(int i=head[x];i!=-1;i=e[i].next)
    {
        if(e[i].c==0)
        {
            int v=e[i].to;
            v-=(n+1);
            printf("%d%s",v,cnt!=2?" ":"\n");
            cnt++;
        }
        if(cnt==3)
            break;
    }
}


int main()
{
	while(~scanf("%d%d",&n,&k))
    {
        int s,t;
        lt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&s,&t);
            l[i]=s,r[i]=s+t-1;
            lt=max(lt,r[i]);
        }
        build();
        if(dinic()!=n*2)
            printf("No\n");
        else
        {
            printf("Yes\n");
            for(int i=1;i<=n;i++)
                output(i);
        }

    }
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
最新发布
05-26

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值