HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)
2013-09-15 23:43
555 查看
Description
At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives and Silicon civilization finally comes to its end with the Civil Union born from the ruins. The shadow fades away, and the new-born Union is opening a new page.
Although the war ended, the affect of the war is far from over. Now the council is busy fixing the transportation system. Before the war, all the stars were connected with artificial wormholes which were destroyed during the war. At the same time, natural wormholes are breaking down with the growing traffic. A new traffic system is on the schedule.
As two civilizations combine, the technology integrates. Scientists find a new traffic system called the Starloop System.
This system is made up of several starloops. People build a starway to connect two stars. A startloop is a closed path with no repetitions of stars or starways allowed, other than the repetition of the starting and ending star. And a starloop contains at least two starts and two starways. A startloop's cost is the sum of the length of all the starways in it. Length of a starway connecting two stars is floor(x), which x is the euclidean distance between two stars. You can build more than one starway between any two stars, but one starway can only belongs to one starloop.
View Code
At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives and Silicon civilization finally comes to its end with the Civil Union born from the ruins. The shadow fades away, and the new-born Union is opening a new page.
Although the war ended, the affect of the war is far from over. Now the council is busy fixing the transportation system. Before the war, all the stars were connected with artificial wormholes which were destroyed during the war. At the same time, natural wormholes are breaking down with the growing traffic. A new traffic system is on the schedule.
As two civilizations combine, the technology integrates. Scientists find a new traffic system called the Starloop System.
This system is made up of several starloops. People build a starway to connect two stars. A startloop is a closed path with no repetitions of stars or starways allowed, other than the repetition of the starting and ending star. And a starloop contains at least two starts and two starways. A startloop's cost is the sum of the length of all the starways in it. Length of a starway connecting two stars is floor(x), which x is the euclidean distance between two stars. You can build more than one starway between any two stars, but one starway can only belongs to one starloop.
#include <cstdio> #include <queue> #include <cstring> #include <cmath> using namespace std; const int MAXN = 210; const int MAXE = 210 * 210 * 2; const int INF = 0x3f3f3f3f; struct ZKW_flow{ int st, ed, ecnt, n; int head[MAXN]; int cap[MAXE], cost[MAXE], to[MAXE], next[MAXE]; void init(){ memset(head, 0, sizeof(head)); ecnt = 2; } void addEdge(int u, int v, int cc, int ww){ cap[ecnt] = cc; cost[ecnt] = ww; to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++; cap[ecnt] = 0; cost[ecnt] = -ww; to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++; } int dis[MAXN]; void SPFA(){ for(int i = 1; i <= n; ++i) dis[i] = INF; priority_queue<pair<int, int> > Q; dis[st] = 0; Q.push(make_pair(0, st)); while(!Q.empty()){ int u = Q.top().second, d = -Q.top().first; Q.pop(); if(dis[u] != d) continue; for(int p = head[u]; p; p = next[p]){ int &v = to[p]; if(cap[p] && dis[v] > d + cost[p]){ dis[v] = d + cost[p]; Q.push(make_pair(-dis[v], v)); } } } for(int i = 1; i <= n; ++i) dis[i] = dis[ed] - dis[i]; } int minCost, maxFlow; bool use[MAXN]; int add_flow(int u, int flow){ if(u == ed){ maxFlow += flow; minCost += dis[st] * flow; return flow; } use[u] = true; int now = flow; for(int p = head[u]; p; p = next[p]){ int &v = to[p]; if(cap[p] && !use[v] && dis[u] == dis[v] + cost[p]){ int tmp = add_flow(v, min(now, cap[p])); cap[p] -= tmp; cap[p^1] += tmp; now -= tmp; if(!now) break; } } return flow - now; } bool modify_label(){ int d = INF; for(int u = 1; u <= n; ++u) if(use[u]) for(int p = head[u]; p; p = next[p]){ int &v = to[p]; if(cap[p] && !use[v]) d = min(d, dis[v] + cost[p] - dis[u]); } if(d == INF) return false; for(int i = 1; i <= n; ++i) if(use[i]) dis[i] += d; return true; } int min_cost_flow(int ss, int tt, int nn){ st = ss, ed = tt, n = nn; minCost = maxFlow = 0; SPFA(); while(true){ while(true){ for(int i = 1; i <= n; ++i) use[i] = 0; if(!add_flow(st, INF)) break; } if(!modify_label()) break; } return minCost; } } G; struct Point { int x, y, z, w; void read() { scanf("%d%d%d%d", &x, &y, &z, &w); } int operator * (const Point &rhs) const { double xx = x - rhs.x, yy = y - rhs.y, zz = z - rhs.z; return (int)sqrt(xx * xx + yy * yy + zz * zz); } }; Point a[MAXN]; int n; int main() { while(scanf("%d", &n) != EOF && n) { int sumw = 0; for(int i = 1; i <= n; ++i) a[i].read(), sumw += a[i].w; G.init(); int ss = 2 * n + 1, tt = ss + 1; for(int i = 1; i <= n; ++i) { G.addEdge(ss, i, a[i].w, 0); G.addEdge(i + n, tt, a[i].w, 0); for(int j = i + 1; j <= n; ++j) { int cost = a[i] * a[j]; G.addEdge(i, j + n, INF, cost); G.addEdge(j, i + n, INF, cost); } } int ans = G.min_cost_flow(ss, tt, tt); if(sumw != G.maxFlow) ans = -1; printf("%d\n", ans); } }
View Code
相关文章推荐
- HDU 2013 ACM/ICPC Asia Regional Hangzhou Online ------ Zhuge Liang's Mines
- HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
- HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)
- hdu 4745 Two Rabbits 2013 ACM/ICPC Asia Regional Hangzhou Online
- hdu 4739 Zhuge Liang's Mines 2013 ACM/ICPC Asia Regional Hangzhou Online
- hdu 4738 Caocao's Bridges 2013 ACM/ICPC Asia Regional Hangzhou Online tarjan 模板
- hdu 4740 The Donkey of Gui Zhou 深搜(2013 ACM/ICPC Asia Regional Hangzhou Online 1003)
- HDU 4739 Zhuge Liang's Mines (2013 ACM/ICPC Asia Regional Hangzhou Online)
- HDU 4710 Balls Rearrangement && 2013 ACM/ICPC Asia Regional Online —— Warmup
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
- HDU 4731 Minimum palindrome 找规律 (2013 ACM/ICPC Asia Regional Chengdu Online 1004)
- hdu 4750 Count The Pairs 并查集+递推(2013 ACM/ICPC Asia Regional Nanjing Online 1003)
- HDU 4707 Pet && 2013 ACM/ICPC Asia Regional Online —— Warmup
- hdu 4727 The Number Off of FFF 简单题( 2013 ACM/ICPC Asia Regional Online —— Warmup2 1012)
- hdu 2013 ACM/ICPC Asia Regional Online —— Warmup2解题报告
- HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)
- HDU 4706 Children's Day && 2013 ACM/ICPC Asia Regional Online —— Warmup
- HDU 4714 Tree2cycle 求树的分叉数 && 2013 ACM/ICPC Asia Regional Online —— Warmup
- hdu 4734 F(x) 数位dp (2013 ACM/ICPC Asia Regional Chengdu Online 1007)
- HDU 4749 Parade Show(2013 ACM/ICPC Asia Regional Nanjing Online)