焦作网络赛F(k可重区间集问题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qkoqhh/article/details/82871809

这个在网络流24题中出现过的(然而窝并没有想起来)

显然先将区间离散化再说

然后建图方式是从i到i+1连容量为k费用为0的边,对每个区间[l,r]从l-1到r连容量为1费用为该区间价值的边(看做这个区间是从l-1的点接过来),然后跑最大费用流即可。。

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include<stdlib.h>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 405 
#define nm 10005
#define pi 3.1415926535897931
const int inf=1e9+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar() ;
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}





struct edge{int t,v,w;edge*next,*rev;}e[nm],*h[NM],*o=e,*p[NM];
void _add(int x,int y,int w,int v){o->t=y;o->v=v;o->w=w;o->next=h[x];h[x]=o++;}
void add(int x,int y,int w,int v){_add(x,y,w,v);_add(y,x,0,-v);h[x]->rev=h[y];h[y]->rev=h[x];}


struct tmp{int l,r;}a[NM];
int n,m,ans,d[NM],w[NM],b[NM],tot,c[NM*2];
bool v[NM];
queue<int>q;


int spfa(){
    v[n+1]++;q.push(n+1);mem(w);mem(v);mem(p);
    inc(i,0,n)d[i]=inf;w[n+1]=inf;d[n+1]=0;
    while(!q.empty()){
	int t=q.front();q.pop();v[t]=false;
	link(t)if(j->w&&d[j->t]>d[t]+j->v){
	    d[j->t]=d[t]+j->v;w[j->t]=min(w[t],j->w);p[j->t]=j;
	    if(!v[j->t])v[j->t]++,q.push(j->t);
	}
    }
    return w[n];
}


int main(){
    int _=read();while(_--){
	mem(e);mem(h);o=e;tot=0;
	n=read();m=read();n=read();
	inc(i,1,n)c[++tot]=a[i].l=read(),c[++tot]=a[i].r=read(),b[i]=read();
	sort(c+1,c+1+tot);tot=unique(c+1,c+1+tot)-c-1;
	inc(i,1,tot)add(i-1,i,m,0);
	inc(i,1,n)a[i].l=lower_bound(c+1,c+1+tot,a[i].l)-c,a[i].r=lower_bound(c+1,c+1+tot,a[i].r)-c,
	    add(a[i].l-1,a[i].r,1,-b[i]);
	n=tot;ans=0;
	add(n+1,0,m,0);
	while(spfa()){
	    ans-=d[n]*w[n];
	    for(int x=n;p[x];x=p[x]->rev->t)p[x]->w-=w[n],p[x]->rev->w+=w[n];
	}
	printf("%d\n",ans);
    }
    return 0;
}

Modular Production Line

An automobile factory has a car production line. Now the market is oversupply and the production line is often shut down. To make full use of resources, the manager divides the entire production line into NNN parts (1...N)(1...N)(1...N). Some continuous parts can produce sub-products. And each of sub-products has their own value. The manager will use spare time to produce sub-products to make money. Because of the limited spare time, each part of the production line could only work at most KKK times. And Because of the limited materials, each of the sub-products could be produced only once. The manager wants to know the maximum value could he make by produce sub-products.

Input

The first line of input is TTT, the number of test case.

The first line of each test case contains three integers, N,KN, KN,K and MMM. (MMM is the number of different sub-product).

The next MMM lines each contain three integers Ai,Bi,WiA_i, B_i, W_iAi​,Bi​,Wi​ describing a sub-product. The sub-product has value WiW_iWi​. Only AiA_iAi​ to BiB_iBi​ parts work simultaneously will the sub-product be produced (include AiA_iAi​ to BiB_iBi​).

1≤T≤1001 \le T \le 1001≤T≤100

1≤K≤M≤2001 \le K \le M \le 2001≤K≤M≤200

1≤N≤1051 \le N \le 10^51≤N≤105

1≤Ai≤Bi≤N1 \le A_i \le B_i \le N1≤Ai​≤Bi​≤N

1≤Wi≤1051 \le W_i \le 10^51≤Wi​≤105

Output

For each test case output the maximum value in a separate line.

样例输入

4
10 1 3
1 2 2
2 3 4
3 4 8
10 1 3
1 3 2
2 3 4
3 4 8
100000 1 3
1 100000 100000
1 2 3
100 200 300
100000 2 3
1 100000 100000
1 150 301
100 200 300

样例输出

10
8
100000
100301

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/82871809