URAL - 1774 最大流

Barber of the Army of Mages

Description

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”.

Example:

input:

3 2
1 3
1 3
1 3

output:

Yes
1 2
1 3
2 3

input:

2 1
1 3
1 3

output:

No

题意: 有n个顾客,每个顾客需要打理两次胡须。理发师每一秒可以给k个顾客打理,同时每个顾客必须在x到x+y-1秒内打理完毕,问能否在限制条件下让所有顾客都打理完毕。若不可以则直接输出No,若可以则输出Yes并输出每个顾客第一次和第二次打理的时间点。

题解
S - 顾客 (流量为2) : 每个顾客需要打理两次
顾客 - 时间点 (流量为1) :一个顾客在一个时间点最多打理一次
时间点 - T (流量为k) :理发师一秒可以给k个顾客打理

建完图跑dinic求最大流即可,若可以流n*2到T即为Yes,并根据残量网络输出路径。

ACODE:

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MX = 2e5 + 7;
const int INF = 0x3f3f3f3f;
int S,T,n,k;
struct Dinic
{
	int hcnt,head[MX];
	int path[MX][2];
	int dis[MX],cur[MX];
	struct Edge
	{
		int v,w,next;
	}e[MX << 1];
	void add(int u,int v,int w){
		e[hcnt].v = v;e[hcnt].w = w;e[hcnt].next = head[u];head[u] = hcnt++;
		e[hcnt].v = u;e[hcnt].w = 0;e[hcnt].next = head[v];head[v] = hcnt++;
	}
	void init(){
		hcnt = 0;
		for(int i = S;i <= T;++i) head[i] = -1;
	}
	bool bfs(){
		for(int i = S;i <= T;++i) dis[i] = 0;
		queue<int>q;
		q.push(S);dis[S] = 1;
		while(!q.empty()){
			int u = q.front();q.pop();
			for(int i = head[u];~i;i = e[i].next){
				int v = e[i].v;
				int w = e[i].w;
				if(w <= 0 || dis[v]) continue;
				dis[v] = dis[u] + 1;
				q.push(v);
				if(v == T) return 1;
			}
		}
		return dis[T];
	}
	int dfs(int u,int in){
		if(u == T || !in) return in;
		int out = 0;
		int res = in;
		for(int i = head[u];~i;i = e[i].next){
			cur[u] = i;
			int v = e[i].v,w = e[i].w;
			if(w <= 0 || dis[v] != dis[u] + 1) continue;
			int flow = dfs(v,min(res,w));
			e[i].w -= flow;e[i^1].w += flow;
			res -= flow;out += flow;
			if(res <= 0) break;
		}
		if(!in) dis[u] = -2;
		return out;
	}
	//print the answer
	void print(){
		for(int i = 2000 + 1;i <= 2000 + n;++i){
			int p = 0;
			for(int j = head[i];~j;j = e[j].next){
				int v = e[j].v,w = e[j^1].w;
				//printf("u = %d v = %d\n", i,v);
				if(!w || v < 1 || v > 2000) continue;
				path[i][p++] = v-1;
			}
			//因为链式前向星是倒叙遍历所以要反一下
			printf("%d %d\n",path[i][1],path[i][0]);
		}
	}
	int sol(){
		int res = 0;
		while(bfs()){
			for(int i = S;i <= T;++i) cur[i] = head[i];
			res += dfs(S,INF);
		}
		return res;
	}
}dinic;

//Hint:
//time 1 ~ 2000
//man 2001 ~ 2000 + n
//S = 0 T = 2001 + n

int main()
{
	scanf("%d%d",&n,&k);
	S = 0,T = 2001 + n;
	dinic.init();
	for(int i = 1;i <= n;++i){
		dinic.add(S,2000 + i,2);
		int l,r;scanf("%d%d",&l,&r);
		for(int j = l;j <= l + r - 1;++j){
			dinic.add(2000 + i,j+1,1);
		}
	}
	for(int i = 1;i <= 2000;++i){
		dinic.add(i,T,k);
	}
	int res = dinic.sol();
	if(res == n + n) printf("Yes\n");
	else{
		printf("No\n");
		return 0;
	}
	dinic.print();
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值