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("");
    }
}

猜你喜欢

转载自www.cnblogs.com/DevilInChina/p/9417868.html