2018 Multi-University Training Contest 10 Problem L.Videos(1012)

1 篇文章 0 订阅
1 篇文章 0 订阅

 

费用流

#include<bits/stdc++.h>
using namespace std;

const int N = 500;
const int inf = 0x3f3f3f3f;

int T, n, m, K, W, tar;
bool inq[N];
int a[N], d[N], p[N];

struct Movie{
    int s, t, w, op;
    void read(){
        scanf("%d %d %d %d", &s, &t, &w, &op);
    }
}mv[N];

struct Edge{
    int from, to, cap, flow, cost;
    Edge(){}
    Edge(int from, int to, int cap, int cost):from(from),to(to),cap(cap),cost(cost){
        flow = 0;
    }
};
vector<Edge> G;
vector<int> V[N];

void add_edge(int from, int to, int cap, int cost){
    G.push_back(Edge(from, to, cap, cost));
    G.push_back(Edge(to, from, 0, -cost));
    int x = G.size();
    V[from].push_back(x - 2);
    V[to].push_back(x - 1);
}

bool BellmanFord(int &flow, int &cost){
    queue<int> Q;
    for(int i=0; i<=tar; i++){
        inq[i] = 0;
        d[i] = inf;
        a[i] = inf;
    }
    d[0] = 0; inq[0] = 1; p[0] = -1;
    Q.push(0);

    int x;
    while(!Q.empty()){
        x = Q.front(); Q.pop();
        inq[x] = 0;
        for(int i=0; i<V[x].size(); i++){
            int j = V[x][i];
            Edge &e = G[j];
            if(e.cap > e.flow && d[e.to] > d[x] + e.cost){
                d[e.to] = d[x] + e.cost;
                p[e.to] = j;
                a[e.to] = min(a[e.to], e.cap - e.flow);
                if(!inq[e.to]){
                    inq[e.to] = 1;
                    Q.push(e.to);
                }
            }
        }
    }

    if(d[tar] == inf)    return 0;

    x = tar;
    flow += a[tar];
    cost += a[tar] * d[tar];

    while(x){
        G[p[x]].flow += a[tar];
        G[p[x]^1].flow -= a[tar];
        x = G[p[x]].from;
    }

    return 1;
}

int solve(){
    tar = m * 2 + 2;

    for(int i=0; i<=tar; i++)    V[i].clear();
    G.clear();

    add_edge(0, tar - 1, K, 0);
    add_edge(tar - 1, tar, K, 0);

    int c;

    for(int i=1; i<=m; i++){
        add_edge(tar - 1, i, 1, 0);
        add_edge(i, i+m, 1, -mv[i].w);
        add_edge(i+m, tar, 1, 0);

        for(int j=1; j<=m; j++){
            if(mv[i].t <= mv[j].s){
                if(mv[i].op == mv[j].op){
                    c = W;
                } else {
                    c = 0;
                }
                add_edge(i+m, j, 1, c);
            }
        }
    }

    int flow, cost;

    flow = cost = 0;

    while(BellmanFord(flow, cost));

    return -cost;
}

int main(){
    scanf("%d", &T);
    while(T--){
        scanf("%d %d %d %d", &n, &m, &K, &W);
        for(int i=1; i<=m; i++){
            mv[i].read();
        }
        printf("%d\n", solve());
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值