codeforces 277E - Binary Tree on Plane (MinCostMaxFlow, 简单)
来源:互联网 发布:水果老虎机算法 编辑:程序博客网 时间:2024/06/04 18:38
题意:
。。。
思路:
将每个点 u 拆成两个顶点 u-in, u-out, 但不连边。
从s向u-in连边, 容量为2, 限制u的子节点最大为2。
从u-out向t连边, 容量为1, 限制u只有一个父亲。
对
然后跑 MinCostMaxFlow, 最后check一下是否满流。
#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;}
0 0
- codeforces 277E - Binary Tree on Plane (MinCostMaxFlow, 简单)
- 【Codeforces Round #170】Codeforces 277E Binary Tree on Plane
- Codeforces Round #170 (Div. 1) E.Binary Tree on Plane
- [Codeforces 277E][Round #170 div.1 E]Binary Tree on Plane
- Codeforces Round #170 (Div. 1) E. Binary Tree on Plane(费用流)
- Codeforces Round #319 (Div. 2)E Points on Plane
- codeforces 600 E. Lomsat gelral (dsu on the tree)
- codeforces 208 E. Blood Cousins (dsu on the tree)
- [Dsu on tree]CodeForces 600 E
- Codeforces Round #319 (Div. 2) E - Points on Plane, 第一次做“构造法”
- codeforces 576C Points on Plane
- Codeforces 725B Food on the Plane
- codeforces 725B Food on the Plane
- CodeForces 725B Food on the Plane
- codeforces #250E The Child and Binary Tree 快速傅里叶变换
- Codeforces 438E The Child and Binary Tree
- codeforces 246 E. Blood Cousins Return (set+dsu on the tree)
- Codeforces 600E Lomsat gelral (DSU on Tree)
- jq-基本知识
- -Dmaven.multiModuleProjectDirectory system propery is not set.
- STL
- Setting Up Continuous Integration & Continuous Deployment With Jenkins
- 欢迎使用CSDN-markdown编辑器
- codeforces 277E - Binary Tree on Plane (MinCostMaxFlow, 简单)
- Checking for Undefined, Null, and Empty Variables in JavaScript
- jq-跨域请求
- 我真的不懂微信营销(一)
- failed to sync branch You might need to open a shell and debug the state of this repo.
- 单链表实现插入排序
- 生命是不断努力,持续累积的结果
- 系统启动及进程管理
- php递归获取顶级父类id