Task Schedule
http://acm.hdu.edu.cn/showproblem.php?pid=3572
题意:给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哦.
最后看最大流是否 == 所有任务所需要的总天数.
C++版本一
题解:Dinic算法+当前弧优化
当前弧优化:因为每次dfs找增广路的过程中都是从每个顶点指向的第1(编号为0)条边开始遍历的,而如果第1条边已达到满流,则会继续遍历第2条边....直至找到汇点,事实上这个递归的过程就造成了很多不必要浪费的时间,所以在dfs的过程中应标记一下每个顶点v当前能到达的第curfir[v]条边,说明顶点v的第0条边~第curfir[v]-1条边都已达满流或者流不到汇点,这样时间复杂度就大大降低了。注意:每次bfs给图重新分层次时,需要清空当前弧数组cirfir[],表示初始时从每个顶点v的第1(编号为0)条边开始遍历。
/*
*@Author: STZG
*@Language: C++
*/
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
#include<ctime>
//#define DEBUG
#define RI register int
#define endl '\n'
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int s,t,n,m,k,p,l,r,u,v,w,c2;
int ans,cnt,flag,temp,sum;
int dis[N],cur[N];
struct node{
int u,v,c;
node(){};
node(int form,int to,int cap):u(form),v(to),c(cap){}
};
vector<node>edge;
vector<int> G[N];
void Addedge(int u,int v,int cap){
edge.push_back({u,v,cap});
edge.push_back({v,u,0});
int sz=edge.size();
G[u].push_back(sz-2);
G[v].push_back(sz-1);
}
bool bfs(int u){
memset(dis,-1,sizeof(dis));
dis[u]=0;
queue<int>q;
q.push(u);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=0;i<G[u].size();i++){
node e=edge[G[u][i]];//cout<<u<<" "<<e.v<<endl;
if(dis[e.v]<0&&e.c>0){
dis[e.v]=dis[u]+1;
q.push(e.v);
}
}
}
return dis[t]>0;
}
int dfs(int u,int flow){
if(u==t)
return flow;
int now;
for(int &i=cur[u];i<G[u].size();i++){
node e=edge[G[u][i]];
if(e.c>0&&dis[u]+1==dis[e.v]&&(now=dfs(e.v,min(flow,e.c)))){
edge[G[u][i]].c-=now;
edge[G[u][i]^1].c+=now;
return now;
}
}
return 0;
}
void dinic(){
while(bfs(s)){
int res=0;
memset(cur,0,sizeof(cur));
while(res=dfs(s,INF)){
ans+=res;
}
}
}
void init(){
s=0;t=n+500+1;
for(int i=s;i<=t;i++)G[i].clear();
edge.clear();
ans=0;
sum=0;
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
scanf("%d",&k);
int T=0;
while(k--){
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=500;i++)Addedge(i+n,t,m);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&p,&u,&v);
Addedge(s,i,p);
sum+=p;
for(int j=u;j<=v;j++)
Addedge(i,j+n,1);
}
dinic();
//cout<<ans<<endl;
cout<<"Case "<<++T<<": "<<(ans==sum?"Yes":"No")<<endl;
cout<<endl;
}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}
C++版本二
题解:ISAP算法
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
#define MAXN 500
#define MAXE 500002
#define INF 0x7ffffff
int ne,nv,tmp,s,t,index;
struct Edge{
int next,pair;
int v,cap,fLow;
}edge[MAXE];
int net[MAXN];
int maxday;
int ISAP()
{
int numb[MAXN],dist[MAXN],curedge[MAXN],pre[MAXN];
int cur_fLow,max_fLow,u,tmp,neck,i;
memset(dist,0,sizeof(dist));
memset(numb,0,sizeof(numb));
memset(pre,-1,sizeof(pre));
for(i = 1 ; i <= nv ; ++i)
curedge[i] = net[i];
numb[nv] = nv;
max_fLow = 0;
u = s;
while(dist[s] < nv)
{
if(u == t)
{
cur_fLow = INF;
for(i = s; i != t;i = edge[curedge[i]].v)
{
if(cur_fLow > edge[curedge[i]].cap)
{
neck = i;
cur_fLow = edge[curedge[i]].cap;
}
}
for(i = s; i != t; i = edge[curedge[i]].v)
{
tmp = curedge[i];
edge[tmp].cap -= cur_fLow;
edge[tmp].fLow += cur_fLow;
tmp = edge[tmp].pair;
edge[tmp].cap += cur_fLow;
edge[tmp].fLow -= cur_fLow;
}
max_fLow += cur_fLow;
u = neck;
}
/* if .... eLse ... */
for(i = curedge[u]; i != -1; i = edge[i].next)
if(edge[i].cap > 0 && dist[u] == dist[edge[i].v]+1)
break;
if(i != -1)
{
curedge[u] = i;
pre[edge[i].v] = u;
u = edge[i].v;
}else{
if(0 == --numb[dist[u]]) break;
curedge[u] = net[u];
for(tmp = nv,i = net[u]; i != -1; i = edge[i].next)
if(edge[i].cap > 0)
tmp = tmp<dist[edge[i].v]?tmp:dist[edge[i].v];
dist[u] = tmp + 1;
++numb[dist[u]];
if(u != s) u = pre[u];
}
}
return max_fLow;
}
void addedge(int u,int v,int f)
{
edge[index].next = net[u];
edge[index].v = v;
edge[index].cap = f;
edge[index].fLow = 0;
edge[index].pair = index+1;
net[u] = index++;
edge[index].next = net[v];
edge[index].v = u;
edge[index].cap = 0;
edge[index].fLow = 0;
edge[index].pair = index-1;
net[v] = index++;
}
int main()
{
int i,j,np,nc,m,n;
int a,b,d,k,vaL;
int tt;
scanf("%d",&tt);
for(int cas=1;cas<=tt;++cas)
{
int cases=1;
int maxval=0;
maxday=0;
scanf("%d%d",&n,&m);
index=0;//index从0开始扫
s = 0;
t = 0;
memset(net,-1,sizeof(net));
for(i=1;i<=n;i++)
{
int pi,si,ei;
scanf("%d%d%d",&pi,&si,&ei);
maxday=max(maxday,ei);
maxval+=pi;
addedge(s,i,pi);
for(j=si;j<=ei;j++)
addedge(i,n+j,1);
}
t=n+maxday+1;
nv=t+1;
for(i=1;i<=maxday;i++)
addedge(i+n,t,m);
int ans=ISAP();
if (ans == maxval)
printf("Case %d: Yes\n\n", cas);
else printf("Case %d: No\n\n", cas);
}
return 0;
}