hdu - 2883 最大流 离散化 dinic

题意:给定n个顾客,第i号顾客在si到达,点了ni个羊肉串,每个羊肉串需要ti个时间烤好。顾客想要在ei得到,一个烤炉只烤m串。问你是否能满足所有顾客的要求?能的话输出“Yes”,否则输出“No”。

和3572类似,不过时间太长要用区间,读入每个时间点排序去重得到tot时间点 tot-1区间

建图:源点到顾客任务(sp, i, ni * ti)

        时间区间到汇点(j, tp, (t[j] - t[j-1]) * m)

        顾客任务包含时间区间(i, j, inf)

最大流 == 总时间?

链接:hdu 2883

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
//#define ll long long
#define MAXN 2005

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 2005;

int n, m;//点数、边数
int sp, tp;//原点、汇点

struct node  {
    int v, next;
    int cap;
}mp[maxn];

int pre[MAXN], dis[MAXN], cur[MAXN];//cur为当前弧优化,dis存储分层图中每个点的层数(即到原点的最短距离),pre建邻接表
int cnt = 0;

void init() {  //不要忘记初始化
    cnt = 0;
    memset(pre, -1, sizeof(pre));
}

void add(int u, int v, int w) { //加边
    mp[cnt].v = v;
    mp[cnt].cap = w;
    mp[cnt].next = pre[u];
    pre[u] = cnt++;
    mp[cnt].v = u;
    mp[cnt].cap = 0;
    mp[cnt].next = pre[v];
    pre[v] = cnt++;
}

bool bfs() {  //建分层图
    memset(dis, -1, sizeof(dis));
    queueq;
    while(!q.empty())
        q.pop();
    q.push(sp);
    dis[sp] = 0;
    int u, v;
    while(!q.empty()) {
        u = q.front();
        q.pop();
        for(int i = pre[u]; i != -1; i = mp[i].next) {
            v = mp[i].v;
            if(dis[v] == -1 && mp[i].cap>0) {
                dis[v] = dis[u] + 1;
                q.push(v);
                if(v == tp)
                    break;
            }
        }
    }
    return dis[tp] != -1;
}

int dfs(int u,int cap) {//寻找增广路
    if(u == tp || cap == 0)
    return cap;
    int res = 0, f;
    for(int &i = cur[u]; i != -1; i = mp[i].next) {//
        int v = mp[i].v;
        if(dis[v] == dis[u] + 1 && (f = dfs(v, min(cap - res, mp[i].cap))) > 0) {
            mp[i].cap -= f;
            mp[i ^ 1].cap += f;
            res += f;
            if(res == cap)
                return cap;
        }
    }
    if(!res)
        dis[u] = -1;
    return res;
}

int dinic() {
    int ans = 0;
    while(bfs()) {
        for(int i = sp; i <= tp; i++)
            cur[i] = pre[i];
        ans += dfs(sp, inf);
    }
    return ans;
}

struct node1 {
    int si, ei;
    int ti, ni;
    int tt;
}pi[205];

int tim[410];

int main()
{
    int t, kcase = 0;
    while(scanf("%d %d", &n, &m) != EOF) {
        init();
        int tot = 0;
        int maxflow = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d %d %d %d", &pi[i].si, &pi[i].ni, &pi[i].ei, &pi[i].ti);
            pi[i].tt = pi[i].ni * pi[i].ti;
            maxflow += pi[i].tt;
            tim[tot++] = pi[i].si;
            tim[tot++] = pi[i].ei;
        }
        sort(tim, tim + tot);
        tot = unique(tim, tim + tot) - tim;
        sp = 0, tp = n + tot + 1;
        for(int i = 1; i <= n; i++) {
            add(sp, i, pi[i].tt);
        }
        for(int i = 1; i < tot; i++) {
            add(n + i, tp, (tim[i] - tim[i - 1]) * m);
            for(int j = 1; j <= n; j++) {
                if(pi[j].si <= tim[i - 1] && pi[j].ei >= tim[i]) {
                    add(j, n + i, inf);
                }
            }
        }
        int ans = dinic();
        if(ans >= maxflow) {
            puts("Yes");
        }
        else {
            puts("No");
        }
    }
    return 0;
}

 

你可能感兴趣的:(网络流)