与最长K可重区间问题一样的解法,但是这道题却有很多需要注意的地方,譬如就是精度问题,一开始没考虑到sprt()里面的乘会爆了精度,然后交上去竟然是TLE,然后找的原因方向也没对,最后TLE了好几次,猜想会不会是爆了精度的原因然后交了,A。
这道题有很多处地方都特别的需要注意,尤其是拆点,还有一定要离散化一下(加速)。不离散化的化,写好一点有概率不T,反正我之前没找到精度问题的时候就是改了离散化。然后这里有一种奇怪的东西,叫做负环,我们在这里需要特别的考虑进去,因为题目中说到的是开线段的个数不大于K,而不是点集“≤K”,所以,我们得去处理这一层关系。拆点,一种特殊的拆点,我们对X区间(x1, x2)去改变成(x1<<1|1, x2<<1)但是有时候像是(5,5)这类的情况,我们又需要特别的再去反转一下x1、x2.
然后我们拆点完成就可以去跑了,Dijkstra还是SPFA都是可以跑的,Dijkstra快得多而已。
#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <limits> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #define lowbit(x) ( x&(-x) ) #define pi 3.141592653589793 #define e 2.718281828459045 #define INF 0x3f3f3f3f3f3f3f3f #define HalF (l + r)>>1 #define lsn rt<<1 #define rsn rt<<1|1 #define Lson lsn, l, mid #define Rson rsn, mid+1, r #define QL Lson, ql, qr #define QR Rson, ql, qr #define myself rt, l, r #define MP(x, y) make_pair(x, y) using namespace std; typedef unsigned long long ull; typedef long long ll; const int maxN = 1e3 + 7, S = 0; int N, K, T, _UP, a[maxN][2]; ll h[maxN], dist[maxN], lsan[maxN], dis[maxN]; struct Eddge { int nex, u, v; ll flow, cost; Eddge(int a=-1, int b=0, int c=0, ll d=0, ll f=0):nex(a), u(b), v(c), flow(d), cost(f) {} }; vector<Eddge> G[maxN]; inline void _add(int u, int v, ll flow, ll cost) { G[u].push_back(Eddge((int)G[v].size(), u, v, flow, cost)); G[v].push_back(Eddge((int)G[u].size() - 1, v, u, 0, -cost)); } struct node { int id; ll val; node(int a=0, ll b=0):id(a), val(b) {} friend bool operator < (node e1, node e2) { return e1.val > e2.val; } }; priority_queue<node> Q; int preP[maxN], preE[maxN]; inline ll MaxFlow_MinCost(ll Flow) { ll ans = 0; for(int i=0; i<=T; i++) h[i] = 0; while(Flow) { for(int i=0; i<=T; i++) dist[i] = INF; dist[S] = 0; while(!Q.empty()) Q.pop(); Q.push(node(S, 0)); while(!Q.empty()) { node now = Q.top(); Q.pop(); int u = now.id; if(dist[u] < now.val) continue; int len = (int)G[u].size(); for(int i=0, v; i<len; i++) { v = G[u][i].v; ll f = G[u][i].flow, c = G[u][i].cost; if(f && dist[v] > dist[u] + c - h[v] + h[u]) { dist[v] = dist[u] + c - h[v] + h[u]; preP[v] = u; preE[v] = i; Q.push(node(v, dist[v])); } } } if(dist[T] == INF) break; for(int i=0; i<=T; i++) h[i] += dist[i]; ll Capa = Flow; for(int u=T; u != S; u = preP[u]) Capa = min(Capa, G[preP[u]][preE[u]].flow); Flow -= Capa; ans += Capa * h[T]; for(int u = T; u != S; u = preP[u]) { Eddge &E = G[preP[u]][preE[u]]; E.flow -= Capa; G[E.v][E.nex].flow += Capa; } } return -ans; } inline void init() { T = _UP + 1; //for(int i=0; i<=T; i++) G[i].clear(); } int main() { // freopen("employee.in", "r", stdin); // freopen("employee.out", "w", stdout); scanf("%d%d", &N, &K); _UP = 1; for(int i=1, y1, y2; i<=N; i++) { scanf("%d%d%d%d", &a[i][0], &y1, &a[i][1], &y2); dis[i] = sqrt(1LL * (a[i][0] - a[i][1]) * (a[i][0] - a[i][1]) + 1LL * (y1 - y2) * (y1 - y2)); if(a[i][0] > a[i][1]) swap(a[i][0], a[i][1]); a[i][0] = (a[i][0] << 1) | 1; a[i][1] = a[i][1] << 1; if(a[i][0] > a[i][1]) swap(a[i][0], a[i][1]); lsan[(i<<1) - 1] = a[i][0]; lsan[i<<1] = a[i][1]; } sort(lsan + 1, lsan + (N<<1) + 1); _UP = (int)(unique(lsan + 1, lsan + (N<<1) + 1) - lsan - 1); init(); for(int i=0; i<=_UP; i++) _add(i, i + 1, K, 0); for(int i=1, x1, x2; i<=N; i++) { x1 = (int)(lower_bound(lsan + 1, lsan + _UP + 1, a[i][0]) - lsan); x2 = (int)(lower_bound(lsan + 1, lsan + _UP + 1, a[i][1]) - lsan); _add(x1, x2, 1, -dis[i]); } printf("%lld\n", MaxFlow_MinCost(K)); return 0; }