思路:
(from mhr)
#include
#include
#include
#include
using namespace std;
#define N 444
#define M 2222
int first[N],next[M],v[M],edge[M],cost[M],s[N],top,tot,ans,T;
int cases,n,K,xx[N],yy[N],zz[N],vis[N],d[N],with[N],minn[N];
void Add(int x,int y,int C,int E){
edge[tot]=E,cost[tot]=C,v[tot]=y,next[tot]=first[x],first[x]=tot++;
}
void add(int x,int y,int C,int E){Add(x,y,C,E),Add(y,x,-C,0);}
bool tell(){
memset(vis,0,sizeof(vis)),memset(d,0x3f,sizeof(d));
memset(with,0,sizeof(with)),memset(minn,0x3f,sizeof(minn));
queue<int>q;q.push(0);d[0]=0;
while(!q.empty()){
int t=q.front();q.pop();vis[t]=0;
for(int i=first[t];~i;i=next[i])
if(d[v[i]]>d[t]+cost[i]&&edge[i]>0){
d[v[i]]=d[t]+cost[i],minn[v[i]]=min(minn[t],edge[i]),with[v[i]]=i;
if(!vis[v[i]])vis[v[i]]=1,q.push(v[i]);
}
}return d[T]!=0x3f3f3f3f;
}
int zeng(){
for(int i=T;i;i=v[with[i]^1])
edge[with[i]]-=minn[T],edge[with[i]^1]+=minn[T];
return d[T];
}
int main(){
scanf("%d",&cases);
while(cases--){
memset(first,-1,sizeof(first)),tot=top=ans=0;
scanf("%d%d",&n,&K);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&xx[i],&yy[i],&zz[i]);
s[++top]=xx[i],s[++top]=yy[i];
}
sort(s+1,s+1+top),add(0,1,0,K);
T=unique(s+1,s+1+top)-s;
for(int i=1;i<=n;i++){
int tx=lower_bound(s+1,s+T,xx[i])-s,ty=lower_bound(s+1,s+T,yy[i])-s;
add(tx,ty,-zz[i],1);
}
for(int i=0;i1,0,K);
while(tell())ans+=zeng();
printf("%d\n",-ans);
}
}