Codeforces 277e-binary Tree on Plane (Mincostmaxflow, simple)

Source: Internet
Author: User

Test instructions:
...
Idea:
splits each point u into two vertices u-in, u-out, but not connected. The
connects from S to U-in, with a capacity of 2, which limits the child nodes of U to a maximum of 2.
from U-out to the t edge, with a capacity of 1, limit u to only one father.
to Y[u]>y[v] y[u] > Y[v], with one edge (U, v), cost is distance.
then run Mincostmaxflow and check to see if the stream is full.

#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <map> #
Include <algorithm> #include <climits> #include <set> #include <queue> using namespace std;
typedef long Long LL;
#define REP (I, S, T) for (int (i) = (s);(i) <= (t), + + (i)) const int N = 400;
Const double INF = 1E10;
Const double EPS = 1e-9;
const int V = N * 2 + 2;

const int maxe = v * v;
    struct Edge {int cap, NXT, to;
Double cost;

};
    Class Mincostmaxflow {Public:edge e[maxe];
    int edges, head[v], bak[v], bake[v];
    Double Dis[v];

    BOOL Vis[v];
        void init () {edges = 0;
    Memset (Head,-1, sizeof (head));
        } void Add_edge (int u, int v, double w, int c) {e[edges] = (edge) {C, head[u], V, w};
        E[edges + 1] = (Edge) {0, head[v], U,-w};
        Head[u] = edges;
        HEAD[V] = edges + 1;
    edges + = 2;
} double sol (int source, int target) {double answer = 0;        while (true) {for (int i = 0; I <= target; ++i) {Vis[i] = 0, dis[i] = INF;
            } queue<int> Q;
            Vis[source] = 1;
            Dis[source] = 0;
            Q.push (source);
            memset (Bak,-1, sizeof (BAK));
            memset (Bake,-1, sizeof (bake));
                while (!q.empty ()) {int u = q.front (); Q.pop ();
                Vis[u] = 0; for (int i = head[u]; I! =-1;)
                    {Edge &e = e[i];
                    int v = e.to;
                        if (E.cap > 0 && dis[u] + e.cost + EPS < Dis[v]) {Dis[v] = Dis[u] + e.cost;
                        BAK[V] = u;
                        BAKE[V] = i;
                            if (!vis[v]) {vis[v] = 1;
                        Q.push (v);
                }} i = E.NXT;
          }  } if (bak[target] = =-1) break;
            Answer + = Dis[target];
                for (int i = target; i = source; i = Bak[i]) {e[bake[i]].cap-= 1;
            E[bake[i]^1].cap + = 1;
    }} return answer;
                } bool Check (int n, int root, int target) {for (int i = 0; i < n; ++i) if (i! = root) {
                int u = i + N; for (int j = Head[u]; J! =-1;)
                    {Edge &e = e[j];
                    if (e.to = = target) {if (E.cap > 0) return 0;
                } j = e.nxt;
    }} return 1;


}
};

int n, X[n], y[n];

Mincostmaxflow Sol;
    Double dist (int u, int v) {Double dx = x[u]-x[v], dy = y[u]-Y[V];
return sqrt (DX * dx + dy * dy);
    } int main () {#ifdef _loca_env_ freopen ("input.in", "R", stdin), #endif//_loca_env scanf ("%d", &n); for (iNT i = 0; I < n;
    ++i) {scanf ("%d%d", x + I, y + i);
    } int root_id = Max_element (y, y + N)-y; int Source = n << 1, target = n << 1 |
    1;
    Sol.init ();
        for (int i = 0; i < n; ++i) {Sol.add_edge (source, I, 0, 2);
    Sol.add_edge (i + N, target, 0, 1);
                } for (int i = 0, i < n; ++i) for (int j = 0; J < N; ++j) if (Y[i] > Y[j]) {
            Sol.add_edge (i, j + N, Dist (i, J), 1);

    } Double answer = Sol.sol (source, target);
            if (!sol.check (n, root_id, target)) {printf ("-1");
    return 0;
    } printf ("%.10f", answer);
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.