POJ 3680 Intervals(最小费用流)

题目链接:点击打开链接

题意:n个区间, 每个区间有一个值, 让你选择若干区间, 使得没有一个点被覆盖超过k次的前提下的最大值。

思路:我们可以把区间端点离散化然后跑费用流, 不超过k次, 我们可以把这个对应流量属性。  那么不难想到, 将区间端点作为结点, 连一条流量为1,费用为-a[i].c的边, 因为可以跳过一些点, 所以我们把每个相邻端点之间用流量INF,费用为0的边连接, 然后源点流量为k, 汇点流量为k, 当其满流的时候, 就求出了最大费用, 而且可以保证每个结点覆盖不会超过k次。

最后多说一下, 这个流量特性, 为什么最大流等于最小割, 其实很简单, 因为限制一条路的最大流量的就是那个最小流量的地方。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 500;
struct Edge {
  int from, to, cap, flow, cost;
};

struct MCMF {
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  int inq[maxn];         // 是否在队列中
  int d[maxn];           // Bellman-Ford
  int p[maxn];           // 上一条弧
  int a[maxn];           // 可改进量

  void init(int n) {
    this->n = n;
    for(int i = 0; i < n; i++) G[i].clear();
    edges.clear();
  }

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

  bool BellmanFord(int s, int t, int& flow, int& cost) {
    for(int i = 0; i < n; i++) d[i] = INF;
    memset(inq, 0, sizeof(inq));
    d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;

    queue<int> Q;
    Q.push(s);
    while(!Q.empty()) {
      int u = Q.front(); Q.pop();
      inq[u] = 0;
      for(int i = 0; i < G[u].size(); i++) {
        Edge& e = edges[G[u][i]];
        if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
          d[e.to] = d[u] + e.cost;
          p[e.to] = G[u][i];
          a[e.to] = min(a[u], e.cap - e.flow);
          if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
        }
      }
    }
    if(d[t] == INF) return false;
    flow += a[t];
    cost += d[t] * a[t];
    int u = t;
    while(u != s) {
      edges[p[u]].flow += a[t];
      edges[p[u]^1].flow -= a[t];
      u = edges[p[u]].from;
    }
    return true;
  }

  // 需要保证初始网络中没有负权圈
  int Mincost(int s, int t) {
    int cost = 0, flow = 0;
    while(BellmanFord(s, t, flow, cost));
    return cost;
  }

};
struct node {
    int a, b, c;
}a[maxn];
MCMF g;
int T, n, k, b[maxn];
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&k);
        int cnt = 0;
        for(int i=0;i<n;i++) {
            scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c);
            b[cnt++] = a[i].a;
            b[cnt++] = a[i].b;
        }
        sort(b, b+cnt);
        int len = unique(b, b+cnt) - b;
        g.init(len + 3);
        for(int i=0;i<len-1;i++) {
            g.AddEdge(i, i+1, INF, 0);
        }
        int s = len, t = len + 1;
        g.AddEdge(s, 0, k, 0);
        g.AddEdge(len-1, t, k, 0);
        for(int i=0;i<n;i++) {
            int l = lower_bound(b, b+len, a[i].a) - b;
            int r = lower_bound(b, b+len, a[i].b) - b;
            g.AddEdge(l, r, 1, -a[i].c);
        }
        printf("%d\n",-g.Mincost(s, t));
    }
    return 0;
}
时间: 2024-08-02 14:49:13

POJ 3680 Intervals(最小费用流)的相关文章

POJ 3680 Intervals 离散 + 费用流

Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6246   Accepted: 2542 Description You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize t

poj 3680 Intervals 最大费用流

题意: 给n给开区间(ai,bi)及相应权值wi,现在要选一些区间,要求任一点不能被超过k个区间覆盖,目标是最大化总的权重. 分析: 转化为求最大费用流,改改最小费用流的模板就好. 代码: //poj 3680 //sep9 #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxN=2048;

POJ 3680 Intervals(费用流+离散化)

题目地址:POJ 3680 这题的建图真心想不出来.建图思维还是不够开阔,不够大胆. 这题要先对坐标进行离散化.可以用左边的点发出一条到右边的点的边,容量为1,费用为负的权值.然后从左往右将依次将相邻的两个点都连起来,权值为0,容量为k,也就是说,如果选了这个区间,就会从费用为负数的边流过去,否则,就是从这个费用为0的边流过去.然后建立一个超级源点与最左边的点相连,权值为0,容量为k,这样就保证了重叠数之多为k,因为增广路上所经过的区间必定是不重合的,而流量只有k,所以满足题意. 代码如下: #

poj 3680 Intervals

给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1<=ai<bi<=100000.1<=wi<=100000. 最小费用最大流. 将所有区间端点离散化到整数1到M,每个数对应一个点. 源点向整数1点连一条容量为K费用为0的边. 整数i点向整数i+1点连一条容量为正无穷费用为0的边.(1<=i<M). 整数M点向汇点连一条容

poj 3680 Intervals 费用流

题目链接 给一些线段, 每个线段有一个值, 并且覆盖一些点, 求每个点被覆盖次数不超过k时, 可以取得的最大值. 首先将点离散化, 然后连边, i向i+1连一条容量为k, 费用为0的边. 对于每条线段, 起点向终点连一条容量为1, 费用为-val的边, 然后跑费用流就好. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include

POJ 3680: Intervals【最小费用最大流】

题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由于要最大,所以是求最大费用最大流),容量为1,至于不超过K的限制,只要从源点到第一个点的流量为K就行,剩下每个相邻的点相连,费用为0,流量只要大于的等于K就可以(我取的正无穷) //poj3680 #include <stdio.h> #include <iostream> #incl

POJ 3680 Intervals(经典费用流)

解题思路: 区间K覆盖问题:数轴上有一些带权值的区间,选出权和尽量大的一些区间,使得任意一个点最多被K个区间覆盖. 构图方法为:把每一个数作为一个节点,然后对于权值为W的区间[ u, v ]连一条边,容量为1,费用为-w,再对所有相邻 的点连边i -> i + 1,容量为K,费用为0:最后求最左端到最右端的最小费用最大流即可.如果数值范围太大,需要先进行离散化. #include <iostream> #include <cstring> #include <cstdi

POJ 3670 Intervals(费用流)

POJ 3680 Intervals 题目链接 题意:给定一些区间,每个区间有一个权值,要求用这些区间去覆盖,每个点最多覆盖k次,问最多得到权值多少 思路:典型的区间k覆盖问题,区间连边容量1,代价-w,然后其他点相邻两两连边,容量k,代价0,跑一下费用流即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm&g

POJ No.3680 Intervals

2016-06-01 22:01:39 题目链接: POJ No.3680 Intervals 题目大意: 给定N个带权区间,最多可以重复选一个点M次,求出一种选法使得所得权最大 解法: 费用流 建模: 区间的端点之间按照副权流量1连接,而每个点之间需要再连0权流量无穷作为跳过用 注意的地方: 十万个点肯定是不行的,看我unique离散化大法 1 //Intervals (POJ No.3680) 2 //费用流 3 #include<stdio.h> 4 #include<algori