Codeforces 277E
按边建模,二叉树一条入边两条出边
判断就要用到mcmf的好处了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+11;
const int oo = 0x3f3f3f3f;
int to[maxn<<1],nxt[maxn<<1];
double cap[maxn<<1],flow[maxn<<1];
double cost[maxn<<1];
int head[maxn],tot;
void init(){
memset(head,-1,sizeof head);
tot=0;
}
void add(int u,int v,double c,double w){
to[tot]=v;
cap[tot]=c;
flow[tot]=0;
cost[tot]=w;
nxt[tot]=head[u];
head[u]=tot++;
swap(u,v);
to[tot]=v;
cap[tot]=0;
flow[tot]=0;
cost[tot]=-w;
nxt[tot]=head[u];
head[u]=tot++;
}
struct QUEUE{
int que[maxn];
int front,rear;
void init(){front=rear=0;}
void push(int x){que[rear++]=x;}
int pop(){return que[front++];}
bool empty(){return front==rear;}
}que;
int n,m,s,t;
bool vis[maxn];
int pre[maxn];
double dis[maxn];
bool spfa(){
que.init();
memset(vis,0,sizeof vis);
memset(pre,-1,sizeof pre);
for(int i = 0; i < n+12; i++) dis[i]=oo;
que.push(s);vis[s]=1;dis[s]=0;
while(!que.empty()){
int u=que.pop(); vis[u]=0;
for(int i = head[u]; ~i; i = nxt[i]){
int v=to[i];
double c=cap[i],f=flow[i],w=cost[i];
if(c>f&&dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
pre[v]=i;
if(!vis[v]){
que.push(v);
vis[v]=1;
}
}
}
}
if(dis[t]==oo) return 0;
else return 1;
}
double mcmf(double &mf){
double mc=0;
mf=0;
while(spfa()){
double tf=oo+1;
for(int i = pre[t]; ~i; i = pre[to[i^1]]){
tf=min(tf,cap[i]-flow[i]);
}
mf+=tf;
for(int i = pre[t]; ~i; i = pre[to[i^1]]){
flow[i]+=tf;
flow[i^1]-=tf;
}
mc+=dis[t]*tf;
}
return mc;
}
#define rep(i,j,k) for(int i = j; i <= k; i++)
#define repp(i,j,k) for(int i = j; i < k; i++)
#define repe(i,u) for(int i = head[u]; ~i; i = nxt[i])
#define scan(a) scanf("%d",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define println(a) printf("%d\n",a)
#define printbk(a) printf("%d ",a)
#define print(a) printf("%d",a)
double x[maxn],y[maxn];
double ddis(double a,double b,double c,double d){
return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
int n1;
int chai(int x){
return x+n1;
}
int main(){
while(scan(n1)!=EOF){
init();
rep(i,1,n1){
scanf("%lf%lf",&x[i],&y[i]);
}
s=2*n1+1;t=s+1;n=t;
rep(i,1,n1){
add(s,i,2,0);
add(chai(i),t,1,0);
}
rep(i,1,n1){
rep(j,1,n1){
if(i==j)continue;
double ddd=ddis(x[i],y[i],x[j],y[j]);
if(y[i]>y[j]) add(i,chai(j),1,ddd);
}
}
// bool flag=0;
// double mx=-oo;
// rep(i,1,n1) mx=max(mx,y[i]);
// bool tmp=0;
double mf;
double ans=mcmf(mf);
if(mf!=n1-1) println(-1);
else printf("%.15lf\n",ans);
// repe(i,t){
// int v=to[i]-n1;
// double c=cap[i^1],f=flow[i^1],cost[i^1];
// }
// if(flag) println(-1);
// else printf("%.15lf\n",mcmf());
}
return 0;
}
Codeforces 277E的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- day35 02-Hibernate持久化对象状态及状态转换
hibernate内置有一个c3p0,不用引入c3p0的jar包也行. 现在其实可以不用去创建表和实体类.因为hibernate可以自动帮我们生成.只要把映射建好了它就可以自动帮我们生成. 创建实体类 ...
- cocos2d-js 浏览器与JSB内存管理机制的不同
写这边文章的主要目的是为了理解使用cocos3d-js开发app时,浏览器调试与真机情况不一致的原因 一.浏览器中内存管理机制 HTML5版本运行时,整个游戏只存在JS脚本与一些必要的资源文件,这时候 ...
- 生产者与消费者-1:N-基于list
一个生产者/多个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...
- Opengl创建机器人手臂代码示例
/*******************************************************robot.cpp*基于opengl的机械手臂示例代码*s:机械臂逆时针旋转*S:机械臂 ...
- Python 黑客 004 用Python构建一个SSH僵尸网络 01 简介
用Python构建一个SSH僵尸网络 01 简介 一. 构建一个SSH僵尸网络的流程图: Created with Raphaël 2.1.0手动操作,实现通过SSH连接目标服务器(手动)用 Pexp ...
- 【Boost】boost库中timer定时器 2
博客转载自:http://blog.csdn.net/yockie/article/details/40386145 先跟着boost文档中asio章节的指南中的几个例子学习一下使用: 所有的Asio ...
- php学习笔记-POST和GET的区别
POST和GET都可以用来提交数据. POST方法提交的数据在浏览器的地址栏是不可见的,当然利用一些工具是可以看到的,而GET方法提交的数据在地址栏是可见的.两者比较,POST更安全一点. POST方 ...
- 算法Sedgewick第四版-第1章基础-004一封装交易对象
1. package ADT; /****************************************************************************** * Co ...
- kaggle Titanic
# coding: utf-8 # In[19]: # 0.78468 # In[20]: import numpy as np import pandas as pd import warnings ...
- python部分运算符理解
1.//取整除 5//3得到1 2.%取余 5%3得到2 3.<<左移 2<<2得到8 2用二进制表示为10,向左移两位得到1000,即十进制的8 4.>>右移 1 ...