首页 技术 正文
技术 2022年11月16日
0 收藏 817 点赞 2,999 浏览 2075 个字
#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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898