对于区间 u->v ,连接边 u->v,权值为-len,容量为1,之后对每个点 i->i+1,连边 i->i+1,容量为k,权值为0,求区间最左端点到最右端点的费用流,费用相反数即为答案。
// q.c#include
#include
#include
#include
#include
#include
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int M=500+5,INF=(int)1e8;
struct Edge {int u,v,nex,cost,flow,cap; Edge() {}Edge(int a,int b,int c,int d,int e,int f):u(a),v(b),nex(c),cost(d),flow(e),cap(f) {}
}ed[M<<3];
int cnt,head[M<<1];
void add_edge(int a,int b,int c,int d) {ed[cnt]&#61;Edge(a,b,head[a], c,0,d); head[a]&#61;cnt&#43;&#43;;ed[cnt]&#61;Edge(b,a,head[b],-c,0,0); head[b]&#61;cnt&#43;&#43;;
}
struct Dinic {int n,s,t,dis[M<<1],pre[M<<1],lim[M<<1]; bool in[M<<1]; queue
}DC;
struct Data {int x,y,z;bool operator <(const Data &A) const {if(x!&#61;A.x) return x
int ref[M<<1];
int main() {freopen("interv.in","r",stdin);freopen("interv.out","w",stdout);memset(head,-1,sizeof(head));int n,k,m&#61;0,a,b,p,q,tot&#61;0;scanf("%d%d",&n,&k);for(int i&#61;1;i<&#61;n;i&#43;&#43;) {scanf("%d%d",&a,&b);if(a>&#61;b) continue;&#43;&#43;m;q&#61;m<<1; p&#61;q-1;da[m].x&#61;p,da[m].y&#61;q;da[m].z&#61;b-a;tmp[p].x&#61;a,tmp[p].y&#61;p;tmp[q].x&#61;b,tmp[q].y&#61;q;}sort(tmp&#43;1,tmp&#43;(m<<1)&#43;1);for(int i&#61;1;i<&#61;(m<<1);i&#43;&#43;) {if(tmp[i].x!&#61;tmp[i-1].x) ref[tmp[i].y]&#61;&#43;&#43;tot;else ref[tmp[i].y]&#61;tot;}for(int i&#61;1;i<&#61;m;i&#43;&#43;) {da[i].x&#61;ref[da[i].x];da[i].y&#61;ref[da[i].y];add_edge(da[i].x,da[i].y,-(da[i].z),1);}for(int i&#61;1;i