1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cstdlib> #include <cmath> #include <queue> #define maxn 25005 using namespace std; typedef long long ll;
template <typename T>void read(T &t) { t=0;char c=getchar();ll f=0; while(!isdigit(c)){f|=c=='-';c=getchar();} while(isdigit(c)){t=t*10+c-'0';c=getchar();} if(f)t=-t; }
const ll inf=0x3f3f3f3f3f3f3f3f; ll n,K; ll xa[maxn],xb[maxn],z[maxn]; ll s,t; ll ansc;
struct edge { ll u,v,f,c,nxt; }g[maxn];
ll head[maxn],ecnt=1; void eADD(ll u,ll v,ll f,ll c) { g[++ecnt].u=u; g[ecnt].v=v; g[ecnt].f=f; g[ecnt].c=c; g[ecnt].nxt=head[u]; head[u]=ecnt; }
inline ll getz(ll xa,ll xb,ll ya,ll yb) { return sqrt((xa-xb)*(xa-xb)+(ya-yb)*(ya-yb)); }
ll dist[maxn],inq[maxn],minf[maxn]; ll pree[maxn],prev[maxn]; bool SPFA() { memset(dist,0x3f,sizeof(dist)); memset(minf,0x3f,sizeof(minf)); queue<int> q; dist[s]=0; inq[s]=1; q.push(s); while(!q.empty()) { ll u=q.front(); q.pop(); inq[u]=0; for(register ll i=head[u];i;i=g[i].nxt) { ll v=g[i].v; if(g[i].f && dist[v]>dist[u]+g[i].c) { prev[v]=u,pree[v]=i; dist[v]=dist[u]+g[i].c; minf[v]=min(minf[u],g[i].f); if(!inq[v]) { inq[v]=1; q.push(v); } } } } return dist[t]<inf; }
int main() { read(n),read(K); ll ocr[maxn<<1]; for(register ll i=1;i<=n;++i) { ll ya,yb; read(xa[i]),read(ya),read(xb[i]),read(yb),z[i]=getz(xa[i],xb[i],ya,yb); xa[i]<<=1,xb[i]<<=1; if(xa[i]>xb[i])swap(xa[i],xb[i]); if(xa[i]==xb[i])xb[i]|=1; else xa[i]|=1; ocr[++ocr[0]]=xa[i],ocr[++ocr[0]]=xb[i]; } sort(ocr+1,ocr+ocr[0]+1); ocr[0]=unique(ocr+1,ocr+ocr[0]+1)-ocr-1; for(register ll i=1;i<=n;++i) xa[i]=lower_bound(ocr+1,ocr+ocr[0]+1,xa[i])-ocr,xb[i]=lower_bound(ocr+1,ocr+ocr[0]+1,xb[i])-ocr; s=0,t=ocr[0]+1; eADD(s,1,K,0),eADD(1,s,0,0); eADD(ocr[0],t,K,0),eADD(t,ocr[0],0,0); for(register ll i=1;i<ocr[0];++i) eADD(i,i+1,inf,0),eADD(i+1,i,0,0); for(register ll i=1;i<=n;++i) eADD(xa[i],xb[i],1,-z[i]),eADD(xb[i],xa[i],0,z[i]); while(SPFA()) { ansc+=dist[t]*minf[t]; for(register ll i=t;i!=s;i=prev[i]) { g[pree[i]].f-=minf[t]; g[pree[i]^1].f+=minf[t]; } } printf("%lld",-ansc); return 0; }
|