Problem Description
Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days. 
Now she wonders whether he has a feasible schedule to finish all the tasks in time. She turns to you for help.
 
Input
On the first line comes an integer T(T<=20), indicating the number of test cases.
You are given two integer N(N<=500) and M(M<=200) on the first line of each test case. Then on each of next N lines are three integers Pi, Si and Ei (1<=Pi, Si, Ei<=500), which have the meaning described in the description. It is guaranteed that in a feasible schedule every task that can be finished will be done before or at its end day.
 
Output
For each test case, print “Case x: ” first, where x is the case number. If there exists a feasible schedule to finish all the tasks, print “Yes”, otherwise print “No”.
Print a blank line after each test case.
 
Sample Input
2
4 3
1 3 5
1 1 4
2 3 7
3 5 9
2 2
2 1 3
1 2 2
 
Sample Output
Case 1: Yes
Case 2: Yes
 
给N个任务,M台机器。每个任务有最早才能开始做的时间S,deadline E,和持续工作的时间P。每个任务可以分段进行,但是在同一时刻,一台机器最多只能执行一个任务. 问存不存在可行的工作时间。

由于时间<=500且每个任务都能断断续续的执行,那么我们把每一天时间作为一个节点来用网络流解决该题.
建图: 源点s(编号0), 时间1-500天编号为1到500, N个任务编号为500+1 到500+N, 汇点t(编号501+N).
源点s到每个任务i有边(s, i, Pi)
每一天到汇点有边(j, t, M) (其实这里的每一天不一定真要从1到500,只需要取那些被每个任务覆盖的每一天即可)
如果任务i能在第j天进行,那么有边(i, j, 1) 注意由于一个任务在一天最多只有1台机器执行,所以该边容量为1,不能为INF或M哦.
最后看最大流是否 == 所有任务所需要的总天数.

 #include <bits/stdc++.h>

 using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
struct Edge
{
int from,to,cap,flow;
Edge (int f,int t,int c,int fl)
{
from=f,to=t,cap=c,flow=fl;
}
};
struct Dinic
{
int n,m,s,t;
vector <Edge> edge;
vector <int> G[maxn];//存图
bool vis[maxn];//标记每点是否vis过
int cur[maxn];//当前弧优化
int dep[maxn];//标记深度
void init(int n,int s,int t)//初始化
{
this->n=n;this->s=s;this->t=t;
edge.clear();
for (int i=;i<n;++i) G[i].clear();
}
void addedge (int from,int to,int cap)//加边,单向边
{
edge.push_back(Edge(from,to,cap,));
edge.push_back(Edge(to,from,,));
m=edge.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs ()
{
queue<int> q;
while (!q.empty()) q.pop();
memset(vis,false,sizeof vis);
vis[s]=true;
dep[s]=;
q.push(s);
while (!q.empty()){
int u=q.front();
//printf("%d\n",u);
q.pop();
for (int i=;i<G[u].size();++i){
Edge e=edge[G[u][i]];
int v=e.to;
if (!vis[v]&&e.cap>e.flow){
vis[v]=true;
dep[v]=dep[u]+;
q.push(v);
}
}
}
return vis[t];
}
int dfs (int x,int mi)
{
if (x==t||mi==) return mi;
int flow=,f;
for (int &i=cur[x];i<G[x].size();++i){
Edge &e=edge[G[x][i]];
int y=e.to;
if (dep[y]==dep[x]+&&(f=dfs(y,min(mi,e.cap-e.flow)))>){
e.flow+=f;
edge[G[x][i]^].flow-=f;
flow+=f;
mi-=f;
if (mi==) break;
}
}
return flow;
}
int max_flow ()
{
int ans = ;
while (bfs()){
memset(cur,,sizeof cur);
ans+=dfs(s,inf);
}
return ans;
}
}dinic;
int full_flow;
int main()
{
int casee = ;
//freopen("de.txt","r",stdin);
int T;scanf("%d",&T);
while (T--){
int n,m;
full_flow = ;
scanf("%d%d",&n,&m);
int src = ,dst = ++n;
dinic.init(++n,src,dst);
bool vis[maxn];
memset(vis,false,sizeof vis);
for (int i=;i<=n;++i){
int p,s,e;
scanf("%d%d%d",&p,&s,&e);
full_flow+=p;
dinic.addedge(src,i+,p);
for (int j=s;j<=e;++j){
vis[j]=true;
dinic.addedge(i+,j,);
}
}
for (int i=;i<maxn;++i){
if (vis[i])
dinic.addedge(i,dst,m);
}
printf("Case %d: ",++casee);
if (dinic.max_flow()==full_flow){//dinic.max_flow()只能跑一遍
printf("Yes\n\n");
}
else
printf("No\n\n");
}
return ;
}

最新文章

  1. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 访问频率限制功能实现、防止黑客扫描、防止恶意刷屏
  2. selenium第二课(脚本录制seleniumIDE的使用)
  3. Redis学习笔记三:多机数据库的实现
  4. 【Android开发坑系列】之try-catch
  5. mysql 5.6密码强度插件使用
  6. if最简单的用法
  7. Xcode4.6 自制iOS可用的 Framework
  8. Android[安德鲁斯] 文本Air Video 远程播放电脑视频
  9. Unity Shader 知识点总结(一)
  10. 【分享】iTOP4412开发板-Bluetooth移植文档
  11. Fiddler的配置
  12. 包建强的培训课程(9):Android App性能优化
  13. Android中高级工程师面试题
  14. java ssh执行shell脚本
  15. 数据加密之RijndaelManaged加密
  16. switch-case最容易忽视的一点
  17. 20155318 2016-2017-2 《Java程序设计》第五周学习总结
  18. 第七届蓝桥杯个人赛省赛--C语言B组
  19. Git在windows环境下的使用教程
  20. 如何使用g++编译调用dll的c++代码

热门文章

  1. 威胁预警|Solr velocity模板注入远程命令执行已加入watchbog武器库,漏洞修补时间窗口越来越短
  2. 【Flutter学习】之button按钮
  3. mysql经典面试题之学生成绩表
  4. php关键字static使用
  5. 彻底搞定C指针-函数名与函数指针
  6. WebBrowser元素定位的常用方法
  7. Java反射实现Servlet处理多个请求--server分发
  8. HDU 1205 吃糖果 (鸽巢原理)
  9. UVA12589_Learning Vector
  10. txt文本程序 打开python文件 另存为原来的文件名,不能覆盖原来的文件解决