#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#define mul(a) (a)*(a)
using namespace std; const int Maxn = ;
const int Maxm = ;
const int inf = 0x3f3f3f3f; struct ZKW_flow{
int st, ed, ecnt, n;
int head[Maxn];
int cap[Maxm], cost[Maxm], to[Maxm], next[Maxm]; void init(){
memset(head, -, sizeof(head));
ecnt = ;
} void add(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 ZKW(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;
int w[Maxn];
struct Point{
int x,y,z;
}p[Maxn];
int DIS(Point a,Point b)
{
return (int)sqrt(1.0*mul(a.x-b.x)+1.0*mul(a.y-b.y)+1.0*mul(a.z-b.z));
}
int main()
{
int n,i,j,sum;
while(scanf("%d",&n),n){
G.init();
sum=;
for(i=;i<=n;i++){
scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].z,&w[i]);
sum+=w[i];
}
for(i=;i<=n;i++){
G.add(,i,w[i],);
G.add(i+n,*n+,w[i],);
for(j=i+;j<=n;j++){
int cost=DIS(p[i],p[j]);
G.add(i,j+n,inf,cost);
G.add(j,i+n,inf,cost);
}
}
int ans=G.ZKW(,*n+,*n+);
if(G.maxFlow==sum)
printf("%d\n",ans);
else
printf("-1\n");
}
return ;
}

hdu 4744 最小费用流的更多相关文章

  1. HDU 4744 Starloop System(ZKW费用流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4744 题意:三维空间n个点,每个点有一个wi值.每对点的距离定义为floor(欧拉距离),每对点之间建 ...

  2. Going Home (hdu 1533 最小费用流)

    集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...

  3. hdu 1853 最小费用流好题 环的问题

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Tota ...

  4. HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)

    Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...

  5. HDU 6188最小费用流

    题目链接:http://hdu.hustoj.com/showproblem.php?pid=6118 掉坑里了,图很好建,Wa了一发,看了Disscuss里面有人提供了一组样例,画图发现:最小流模板 ...

  6. hdu 4494 最小费用流

    思路:这题我在下午重现的时候就用的费用流做,可是各种悲催的超时,只是我一开始的那种建图方式多了一个二分查找. 戏剧性的是,求距离的返回值写成int型了,CodeBlock编译器又没有警告,然后就WA啊 ...

  7. hdu 4411 最小费用流

    思路:主要就是要把一个每个城市拆为两个点,建一条容量为1,费用为-inf的边,保证每个城市都会被遍历. /*最小费用最大流*/ #include<iostream> #include< ...

  8. HDU 3667.Transportation 最小费用流

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 4067 hdoj 4067 Random Maze 最小费用流

    给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...

随机推荐

  1. C# 使用ping命令

    方法一:调用cmd 的ping命令 private static string CmdPing(string strIp) { Process p = new Process(); p.StartIn ...

  2. 面试过程中javascript原型链与作用域的问题

    现在校招也基本结束了,所以有时间把这段时间遇到的问题做个总结.在很多的笔试题目中,发现有很多对JS作用域方面的考察,所以查阅资料总结一下. 众所周知,js不像其他OOP语言那样,他是一种弱类型的语言, ...

  3. Django官方文档学习2——数据库及模板

    网址:https://docs.djangoproject.com/en/1.10/intro/tutorial02/ 1.扫描installed_apps,创建需要的数据库table python ...

  4. hdu 1861-游船出租

    游船出租                                                                                   Time Limit: 1 ...

  5. List排序忽略大小写

    public List<String> sortListIgnoreCase(List<String> list) {        Collections.sort(list ...

  6. 【ASP.NET】C# 将HTML中Table导出到Excel(TableToExcel)

    首先,说下应用场景 就是,把页面呈现的Table 导出到Excel中.其中使用的原理是 前台使用ajax调用aspx后台,传递过去参数值,导出.使用的组件是NPOI. 前台调用: <script ...

  7. Oracle数据库备份与恢复的常用方法

    Oracle数据库有三种常用的备份方法,分别是导出/导入(EXP/IMP).热备份和冷备份.导出/导入备份是一种逻辑备份,相对于导出/导入来说,热备份.冷备份是一种物理备份. 导出/导入(Export ...

  8. Delphi和JAVA用UTF-8编码进行Socket通信例子

    最近的项目(Delphi开发),需要经常和java语言开发的系统进行数据交互(Socket通信方式),数据编码约定采用UTF-8编码. 令我无语的是:JAVA系统那边反映说,Delphi发的数据他们收 ...

  9. HTML5 API——无刷新更新地址 history.pushState/replaceState 方法

    尽 管是上面讲到的<JavaScript高级程序设计>(第二版)中提到,BOM中的location.path/query…… (window.location)在通过JavaScript更 ...

  10. 【JavaScript】关于prototype原型的一些链接

    http://www.cnblogs.com/slowsoul/archive/2013/05/30/3107198.html http://www.thinksaas.cn/group/topic/ ...