//线性规划与网络流第22题
//最长k可重线段问题
//20分/100分 程序
//感觉是数据有误T_T
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#define pb push_back
using namespace std;
typedef long long LL;
const int N=2105,inf=0x3f3f3f3f;
const LL INF=(1LL)<<50;
struct MCMF
{
struct Edge
{
int from,to,cap,flow;
LL cost;
Edge(){}
Edge(int a,int b,int c,int d,LL e):from(a),to(b),cap(c),flow(d),cost(e){}
};
int n,m,s,t;
vector<Edge> edges;
vector<int> G[N];
int inq[N],p[N],a[N];
LL d[N];
queue<int> Q;
void init(int n)
{
this->n=n;
for(int i=0;i<n;++i) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,LL cost)
{ //printf("~~~~~~~%d %d %d %lld\n",from,to,cap,cost);
edges.push_back(Edge(from,to,cap,0,cost));
edges.push_back(Edge(to,from,0,0,-cost));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool SPFA(int s,int t,int & flow,LL & cost)
{
for (int i=0;i<n;i++)d[i]=INF;
memset(inq,0,sizeof(inq));
d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();Q.pop();
inq[u]=0;
for(int i=0;i<G[u].size();++i)
{
Edge& e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]) {Q.push(e.to);inq[e.to]=1;}
}
}
}
if(d[t]==INF) return false;
flow+=a[t];//固定流量k,检查,若flow+a>=k,只增加k-flow个单位
cost+=d[t]*a[t];
int u=t;
while(u!=s)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
u=edges[p[u]].from;
}
return true;
}
LL Mincost(int s,int t)
{
int flow=0;
LL cost=0;
int id=0;
while(SPFA(s,t,flow,cost));
return cost;
}
}it;
int n,k;
vector<int >A;
int a[605][4];
void doit()
{
A.clear();
for (int i=1;i<=n;i++)
{scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i][2],&a[i][3]);
if (a[i][0]>a[i][2])
{
swap(a[i][0],a[i][2]);
swap(a[i][1],a[i][3]);
}
A.pb(a[i][0]);
A.pb(a[i][2]-1);
}
int sz;//全局变量
sort(A.begin(),A.end());
sz=unique(A.begin(),A.end())-A.begin();
int s=0,t=2*sz+1;
it.init(t+1);
it.AddEdge(0,1,inf,0);
it.AddEdge(t-1,t,inf,0);
LL ss;
for (int i=1;i<=n;i++)
{ int id1=lower_bound(A.begin(),A.begin()+sz,a[i][0])-A.begin();
int id2=lower_bound(A.begin(),A.begin()+sz,a[i][2]-1)-A.begin();
id1++; id2++;
ss=LL(sqrt(double((LL(a[i][0]-a[i][2]))*(a[i][0]-a[i][2])+(LL(a[i][1]-a[i][3]))*(a[i][1]-a[i][3]))));
if (id1>id2) it.AddEdge(id2+sz,id1,1,-ss);
if (id1==id2) it.AddEdge(id1,id2+sz,1,-ss);
if (id1<id2) it.AddEdge(id1+sz,id2,1,-ss);
}
for (int i=1;i<=sz;i++)
it.AddEdge(i,i+sz,k,0);
for (int i=1;i<sz;i++)
if (A[i]-A[i-1]==1)it.AddEdge(i+sz,i+1,inf,0);else it.AddEdge(i+sz,i+1,k,0);
cout<<-it.Mincost(s,t)<<endl;
}
int main()
{ int cas;
while (scanf("%d%d",&n,&k)!=EOF) doit();
return 0;
}
/*
4 2
1 2 7 3
6 5 8 3
7 8 10 5
9 6 13 9
2 1
1 5 3 5
1 5 100 5
2 1
1 5 2 5
1 5 100 5
2 1
0 5 3 5
1 5 100 5
2 1
0 5 2 5
1 5 100 5
3 2
1 5 1 50
2 5 2 50
1 5 1 50
3 2
1 0 1 100
1 0 1 100
0 0 2 0
*/
线性规划与网络流第22题 最长k可重线段问题 !!!!!!!!!!!!!!!!
最新推荐文章于 2022-09-15 14:51:01 发布