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<algorithm>
  5 #include<queue>
  6 using namespace std;
  7 const int maxn=410;
  8 int T,N,K;
  9 int hash[maxn];
 10 int a[maxn];
 11 int w[maxn];
 12 struct edge
 13 {
 14     int to;
 15     int from;
 16     int cost;
 17     int flow;
 18     int next;
 19     edge(){}
 20     edge(int from,int to,int cost,int flow,int next):from(from),to(to),cost(cost),flow(flow),next(next){}
 21 };
 22 edge n[maxn*10];
 23 int cnt;
 24 int len;
 25 int now;
 26 int head[maxn];
 27 bool vis[maxn];
 28 int dist[maxn];
 29 int pre[maxn];
 30 queue <int> q;
 31 void insert(int x,int y,int z,int cost)
 32 {
 33     n[++cnt]=edge(x,y,cost,z,head[x]);
 34     head[x]=cnt;
 35     n[++cnt]=edge(y,x,-cost,0,head[y]);
 36     head[y]=cnt;
 37     return ;
 38 }
 39 void SPFA(int s,int t)
 40 {
 41     fill(dist,dist+maxn,100000);
 42     q.push(s);
 43     vis[s]=1;
 44     dist[s]=0;
 45     while(!q.empty())
 46     {
 47         now=q.front();
 48         q.pop();
 49         vis[now]=0;
 50         for(int i=head[now];i;i=n[i].next)
 51         {
 52             if(n[i].flow>0)
 53             {
 54                 if(dist[n[i].to]>dist[now]+n[i].cost)
 55                 {
 56                     dist[n[i].to]=dist[now]+n[i].cost;
 57                     pre[n[i].to]=i;
 58                     if(!vis[n[i].to])
 59                     {
 60                         vis[n[i].to]=1;
 61                         q.push(n[i].to);
 62                     }
 63                 }
 64             }
 65         }
 66     }
 67     for(int i=pre[t];i;i=pre[n[i].from])
 68     {
 69         n[i].flow--;
 70         n[i^1].flow++;
 71     }
 72     return ;
 73 }
 74 int Mincost_flow(int s,int t,int num)
 75 {
 76     int ans=0;
 77     while(num--)
 78     {
 79         SPFA(s,t);
 80         ans+=dist[t];
 81     }
 82     return -ans;
 83 }
 84 int main()
 85 {
 86     scanf("%d",&T);
 87     while(T--)
 88     {
 89         cnt=1;
 90         fill(head,head+maxn,0);
 91         scanf("%d %d",&N,&K);
 92         for(int i=1;i<=N;i++)
 93         {
 94             scanf("%d %d",&a[i*2-1],&a[i*2]);
 95             scanf("%d",&w[i]);
 96             hash[i*2-1]=a[i*2-1];
 97             hash[i*2]=a[i*2];
 98         }
 99         sort(hash+1,hash+2*N+1);
100         len=unique(hash+1,hash+2*N+1)-hash-1;
101         for(int i=1;i<=N*2;i++)
102         {
103             a[i]=lower_bound(hash+1,hash+len+1,a[i])-hash;
104             if(!(i&1))insert(a[i-1],a[i],1,-w[i/2]);
105         }
106         for(int i=1;i<len;i++)insert(i,i+1,100000,0);
107         insert(len,len+1,K,0);
108         insert(0,1,K,0);
109         printf("%d\n",Mincost_flow(0,len+1,K));
110     }
111 }
时间: 2024-07-31 10:38:06

POJ No.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 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

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

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

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

给定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个区间, 每个区间有一个值, 让你选择若干区间, 使得没有一个点被覆盖超过k次的前提下的最大值. 思路:我们可以把区间端点离散化然后跑费用流, 不超过k次, 我们可以把这个对应流量属性.  那么不难想到, 将区间端点作为结点, 连一条流量为1,费用为-a[i].c的边, 因为可以跳过一些点, 所以我们把每个相邻端点之间用流量INF,费用为0的边连接, 然后源点流量为k, 汇点流量为k, 当其满流的时候, 就求出了最大费用, 而且可以保证每个结点覆盖不会超过k次.

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