BZOJ 3876 AHOI 2014 支线剧情 有下界有源汇的费用流

题目大意:主人公在玩游戏,他的存档系统坏了,只能从头开始游戏,不能从中途开始,问最少多长时间可以走过所有的流程。


思路:每一条边都要至少走一次,这是流量的下界,源点是游戏的开始,汇点是所有结局。裸的有下界有源汇的费用流。我也不知道为什么要那样建图。。


CODE:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1000
#define MAXE 1000010
#define S 0
#define T (MAX - 1)
#define INF 0x3f3f3f3f
using namespace std;
 
struct MinCostMaxFlow{
    int head[MAX],total;
    int next[MAXE],aim[MAXE],flow[MAXE],cost[MAXE];
     
    int f[MAX],from[MAX],p[MAX];
    bool v[MAX];
     
    MinCostMaxFlow():total(1) {}
    void Add(int x,int y,int f,int c) {
        next[++total] = head[x];
        aim[total] = y;
        flow[total] = f;
        cost[total] = c;
        head[x] = total; 
    }
    void Insert(int x,int y,int f,int c) {
        Add(x,y,f,c);
        Add(y,x,0,-c);
    }
    bool SPFA() {
        static queue<int> q;
        while(!q.empty())   q.pop();
        memset(f,0x3f,sizeof(f));
        memset(v,false,sizeof(v));
        f[S] = 0;
        q.push(S);
        while(!q.empty()) {
            int x = q.front(); q.pop();
            v[x] = false;
            for(int i = head[x]; i; i = next[i])
                if(flow[i] && f[aim[i]] > f[x] + cost[i]) {
                    f[aim[i]] = f[x] + cost[i];
                    if(!v[aim[i]])
                        v[aim[i]] = true,q.push(aim[i]);
                    from[aim[i]] = x;
                    p[aim[i]] = i;
                }
        }
        return f[T] != INF;
    }
    int EdmondsKarp() {
        int re = 0;
        while(SPFA()) {
            int max_flow = INF;
            for(int i = T; i != S; i = from[i])
                max_flow = min(max_flow,flow[p[i]]);
            for(int i = T; i != S; i = from[i]) {
                flow[p[i]] -= max_flow;
                flow[p[i]^1] += max_flow;
            }
            re += f[T] * max_flow;
        }
        return re;
    }
}solver;
 
int points;
 
int main()
{
    cin >> points;
    //solver.Insert(S,1,INF,0);
    for(int cnt,i = 1; i <= points; ++i) {
        scanf("%d",&cnt);
        //if(!cnt)  solver.Insert(i,T,INF,0);
        solver.Insert(i,1,INF,0);
        for(int x,len,j = 1; j <= cnt; ++j) {
            scanf("%d%d",&x,&len);
            solver.Insert(i,x,INF,len);
            solver.Insert(S,x,1,len);
            solver.Insert(i,T,1,0);
        }
    }
    cout << solver.EdmondsKarp() << endl;
    return 0;
}


你可能感兴趣的:(网络流,费用流,bzoj,AHOI2014)