• hdu 3572 task schedule 网络流


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

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;

    const int MAXN = 3000;
    const int MAXM = 210000;
    const int INF =0x7ffffff;
    struct Edge
    {
    int st, ed;
    int next;
    int flow;
    }edge[MAXM];
    int head[MAXN];
    int n,m,S,T,tot,day;


    int seq[MAXM], sl;

    int ans = 0;
    void add_edge(int u, int v, int w)
    {
    edge[tot].flow = w;
    edge[tot].st = u;
    edge[tot].ed = v;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].flow = 0;
    edge[tot].st = v;
    edge[tot].ed = u;
    edge[tot].next = head[v];
    head[v] = tot++;
    }

    void Build()
    {
    int i,j,p,s,e;
    scanf("%d%d",&n,&m);
    S=0,T=500+n+1; tot=0; day=0;
    memset(head,-1,sizeof(head));
    for(i=1;i<=n;++i)
    {
    scanf("%d%d%d",&p,&s,&e);
    day+=p;
    add_edge(0,i,p);
    for(j=s;j<=e;++j) add_edge(i,n+j,1);
    }
    for(i=1;i<=500;++i) add_edge(n+i,T,m);
    }

    //
    int d[MAXN];
    bool dinic_bfs()
    {
    int i, j;
    memset(d, -1, sizeof(d));
    int que[MAXN*10], rear = 1;
    que[0] = S; d[S] = 0;
    for(i = 0; i < rear; i++)
    for(j = head[que[i]]; j != -1; j = edge[j].next)
    if(d[edge[j].ed] == -1 && edge[j].flow > 0)
    {
    d[edge[j].ed] = d[que[i]]+1;
    que[rear++] = edge[j].ed;
    }
    return d[T] >= 0;
    }

    int dinic_dfs()
    {
    int src=S,dest=T;
    int stk[MAXN], top = 0;
    int ret = 0, cur, ptr, pre[MAXN], minf, i;
    bool del[MAXN];
    memset(del, false, sizeof(del));

    stk[top++] = src;
    pre[src] = src; cur = src;
    while(top)
    {
    while(cur != dest && top)
    {
    for(i = head[cur]; i != -1; i = edge[i].next)
    {
    if(d[edge[i].ed] == d[cur]+1 && edge[i].flow > 0 && !del[edge[i].ed])
    {
    stk[top++] = edge[i].ed;
    cur = edge[i].ed;
    pre[edge[i].ed] = i;
    break;
    }
    }
    if(i == -1)
    {
    del[cur] = 1;
    top--;
    if(top) cur = stk[top-1];
    }
    }
    //update the flow
    if(cur == dest)
    {
    minf = INF;
    while(cur != src)
    {
    cur = pre[cur];
    if(edge[cur].flow < minf) minf = edge[cur].flow;
    cur = edge[cur].st;
    }
    cur = dest;
    while(cur != src)
    {
    cur = pre[cur];
    edge[cur].flow -= minf;
    edge[cur^1].flow += minf;
    if(edge[cur].flow == 0) ptr = edge[cur].st;
    cur = edge[cur].st;
    }
    while(top > 0&& stk[top-1] != ptr) top--;
    if(top) cur = stk[top-1];
    ret += minf;
    }
    }
    return ret;
    }

    int Dinic()
    {
    int ret = 0, t;
    while(dinic_bfs())
    {
    t = dinic_dfs();
    if(t) ret += t;
    else break;
    }
    if(ret==day) puts("Yes\n");
    else puts("No\n");
    }
    int main()
    {
    //freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;++i)
    {
    printf("Case %d: ",i);
    Build();
    Dinic();
    }
    return 0;
    }

  • 相关阅读:
    Codeforces Round #461 (Div. 2)B-Magic Forest+位运算或优雅的暴力
    动态规划:树形DP
    动态规划:划分DP
    动态规划:状压DP
    图论:树的直径
    图论:点分治
    图论:2-SAT
    数据结构&图论:K短路-可持久化可并堆
    图论:次短路
    图论:曼哈顿距离最小生成树
  • 原文地址:https://www.cnblogs.com/forgood/p/2638722.html
Copyright © 2020-2023  润新知