zoukankan      html  css  js  c++  java
  • HDU 3572 Task Schedule(网络流+当前弧优化)

    网络流入门题目,抽象建边的过程有些困难,外加当前弧优化就过去了(没有这个会T)

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <map>
    #include <set>
    #include <cstdio>
    using namespace std;
    #define N 1205
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f
    typedef long long ll;
    class graphic{
    public:
        void init(){
            for(int i = 0 ; i < N ; ++i){
                G[i].clear();
            }
            edge.clear();
            cnt = 0;
        }
        void add(int u,int v,int d){
            _add(u,v,d);
            _add(v,u,0);
        }
        void setST(int a,int b){
            src = a,dst = b;
        }
        int Dinic(){
            int ans = 0;
            while (bfs()){
                memset(cur,0, sizeof(cur));
                ans+=dfs(src,0x3f3f3f3f);
            }
            return ans;
        }
    private:
        struct Edge{
            Edge(int a,int b):to(a),cap(b){}
            int to,cap;
        };
        vector<Edge>edge;
        int cnt,cur[N],src,dst,dis[N];
        vector<int>G[N];
        void _add(int u,int v,int d){
            edge.emplace_back(Edge(v,d));
            G[u].push_back(cnt++);
        }
        bool bfs(){
            memset(dis,-1, sizeof(dis));
            queue<int>q;
            dis[src] = 0;
            q.push(src);
            while (!q.empty()){
                int u = q.front();
                q.pop();
                for(auto i:G[u]){
                    Edge &e = edge[i];
                    if(e.cap > 0 and dis[e.to]==-1) {
                        dis[e.to] = dis[u] + 1;
                        q.push(e.to);
                    }
                }
            }
            return dis[dst]!=-1;
        }
        int dfs(int u,int a){
            if(u==dst or a == 0)return a;
            int flow = 0,f,len = G[u].size();
            for(int &i = cur[u] ; i < len ; ++i){
                Edge&e = edge[G[u][i]];
                if(e.cap > 0 and dis[e.to]==dis[u]+1 and (f = dfs(e.to,min(a,e.cap)))>0){
                    e.cap-=f;
                    edge[G[u][i]^1].cap+=f;
                    flow+=f;
                    a-=f;
                    if(a==0)break;
                }
            }
            return flow;
        }
    };
    graphic ss;
    int num[25][25],n;
    long long ans;
    int fx[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
    bool vis[N];
    int main(){
        int m,p,s,e;
        int l;
        cin>>l;
        for(int t = 1 ; t <= l ; ++t){
            cin>>n>>m;
            int src=0,dst=500+n+1;
            memset(vis,false, sizeof(vis));
            ss.init();
            ss.setST(src,dst);
            long long full_flow = 0;
            for(int i = 1 ; i <= n ; ++i){
                scanf("%d %d %d",&p,&s,&e);
                ss.add(src,500+i,p);
                full_flow+=p;
                for(int j = s; j <= e ; ++j){
                    ss.add(500+i,j,1);
                    vis[j] = true;
                }
            }
            for(int i = 1 ; i <= 500 ; ++i){
                if(vis[i])ss.add(i,dst,m);
            }
            printf("Case %d: ",t);
            long long ans = ss.Dinic();
            if(ans==full_flow)puts("Yes");
            else puts("No");
            puts("");
        }
    }
  • 相关阅读:
    Hibernate 系列教程13-继承-鉴别器与内连接相结合
    Hibernate 系列教程12-继承-Join策略
    Hibernate 系列教程11-继承-Single Table策略
    Hibernate 系列教程10-组成关系
    Hibernate 系列教程9-自关联
    Hibernate 系列教程8-复合主键
    Hibernate 系列教程7-双向一对一
    Hibernate 系列教程6-双向多对多
    Hibernate 系列教程5-双向多对一
    第三章:3.6 使用 Session
  • 原文地址:https://www.cnblogs.com/DevilInChina/p/9417868.html
Copyright © 2011-2022 走看看