http://codeforces.com/contest/277/problem/E
题意:
给你N个点,每个点有x,y 坐标。
yi > yj的时候i可以向j连边,长度为距离。
现在要求最小有向二叉树(边和最小)。
题解:
如果没有二叉树的限制,就是最小生成树裸题。
现在每个点有两个出度一个入度可以考虑网络流。
把每个点拆成两个点,一个用来出,一个用来入即可。
代码:
#include <bits/stdc++.h>
#ifdef LOCAL
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define debug(x) 1;
#endif
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lowbit(x) x&-x
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<b;i++)
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const double PI = acos (-1.);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5;
int x[1111], y[2222];
const int MAXM = 222222;
struct Edge {
int to, next, cap, flow;
double cost;
} edge[MAXM];
int head[MAXN], tol;
int pre[MAXN];
double dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从 0~N-1
void init (int n) {
N = n;
tol = 0;
memset (head, -1, sizeof (head) );
}
void addedge (int u, int v, int cap, double cost) {
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa (int s, int t) {
queue<int>q;
for (int i = 0; i < N; i++) {
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push (s);
while (!q.empty() ) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if (!vis[v]) {
vis[v] = true;
q.push (v);
}
}
}
}
if (pre[t] == -1) return false;
else return true;
}
//返回的是最大流, cost 存的是最小费用
int minCostMaxflow (int s, int t, double &cost) {
int flow = 0;
cost = 0;
while (spfa (s, t) ) {
int Min = INF;
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
if (Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
edge[i].flow += Min;
edge[i ^ 1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
}
double getdis (double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
#ifdef LOCAL
freopen ("input.txt", "r", stdin);
#endif
int n;
scanf ("%d", &n);
init(2 * n + 5);
for (int i = 1; i <= n; i++)
scanf ("%d %d", &x[i], &y[i]);
int st = 0, en = 2 * n + 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (y[i] > y[j]) {
addedge(i + n, j , 1, getdis(x[i], y[i], x[j], y[j]));
}
}
}
for (int i = 1; i <= n; i++) {
addedge(st, i + n, 2, 0);
addedge(i, en, 1, 0);
}
double cost;
int flow = minCostMaxflow(st, en, cost);
// debug(flow)
if (flow == n - 1) printf ("%.9lf\n", cost);
else puts ("-1");
return 0;
}