HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description
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.

As the picture above shows, there are two starloops. One is blue and the other one is brown.
As a starloop is set up, each star on the starloop will get a unit of star-energy. So the two blue stars can get one unit of star-energy, and at the same time the black two stars can get two units because they both belong to two starloops. When a star earns a certain number of energy units, the transporter on that star will be activated. One can easily travel between any two stars whose transporter is activated.
Now the council wants to know the minimal cost to build a starloop system on all the stars . In other words, every star's transporter should be activated
Input
For each test case:
There is a line with one integer n which is the number of stars.
The following n lines each describes a star by four integers xi, yi, zi and wi, defined as the spatial coordinate and the number of energy units the star needs to activate the transporter. Please NOTE that getting more than wi energy units will put the star in a dangerous situation, so it is not allowed.
The input ends with n = 0.
1<=n<=100
|xi|,|yi|,|zi|<=200
wi<=50
Output
题目大意:空间上n个点,每个点有一个wi。每对点的距离定义为floor(欧拉距离),每对点之间建一条边的费用为两点间的距离,每对点之间可以建多条边。现要求对每一个点 i ,都在 wi 个简单环上(每个点每条边都只经过一次),每条边只能属于一个简单环(随你选择属于哪个),简单环的费用为sum{每条边的费用},问最小的建环费用。
思路:每个点拆成a、b两个点,从附加源点S到a连一条边,容量为wi,费用为0;从b到附加汇点T连一条边,容量为wi,费用为0。每两个点i, j之间,ai到bj连一条边,bi到aj连一条边,费用均为i, j的距离,容量均为无穷大。若最大流=sum{wi},那么有解,输出最小费用,否则输出-1。
我也不会证明,我比赛的时候觉得这样应该可以构造出解,但是没写……可是后来比赛结束试了一下1A了T_T……等我想到为什么是对的再回来补证明……
好了我回来补证明了,经过本菜的苦思冥想,最终我认为,我看错题了,打开题目再看了一次,真的看错题了……我当时想那样建图只是直觉觉得那样可以过样例没想到能AC……
好了正题,这里准备借用无源汇上下界网络流的思想,不会的可以翻我以前的blog
对每一个点,拆成两个点a、b,b到a连一条边,容量上界为wi,下界为wi,对不同的两个点,分别a到b连一条边,容量上界为正无穷大,下界为零。
那么,做无源汇上下界网络最小费用流,当且仅当下界都满的时候,对于每个点 i (即边bi→ai)都在 wi 个简单环上。
这是因为对于无源汇上下界网络流,只能以环的形式进行增广,因为要求费用尽量小,那么每次找出的环一定都是简单环(多过几个就不划算了)。
而流每增加1,就等同于建一个环的边,所以每条边都只会属于一个简单环。
简化一下就是最初所说的建图了。
代码(500MS):
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std; const int MAXN = ;
const int MAXE = * * ;
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, , sizeof(head));
ecnt = ;
} 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] = ; cost[ecnt] = -ww; to[ecnt] = u;
next[ecnt] = head[v]; head[v] = ecnt++;
} int dis[MAXN]; void SPFA(){
for(int i = ; i <= n; ++i) dis[i] = INF;
priority_queue<pair<int, int> > Q;
dis[st] = ;
Q.push(make_pair(, 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 = ; 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^] += tmp;
now -= tmp;
if(!now) break;
}
}
return flow - now;
} bool modify_label(){
int d = INF;
for(int u = ; 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 = ; 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 = ;
SPFA();
while(true){
while(true){
for(int i = ; i <= n; ++i) use[i] = ;
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 = ;
for(int i = ; i <= n; ++i) a[i].read(), sumw += a[i].w;
G.init();
int ss = * n + , tt = ss + ;
for(int i = ; i <= n; ++i) {
G.addEdge(ss, i, a[i].w, );
G.addEdge(i + n, tt, a[i].w, );
for(int j = i + ; 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 = -;
printf("%d\n", ans);
}
}
HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)的更多相关文章
- HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Problem Description Mex is a function on a set of integers, which is universally used for impartial ...
- hdu 4747 Mex (2013 ACM/ICPC Asia Regional Hangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 思路: 比赛打得太菜了,不想写....线段树莽一下 实现代码: #include<iost ...
- [2013 ACM/ICPC Asia Regional Hangzhou Online J/1010]hdu 4747 Mex (线段树)
题意: + ;];;;], seg[rt << | ]);)) * fa.setv;) * fa.setv;;], seg[rt << | ], r - l + );;, ...
- HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon ...
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
- HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)
Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...
- [2013 ACM/ICPC Asia Regional Nanjing Online C][hdu 4750]Count The Pairs(kruskal + 二分)
http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意: 定义f(u,v)为u到v每条路径上的最大边的最小值..现在有一些询问..问f(u,v)>=t ...
随机推荐
- AngularJS表格排序
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- SQL Server笔记——sql语句创建数据库
MS SQLServer的每个数据库包含: 1个主数据文件(.mdf)必须. 1个事务日志文件(.ldf)必须. 可以包含: 任意多个次要数据文件(.ndf) 多个事务日志文件 CREATE DATA ...
- Openresty最佳案例 | 第1篇:Nginx介绍
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616591 本文出自方志朋的博客 Nginx 简介 Nginx是一个高性能的Web 服务 ...
- MySQL配置和启动
1.首先下载MySQl安装包,解压安装包 打开mysql下面的bin文件夹,双击如图标记的 .exe运行 2.配置Mysql (1)运行程序后点击 next (2)这里有精确配置和标准配置,根据情况 ...
- C.Sum 2017 ACM-ICPC 亚洲区(西安赛区)网络赛
题目来源:Sum 限制:1000ms 32768K Define the function S(x) for xx is a positive integer. S(x) equals to the ...
- hdu_1788_Chinese remainder theorem again (lcm
我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,…,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2(mod m2) … x≡ak(mod m ...
- Linux-history的用法
history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...] history的作用是显示或操纵历史 ...
- 网站安全检测 漏洞检测 对thinkphp通杀漏洞利用与修复建议
thinkphp在国内来说,很多站长以及平台都在使用这套开源的系统来建站,为什么会这么深受大家的喜欢,第一开源,便捷,高效,生成静态化html,第二框架性的易于开发php架构,很多第三方的插件以及第三 ...
- Leecode刷题之旅-C语言/python-118杨辉三角
/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...
- 50条大牛C++编程开发学习建议
每个从事C++开发的朋友相信都能给后来者一些建议,但是真正为此进行大致总结的很少.本文就给出了网上流传的对C++编程开发学习的50条建议,总结的还是相当不错的,编程学习者(不仅限于C++学习者)如果真 ...