满流判断+方案输出
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
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 ≤
n,
k ≤ 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
pi,
qi, 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;
}