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

 

转载于:https://www.cnblogs.com/DevilInChina/p/9417868.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值