/*
构图很关键,由于是开区间,所以可以把顶点之间的边当成是线段之间的点。
每条线段之间的边的容量定义为k,说明这个线段只能交叉k次。,k次之后就不能从这个线段交叉过去了。
好题
*/
// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <strstream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>
using namespace std;
// typedef
typedef long long LL;
typedef unsigned long long ULL;
//
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FF(i,a) for(int i=0;i<(a);i+++)
#define FFD(i,a) for(int i=(a)-1;i>=0;i--)
#define Z(a) (a<<1)
#define Y(a) (a>>1)
const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
if(x>y) return x;
return y;
}
template<class T> inline T TMIN(T x,T y)
{
if(x<y) return x;
return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
T t = x;
x = y;
y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
return TMAX(TMAX(x,y),z);
}
// code begin
#define MAXN 410
#define MAXM 2000
struct node
{
int s;
int e;
int next;
int remain;
int cost;
};
node mem[MAXM];
int G[MAXN];
class MinCostMaxFlow_fordfulkerson_SPFA_queue
{
private:
int N,source,sink;
int dx;
// SPFA用
int dist[MAXN];
int isin[MAXN];
int cnt[MAXN];
int fat[MAXN];
int que[MAXN]; //BFS用
int flow;
int cost;
public:
MinCostMaxFlow_fordfulkerson_SPFA_queue(){}
void Set(int Nt,int sourcet,int sinkt)
{
N = Nt;
source = sourcet;
sink = sinkt;
}
void Init()
{
dx = 0;
memset(G,-1,sizeof(G));
}
void Add_edge(int a,int b,int f,int c)
{
mem[dx].s = a;
mem[dx].e = b;
mem[dx].remain = f;
mem[dx].cost = c;
mem[dx].next = G[a];
G[a] = dx++;
mem[dx].s = b;
mem[dx].e = a;
mem[dx].remain = 0;
mem[dx].cost = -c;
mem[dx].next = G[b];
G[b] = dx++;
}
void Print()
{
printf("打印图\n");
FORi(1,N+1,1)
{
printf("%d: ",i);
int mdx = G[i];
while(mdx!=-1)
{
int v = mem[mdx].e;
printf("[%d %d %d] ",v,mem[mdx].remain,mem[mdx].cost);
mdx = mem[mdx].next;
}
printf("\n");
}
printf("\n");
}
int Getflow(){return flow;}
int Getcost(){return cost;}
public:
int SPFA() // bellman ford queue optimization
{
// from source
fill(dist,dist+N+1,-1);
memset(isin,0,sizeof(isin));
memset(cnt,0,sizeof(cnt));
int head(0),tail(0);
dist[source] = 0;
que[tail] = source;
tail = (tail+1)%MAXN;
isin[source] = true;
cnt[source] ++;
while(head!=tail)
{
int cur = que[head];
isin[cur] = false;
head = (head+1)%MAXN;
int mdx = G[cur];
while(mdx!=-1)
{
int v = mem[mdx].e;
if( mem[mdx].remain!=0 && dist[cur]+mem[mdx].cost>dist[v])
{
dist[v] = dist[cur] + mem[mdx].cost;
fat[v] = mdx;
if(!isin[v])
{
isin[v] = true;
cnt[v] ++;
que[tail] = v;
tail = (tail+1)%MAXN;
}
}
mdx = mem[mdx].next;
}
}
//if(dist[sink]==-1) return -1;
return dist[sink];
}
int Augment()
{
int minp = INFi;
int mdx = fat[sink];
while(mem[mdx].s!=source)
{
if( mem[mdx].remain<minp )
{
minp = mem[mdx].remain;
}
mdx = fat[mem[mdx].s];
}
mdx = fat[sink];
while(mem[mdx].s!=source)
{
mem[mdx].remain -= minp;
mem[mdx^1].remain += minp;
mdx = fat[mem[mdx].s];
}
return minp;
}
void mincostmaxflow()
{
cost=0;
flow=0;
int fdx;
int cdx;
while((cdx=SPFA())!=-1)
{
fdx = Augment();
cost += cdx*fdx;
flow += fdx;
}
}
};
int T,N,K;
int source,sink;
struct seg
{
int s;
int e;
int w;
};
seg xd[210];
int ver[MAXN],top,vs;
int index[100010];
MinCostMaxFlow_fordfulkerson_SPFA_queue g;
int main()
{
read;
write;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&N,&K);
top = 0;
FORi(1,N+1,1)
{
scanf("%d %d %d",&xd[i].s,&xd[i].e,&xd[i].w);
ver[top++] = xd[i].s;
ver[top++] = xd[i].e;
}
// 去除重复
sort(ver,ver+top);
vs = 1;
FORi(1,top,1)
{
if(ver[i]!=ver[i-1])
{
ver[vs++] = ver[i];
}
}
// 离散化 vs顶点个数
FORi(0,vs,1)
{
index[ver[i]] = i+1;
}
// 构图
g.Init();
source = vs+1;
sink = vs+2;
g.Set(vs+2,source,sink);
FORi(1,vs,1)
{
// i i+1
g.Add_edge(i,i+1,K,0);
}
FORi(1,N+1,1)
{
if( xd[i].s>xd[i].e )
SWAP(xd[i].s,xd[i].e);
g.Add_edge(index[xd[i].s],index[xd[i].e],1,xd[i].w);
}
g.Add_edge(source,1,K,0);
g.Add_edge(vs,sink,K,0);
//g.Print();
g.mincostmaxflow();
printf("%d\n",g.Getcost());
}
return 0;
}
转载于:https://www.cnblogs.com/ac2012/archive/2011/03/19/1988692.html