hdu 3572 Task Schedule

来源:互联网 发布:如何用为知笔记做批注 编辑:程序博客网 时间:2024/04/28 06:02

http://acm.hdu.edu.cn/showproblem.php?pid=3572

这个就是要把每天每个任务拆开对待。然后建网络流,直接套模板就过了,一点压力都没有。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <vector>#define INF 0x7fffffff#define maxn 1005using namespace std;int vis[maxn],cur[maxn],d[maxn],s,m,n,t,tot,cnt;struct Edge{    int from,to,cap,flow;};vector<int>G[maxn];vector<Edge>edges;void init(){    for(int i=0;i<=cnt;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});    int m=edges.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}bool BFS(){    memset(vis,0,sizeof vis);    memset(d,0,sizeof d);    queue<int>Q;    Q.push(s);    d[s]=0;    vis[s]=1;    while(!Q.empty())    {        int x = Q.front();Q.pop();        int len = G[x].size();        for(int i=0;i<len;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){    if(x==t||a==0)return a;    int flow = 0,f;    int len = G[x].size();    for(int &i = cur[x];i<len ;i++)    {        Edge &e = edges[G[x][i]];        if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>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())    {        memset(cur,0,sizeof cur);        flow+=DFS(s,INF);        if(flow==tot)break;    }    return flow;}int main(){//    freopen("in.txt","r",stdin);    int T;scanf("%d",&T);    for(int cas=1;cas<=T;cas++)    {        init();        cnt = 501;int MAX=0,MIN=INF;s=0;        int cap,u,v;tot=0;        scanf("%d%d",&m,&n);        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&cap,&u,&v);            MAX=max(MAX,v);            MIN=min(MIN,u);            tot+=cap;            AddEdge(s,++cnt,cap);            for(int i=u;i<=v;i++)            {                AddEdge(cnt,i,1);            }        }        t=++cnt;        for(int i=MIN;i<=MAX;i++)        {            AddEdge(i,cnt,n);        }        int ans = Maxflow(s,t);        printf("Case %d: %s\n\n",cas,ans==tot?"Yes":"No");    }    return 0;}

0 0
原创粉丝点击