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

19 篇文章 0 订阅
9 篇文章 0 订阅

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


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


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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值