2017寒假集训 1-D
HDU 3572 Task Schedule
网络流
传送门:HustOJ
传送门:HDU
题意
有n个任务,m个机器。每个任务有三个参数 S E P,分别表示任务必须于起始时间S后发生,必须于完成时间E前完成,需要经过P时间做完。每个机器每天只能做一个任务,每个任务可以由不同的机器不连续的完成,但每个任务在同一时间只能在一台机器上进行。问m个机器能不能完成任务。
思路
果然网络流难得还是模型啊。。这题居然也能是网络流。。
超级源,超级汇。
超级源连边向每个任务,流量是事件的需要天数。满流意味任务可以做完。
事件连边向每个天的节点,流量是1。每个任务在一天只能由一个机器做。
天节点连边向超级汇,流量是机器数。表示每天每个做多做的任务数是机器数。
其实想想就很好明白了。。
最后Dinic,流要是等于每个任务所需天数和,就是yes,否则no。
代码
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
const int MAXN=1007;
const int oo=0x3f3f3f3f;
typedef long long LL;
struct Edge{
int from, to, cap, flow;//cap容量 flow流量
Edge(){}
Edge(int u, int v, int c, int f){ from=u;to=v;cap=c;flow=f; }
}e[MAXN];
vector<Edge> edges;//顺序的插入边
vector<int> G[MAXN];//保存边号
bool vis[MAXN];//BFS使用
int d[MAXN];
int cur[MAXN];
const int super_s=1001;
const int super_t=1002;
void init(int n)
{
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));//单向边第三个参数写0,双向边写cap
int t_m=edges.size();
G[from].push_back(t_m-2);
G[to].push_back(t_m-1);
}
bool BFS(int s, int t)
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty())
{
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)//残量网络
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a, int s, int t)
{
if(x==t||a==0) return a;
int flow=0, f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to, min(a, e.cap-e.flow), s, t))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(int s, int t)
{
int flow=0;
while(BFS(s, t))
{
memset(cur, 0, sizeof(cur));
flow+=DFS(s, oo, s, t);
}
return flow;
}
int main()
{
int T;
while(cin>>T)
{
for(int t=0;t<T;t++)
{
init(1005);
int n, m;
cin>>n>>m;
int summer=0;
for(int i=1;i<=n;i++)
{
int a, b, c;
cin>>a>>b>>c;
summer+=a;
AddEdge(super_s, i, a);
for(int j=b;j<=c;j++)
{
AddEdge(i, 500+j, 1);
}
}
for(int i=501;i<=1000;i++)
{
AddEdge(i, super_t, m);
}
int ma=Maxflow(super_s, super_t);
if(ma>=summer) printf("Case %d: Yes\n\n",t+1);
else printf("Case %d: No\n\n", t+1);
}
}
//system("pause");
return 0;
}