码迷,mamicode.com
首页 > 其他好文 > 详细

ACM-ICPC 2018 焦作赛区网络预赛 F. Modular Production Line (区间K覆盖-最小费用流)

时间:2018-09-15 19:19:05      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:==   set   turn   方式   product   return   取绝对值   绝对值   freopen   

很明显的区间K覆盖模型,用费用流求解.只是这题N可达1e5,需要将点离散化.
建模方式步骤:
1.对权值为w的区间[u,v],加边id(u)->id(v+1),容量为1,费用为-w;
2.对所有相邻的点加边id(i)->id(i+1),容量为正无穷,费用为0;
3.建立源点汇点,由源点s向最左侧的点加边,容量为K,费用为0,由最右侧的点向汇点加边,容量为K,费用为0
4.跑出最大流后,最小费用取绝对值就是能获得的最大权

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge{
    int to, next, cap, flow, cost;
} edge[MAXM];
int head[MAXN], tol;
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N;
void init(int n)
{
    N = n;
    tol = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int cap, int cost)
{
    edge[tol].to = v;
    edge[tol].cap = cap;
    edge[tol].cost = cost;
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].cost = -cost;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}

bool spfa(int s, int t){
    queue<int> q;
    for (int i = 0; i < N; i++){
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while (!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;

            if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost){
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i;
                if (!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if (pre[t] == -1) return false;
    else  return true;
}

int minCostMaxflow(int s, int t, int &cost){
    int flow = 0;
    cost = 0;
    while (spfa(s, t)){
        int Min = INF;
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]){
            if (Min > edge[i].cap - edge[i].flow)
                Min = edge[i].cap - edge[i].flow;
        }
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]){
            edge[i].flow += Min;
            edge[i ^ 1].flow -= Min;
            cost += edge[i].cost * Min;
        }
        flow += Min;
    }
    return flow;
}

map<int,int> dp;
struct edg{
    int u,v,w;
}ed[MAXN];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T,N,M,K,u,v,w;
    scanf("%d",&T);
    map<int,int> ::iterator it;
    while(T--){
        dp.clear();
        scanf("%d %d %d",&N, &K, &M);
        for(int i=1;i<=M;++i){
            scanf("%d %d %d",&u,&v,&w);
            v++;
            ed[i] = (edg){u,v,w};
            dp[u] = dp[v] = 1;
        }
        int cnt=0;
        for(it = dp.begin();it!=dp.end();++it){
            int id = it->first;
            dp[id] =  ++cnt;
        }
        init(cnt+5);
        int s = 0,t = cnt+1;
        addedge(s,1,K,0);
        addedge(cnt,t,K,0);
        for(int i=1;i<cnt;++i){
            addedge(i,i+1,INF,0);
        }
        for(int i=1;i<=M;++i){
            u = ed[i].u, v = ed[i].v;
            u = dp[u], v =dp[v];
            addedge(u,v,1,-ed[i].w);
        }
        int cost;
        minCostMaxflow(s,t,cost);
        printf("%d\n",-cost);
    }
    return 0;
}

ACM-ICPC 2018 焦作赛区网络预赛 F. Modular Production Line (区间K覆盖-最小费用流)

标签:==   set   turn   方式   product   return   取绝对值   绝对值   freopen   

原文地址:https://www.cnblogs.com/xiuwenli/p/9651790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!