hdu 4744 最小费用流
#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 最小费用流的更多相关文章
- HDU 4744 Starloop System(ZKW费用流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4744 题意:三维空间n个点,每个点有一个wi值.每对点的距离定义为floor(欧拉距离),每对点之间建 ...
- Going Home (hdu 1533 最小费用流)
集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...
- hdu 1853 最小费用流好题 环的问题
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others) Tota ...
- 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 ...
- HDU 6188最小费用流
题目链接:http://hdu.hustoj.com/showproblem.php?pid=6118 掉坑里了,图很好建,Wa了一发,看了Disscuss里面有人提供了一组样例,画图发现:最小流模板 ...
- hdu 4494 最小费用流
思路:这题我在下午重现的时候就用的费用流做,可是各种悲催的超时,只是我一开始的那种建图方式多了一个二分查找. 戏剧性的是,求距离的返回值写成int型了,CodeBlock编译器又没有警告,然后就WA啊 ...
- hdu 4411 最小费用流
思路:主要就是要把一个每个城市拆为两个点,建一条容量为1,费用为-inf的边,保证每个城市都会被遍历. /*最小费用最大流*/ #include<iostream> #include< ...
- HDU 3667.Transportation 最小费用流
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
随机推荐
- SQL SERVER 数据库表同步复制 笔记
SQL SERVER 数据库表同步复制 笔记 同步复制可运行在不同版本的SQL Server服务之间 环境模拟需要两台数据库192.168.1.1(发布),192.168.1.10(订阅) 1.在发布 ...
- Toast在关闭应用后还显示的解决办法
1.我们在用Toast的用法就是:Toast.makeText(Context,CharSequence , Duration).show().但有的时候如果你在一次操作当中多次点击一个view的时候 ...
- application与cache
每个项目都有一些全局,常用的信息,而这些信息如果在每次使用时都载入,那必将耗费很大的资源,特别是对访问压力大的系统.因此,这个情况中,把这些全局信息放到缓存中是很必要的,放在缓存中可以使得数据能够很快 ...
- (剑指Offer)面试题28:字符串的排列
题目: 输入一个字符串,打印出该字符串中字符的所有排列. 例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 思路: 把一个字符串看 ...
- 7. 泛化(Generalization)
什么是泛化关系?用一个例子简单的说:假设A是B和C的父类,B.C具有公共类(父类)A,说明A是B.C的一般化(概括,也称泛化),B.C是A的特殊化. 在编程上,泛化关系(Generalization) ...
- CMSIS Example - osMessageQ osMessagePut osMessageGet
#include "cmsis_os.h" void Thread0( void * arg); void Thread1( void * arg); osThreadDef( T ...
- MySQL 多会话之间更新数据的小实例
1:创建一个实验表 mysql> use test; mysql> CREATE TABLE t -> (id int(11) NOT NULL DEFAULT 0, -> n ...
- 给eclipse装一些插件
最近换了一个环境,需要折腾一个新的eclipse配置,所以在这里记录下. 1.安装marketplace help=>install,输入地址:http://download.eclipse.o ...
- cdoj 30 最短路 flyod
最短路 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/30 Descript ...
- 32位Windows7上8G内存使用感受+xp 32位下使用8G内存 (转)
32位Windows7上8G内存使用感受+xp 32位下使用8G内存 博客分类: Windows XPWindowsIE企业应用软件测试 我推荐做开发的朋友:赶快加入8G的行列吧....呵呵..超爽 ...