A - Barber of the Army of Mages
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87643#problem/A

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

Sample Input

3 2
1 3
1 3
1 3

Sample Output

Yes
1 2
1 3
2 3

HINT

题意

有n个顾客,每个顾客需要理2次胡须

每一秒,理发师可以给k个人理发

然后每个顾客必须在x秒到x+y-1秒内理完

然后让你构造出一种解

题解

网络流,贪心的话就走远了……

S-2-顾客-1-天数-k-T

然后跑一发最大流就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 5000
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** namespace NetFlow
{
const int MAXN=,MAXM=,inf=1e9;
vector<int> Q[];
struct Edge
{
int v,c,f,nx;
Edge() {}
Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
} E[MAXM];
int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;
void init(int _n)
{
N=_n,sz=; memset(G,-,sizeof(G[])*N);
}
void link(int u,int v,int c)
{
E[sz]=Edge(v,c,,G[u]); G[u]=sz++;
E[sz]=Edge(u,,,G[v]); G[v]=sz++;
}
int ISAP(int S,int T)
{//S -> T
int maxflow=,aug=inf,flag=false,u,v;
for (int i=;i<N;++i)cur[i]=G[i],gap[i]=dis[i]=;
for (gap[S]=N,u=pre[S]=S;dis[S]<N;flag=false)
{
for (int &it=cur[u];~it;it=E[it].nx)
{
if (E[it].c>E[it].f&&dis[u]==dis[v=E[it].v]+)
{
if (aug>E[it].c-E[it].f) aug=E[it].c-E[it].f;
pre[v]=u,u=v; flag=true;
if (u==T)
{
for (maxflow+=aug;u!=S;)
{
E[cur[u=pre[u]]].f+=aug;
E[cur[u]^].f-=aug;
}
aug=inf;
}
break;
}
}
if (flag) continue;
int mx=N;
for (int it=G[u];~it;it=E[it].nx)
{
if (E[it].c>E[it].f&&dis[E[it].v]<mx)
{
mx=dis[E[it].v]; cur[u]=it;
}
}
if ((--gap[dis[u]])==) break;
++gap[dis[u]=mx+]; u=pre[u];
}
return maxflow;
}
bool bfs(int S,int T)
{
static int Q[MAXN]; memset(dis,-,sizeof(dis[])*N);
dis[S]=; Q[]=S;
for (int h=,t=,u,v,it;h<t;++h)
{
for (u=Q[h],it=G[u];~it;it=E[it].nx)
{
if (dis[v=E[it].v]==-&&E[it].c>E[it].f)
{
dis[v]=dis[u]+; Q[t++]=v;
}
}
}
return dis[T]!=-;
}
int dfs(int u,int T,int low)
{
if (u==T) return low;
int ret=,tmp,v;
for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
{
if (dis[v=E[it].v]==dis[u]+&&E[it].c>E[it].f)
{
if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
{
ret+=tmp; E[it].f+=tmp; E[it^].f-=tmp;
}
}
}
if (!ret) dis[u]=-; return ret;
}
int dinic(int S,int T)
{
int maxflow=,tmp;
while (bfs(S,T))
{
memcpy(cur,G,sizeof(G[])*N);
while (tmp=dfs(S,T,inf)) maxflow+=tmp;
}
return maxflow;
}
void solve(int S,int T,int p,int n)
{
int C = dinic(S,T);
if(C!=p)
printf("No\n");
else
{
printf("Yes\n");
for(int i=;i<=n;i++)
{
int flag=;
for(int j=G[i+];j!=-;j=E[j].nx)
{
if(E[j].f==)
{
Q[i].push_back(E[j].v-);
}
}
printf("%d %d\n",Q[i][],Q[i][]);
}
}
}
} using namespace NetFlow;
int vis[];
int main()
{
init();
int n=read(),k=read();
for(int i=;i<=+n;i++)
link(,i,);
for(int i=;i<=n;i++)
{
int x=read(),y=read();
for(int j=x;j<=x+y-;j++)
{
link(i+,+j,);
if(!vis[j])
{
vis[j]=;
link(+j,,k);
}
}
}
solve(,,*n,n);
//cout<<dinic(1,4000)<<endl;
}

URAL 1774 A - Barber of the Army of Mages 最大流的更多相关文章

  1. Ural 1774 Barber of the Army of Mages 最大流

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1774 1774. Barber of the Army of Mages Time li ...

  2. ural 1090 In the Army Now

    http://acm.timus.ru/problem.aspx?space=1&num=1090 #include <cstdio> #include <cstring&g ...

  3. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  4. poj3069 Saruman's Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

  5. 1472. Martian Army

    http://acm.timus.ru/problem.aspx?space=1&num=1472 题目大意: 一颗树,根节点(1) 的值为 1.0,所有叶子节点的值为 0.0 ,其他节点值任 ...

  6. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  7. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  8. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  9. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

随机推荐

  1. 连接Excel时出现未指定的错误

    使用  strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended ...

  2. getDeclaredMethods()和getMethods()区别

    getDeclaredMethods()          返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共.保护.默认(包)访问和私有方法, ...

  3. .NET Remoting

    .NET Remoting   .NET Remoting是微软早期的分布式通信技术,虽然微软后来通过WCF通用基础通信框架整合掉了,但是通过回顾学习Remoting,反过来学习理解WCF也是很有帮助 ...

  4. POJ 3233 Matrix Power Serie

    题意:给一个n×n的矩阵A,求S = A + A2 + A3 + … + Ak. 解法:从式子中可得递推式S(n) = S(n - 1) + An,An = An-1×A,可得矩阵递推式 [S(n), ...

  5. Multiple View Geometry in Computer Vision Second Edition by Richard Hartley 读书笔记(一)

    var bdots = "../" var sequence = [ 'l1', 'l2', 'l3', 'l4' ]; Chapter1是个总览,引出了射影几何的概念,通过在欧式 ...

  6. rfid 门卡系统和人体红外感应开发

    今天忙了一天了,因为毕昇杯我发现如果不加把劲,可能寒假之前代码搞不出了,今天突击了两个模块,一个人体感应模块,和rfid刷卡模块,这两个模块谈不上自己编写代码,今天的任务也仅仅是看懂了代码,现在我总结 ...

  7. linux下简单文本处理

    1. 根据第二列的数据来确定第一列的值 awk '{if(a!=$0)i++;print i,$0;a=$0}' arr >arr.out 2. 补齐长度 seq arr.out|awk '{p ...

  8. 使用 CreateInstallMedia 创建 苹果系统安装U盘

    一般来说,从app store上面 下载下来的image位置,都是在 /Applications 下面 使用命令创建安装U盘,(备份一下命令,太长,记不住) sudo /Applications/In ...

  9. MFC学习知识点20160715

    1.   sizeof()  :返回所查询目标所占用字节数 _countof() :返回所查询目标所含有元素个数 _countof 是 C++中计算一个固定大小数组长度的宏,比如: T arr[10] ...

  10. 软件工程 --- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] [附加题]

    软件工程 --- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] [附加题] 首先,在分组之前,我和室友薛亚杰已经详细阅读了往届学长的博客,认为电梯调度 ...