#include <queue>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=410;
const int maxm=100010;
int head[maxn],eid,dis[maxn],pre[maxn],a[maxn];
bool vis[maxn];
struct node{
int u,v,cap,next,cost;
}eg[maxm];
void addedge(int u,int v,int cap,int cost){
eg[eid].u=u;eg[eid].v=v;eg[eid].cap=cap;
eg[eid].next=head[u];
eg[eid].cost=cost;
head[u]=eid++;
eg[eid].u=v;eg[eid].v=u;eg[eid].cap=0;
eg[eid].next=head[v];
eg[eid].cost=-cost;
head[v]=eid++;
}
bool spfa(int s,int t,int &flow,int &cost){
memset(vis,false,sizeof(vis));
for(int i=0;i<maxn;i++)dis[i]=inf;
dis[s]=0;vis[s]=true;pre[s]=0;a[s]=inf;
queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=eg[i].next){
if(eg[i].cap&&dis[eg[i].v]>dis[u]+eg[i].cost){
dis[eg[i].v]=dis[u]+eg[i].cost;
pre[eg[i].v]=i;
a[eg[i].v]=min(a[u],eg[i].cap);
if(!vis[eg[i].v]){
q.push(eg[i].v);
vis[eg[i].v]=true;
}
}
}
}
if(dis[t]==inf) return false;
flow+=a[t];
cost+=dis[t]*a[t];
int u=t;
while(u!=s){
eg[pre[u]].cap-=a[t];
eg[pre[u]^1].cap+=a[t];
u=eg[pre[u]].u;
}
return true;
}
int mincost(int s,int t){
int flow=0,cost=0;
while(spfa(s,t,flow,cost));
return cost;
}
void init(){
memset(head,-1,sizeof(head));
eid=0;
}
int A[210],B[210],C[210],tmp[maxn];
int main(){
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
int len=0;
for(int i=0;i<n;i++) scanf("%d%d%d",&A[i],&B[i],&C[i]),tmp[len++]=A[i],tmp[len++]=B[i];
sort(tmp,tmp+len);
int kkk=1;
for(int i=1;i<len;i++) if(tmp[i]!=tmp[i-1]) tmp[kkk++]=tmp[i];
addedge(0,1,m,0);addedge(kkk,kkk+1,m,0);
for(int i=1;i<kkk;i++) addedge(i,i+1,inf,0);
for(int i=0;i<n;i++){
int a=lower_bound(tmp,tmp+kkk,A[i])-tmp+1;
int b=lower_bound(tmp,tmp+kkk,B[i])-tmp+1;
addedge(a,b,1,-C[i]);
}
printf("%d\n",-mincost(0,kkk+1));
}
return 0;
}
费用流SPFA版
最新推荐文章于 2023-01-07 16:14:47 发布