hdoj 3572 Task Schedule【建立超级源点超级汇点】
Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6000 Accepted Submission(s):
1922
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.
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.
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.
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
#define INF 0x7fffff
#define MAX 11000
#define MAXM 1001000
using namespace std;
struct node
{
int from,to,cap,flow,next;
}edge[MAXM];
int ans,head[MAX];
int cur[MAX];
int vis[MAX];
int dis[MAX];
int sum,n,m;
int sec;//超级汇点
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
node E1={u,v,w,0,head[u]};
edge[ans]=E1;
head[u]=ans++;
node E2={v,u,0,0,head[v]};
edge[ans]=E2;
head[v]=ans++;
}
void getmap()
{
int i,j,last=-1;
sum=sec=0;
int bt,et,time;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&time,&bt,&et);
sum+=time;
add(0,i,time);//超级源点连接第i件任务
for(j=bt;j<=et;j++)
add(i,n+j,1);//将每件任务与完成这件任务所需要的时间段内的每一天连接
last=max(last,et);
}
sec=n+last+1;
for(i=1;i<=sec;i++)
add(n+i,sec,m);//将所有的时间段内的点指向超级汇点
}
int bfs(int beg,int end)
{
int i;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
queue<int>q;
while(!q.empty())
q.pop();
vis[beg]=1;
dis[beg]=0;
q.push(beg);
while(!q.empty())
{
int u=q.front();
q.pop();
for(i=head[u];i!=-1;i=edge[i].next)//遍历所有的与u相连的边
{
node E=edge[i];
if(!vis[E.to]&&E.cap>E.flow)//如果边未被访问且流量未满继续操作
{
dis[E.to]=dis[u]+1;//建立层次图
vis[E.to]=1;//将当前点标记
if(E.to==end)//如果当前点搜索到终点则停止搜索 返回1表示有从原点到达汇点的路径
return 1;
q.push(E.to);//将当前点入队
}
}
}
return 0;//返回0表示未找到从源点到汇点的路径
}
int dfs(int x,int a,int end)//把找到的这条边上的所有当前流量加上a(a是这条路径中的最小残余流量)
{
//int i;
if(x==end||a==0)//如果搜索到终点或者最小的残余流量为0
return a;
int flow=0,f;
for(int& i=cur[x];i!=-1;i=edge[i].next)//i从上次结束时的弧开始
{
node& E=edge[i];
if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)//如果
{//bfs中我们已经建立过层次图,现在如果 dis[E.to]==dis[x]+1表示是我们找到的路径
//如果dfs>0表明最小的残余流量还有,我们要一直找到最小残余流量为0
E.flow+=f;//正向边当前流量加上最小的残余流量
edge[i^1].flow-=f;//反向边
flow+=f;//总流量加上f
a-=f;//最小可增流量减去f
if(a==0)
break;
}
}
return flow;//所有边加上最小残余流量后的值
}
int Maxflow(int beg,int end)
{
int flow=0;
while(bfs(beg,end))//存在最短路径
{
memcpy(cur,head,sizeof(head));//复制数组
flow+=dfs(beg,INF,end);
}
return flow;//最大流量
}
int main()
{
int t;
scanf("%d",&t);
int k=1;
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
printf("Case %d: ",k++);
if(sum==Maxflow(0,sec))
printf("Yes\n\n");
else
printf("No\n\n");
}
return 0;
}
hdoj 3572 Task Schedule【建立超级源点超级汇点】的更多相关文章
- hdu 3572 Task Schedule (dinic算法)
pid=3572">Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 图论--网络流--最大流 HDU 3572 Task Schedule(限流建图,超级源汇)
Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...
- 解题报告:hdu 3572 Task Schedule(当前弧优化Dinic算法)
Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...
- hdu 3572 Task Schedule(最大流&&建图经典&&dinic)
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 3572 Task Schedule(拆点+最大流dinic)
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu 3572 Task Schedule(最大流)2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
题意: 告诉我们有m个任务和k个机器.第i个任务需要ci天完成,最早从第ai天开始,最晚在第bi天结束.每台机器每天可以执行一个任务.问,是否可以将所有的任务都按时完成? 输入: 首行输入一个整数t, ...
- HDU 3572 Task Schedule (最大流)
C - Task Schedule Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu 3572 Task Schedule
Task Schedule 题意:有N个任务,M台机器.每一个任务给S,P,E分别表示该任务的(最早开始)开始时间,持续时间和(最晚)结束时间:问每一个任务是否能在预定的时间区间内完成: 注:每一个任 ...
- hdu 3572 : Task Schedule (网络流)
题目链接 题意: 有M个机器,N个任务 对第i个任务,需要在[Si,Ei]这段时间内恰有Pi天被process 每天最多有M个机器同时工作 每一天,一个任务若被process,那么它恰占用一个机器. ...
随机推荐
- Nhibernate 一对多,多对一配置
先来分析下问题,这里有两张表:Users(用户表) U和PersonalDynamic(用户动态表) PD,其中PD表的UserId对应U表的Id 如图: 现在映射这两张表: 如图: User.hbm ...
- MSCRM4.0如何使js事件在批量编辑表单中触发
MSCRM4.0如何使js事件在批量编辑表单中触发 MSCRM4.0如何使js事件在批量编辑表单中触发 MSCRM3.0我们可以通过在onload事件加入以下代码来控制某个属性为只读.crmForm. ...
- [转载]C# 多选功能(checkedListBox控件)
// 全选; private void btn_allSelected_Click(object sender, EventArgs e) { //this.CheckedListBox1.Check ...
- unity Android 打包后读取 xml 文件
原地址:http://www.cnblogs.com/wuzhang/p/wuzhang20140731.html 问题: 前天在做东西的过程中发现了一个让人很纠结的问题,为什么Unity 程序 ...
- 网上测试了很多关于PYTHON的WEBSOCKET样例,下面这个才成功了
这是最底层的, 嘿嘿,我 还是习惯搞个框架来实现急需要的功能... 这个东东玩得很有意思的.. 服务器端的代码: import simplejson import socket import sys ...
- 李洪强漫谈iOS开发[C语言-015]-变量的使用
- C#博文搜集
1. abstract (抽象类) 参考1 2. interface (接口) 参考1 3. 委托 C#委托学习
- OpenSSH for Windows,CopSSH
https://www.oschina.net/p/openssh+for+windows https://www.oschina.net/p/copssh
- 对于Unicode编码在js中和html中
1.对于Unicode在js中 var a="\u9102WQW121" 中"\"是需要转义的,直接在页面输出的效果
- UltraEdit (Ctrl + F) 查找、(Ctrl + R)替换功能失效
环境: Windows 7 Service Pack 1 X64 工具: UltraEdit Version 21 症状: UltraEdit (Ctrl + F) 查找.(Ctrl + R)替换功能 ...