URAL 1774 A - Barber of the Army of Mages 最大流
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
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”.
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 最大流的更多相关文章
- 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 ...
- ural 1090 In the Army Now
http://acm.timus.ru/problem.aspx?space=1&num=1090 #include <cstdio> #include <cstring&g ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- poj3069 Saruman's Army
http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...
- 1472. Martian Army
http://acm.timus.ru/problem.aspx?space=1&num=1472 题目大意: 一颗树,根节点(1) 的值为 1.0,所有叶子节点的值为 0.0 ,其他节点值任 ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
随机推荐
- Oracle 课程二之Oracle数据库逻辑结构
完成本课程的学习后,您应该能够: •数据库总体的逻辑结构 •深入理解数据库最小存储单元block •理解行迁移和行链接 •理解高水位线 •Drop.truncate.delete区别 1.数据库的 ...
- Autofac 依赖注入 ASP.NET MVC5 插件机制中插件的简单实现
一.前言 由于项目业务复杂,创建了多个插件并把他们放在了不同的项目中,项目使用AutoFac做的IOC:但是主项目可以注入,插件注入失败, 没有为该对象定义无参数的构造函数.下面就一步一步注入插件项目 ...
- GitHub上README.md教程
最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编写README文件的同学们. README文件后缀名为md.md是markdown的缩写,markdown是一种编辑博客的语言 ...
- android中sqlite3常用命令
1)打开数据库 在adb shell模式下执行命令sqlite3 + 数据库名称,例如打开email中的EmailProvider.db数据库: 2)sqlite3特殊命令 大多数候,sqlite3读 ...
- hdu 4300(kmp)
题意:说实话这个题的题意还真的挺难懂的,我开始看了好久都没看懂,后来百度了下题意才弄懂了,这题的意思就是首先有一个字母的转换表,就是输入的第一行的字符串,就是'a'转成第一个字母,'b'转成转换表的第 ...
- [转] TreeList 当前节点图标和背景色设置
高原之上原文TreeList 选中节点时图标状态和背景色 // 给TreeList加SelectImage this.treelArea.SelectImageList = imglCustom; / ...
- 怎样在 Ubuntu 中修改默认程序
导读 作为一个新手,你需要知道如何在 Ubuntu 中修改任何默认程序,这也是我今天在这篇指南中所要讲的. 对于我来说,安装 VLC 多媒体播放器是安装完 Ubuntu 16.04 该做的事中最先做的 ...
- openfl关于windows平台编译报错解决办法
报错信息: 无法打开程序数据库“e:\newproj\mainclient\bin\windows\cpp\obj\obj\msvc-debug-ncxp\vc.pdb”:如果要将多个 CL.EXE ...
- (转载)OC学习篇之---类的初始化方法和点语法的使用
昨天介绍了OC中类的定义和使用,今天我们来继续学习类的初始化方法和点语法的使用. 一.首先来看一下类的初始化方法 在Java中我们知道一个每个类都有构造方法,这里的初始化方法就是和构造方法一个概念的, ...
- Memory Cache(内存缓存)
当Google测试了Google Search服务的可用性后,发现速度是最影响Web应用的可用性的因素之一.相对于作用相同但是速度慢的应用,用户更喜欢速度快的应用.多来年,Google已经掌握了如何使 ...