CF 672C 两个人捡瓶子 最短路与次短路思想
2 seconds
256 megabytes
standard input
standard output
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.
We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each.
For both Adil and Bera the process looks as follows:
- Choose to stop or to continue to collect bottles.
- If the choice was to continue then choose some bottle and walk towards it.
- Pick this bottle and walk to the recycling bin.
- Go to step 1.
Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.
They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.
First line of the input contains six integers ax, ay, bx, by, tx and ty(0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively.
The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground.
Then follow n lines, each of them contains two integers xi and yi (0 ≤ xi, yi ≤ 109) — position of the i-th bottle.
It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.
Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .
3 1 1 2 0 0 1 1
2 1
2 3
11.084259940083
5 0 4 2 2 0 5 2
3 0
5 5
3 5
3 3
33.121375178000
Consider the first sample.
Adil will use the following path: .
Bera will use the following path: .
Adil's path will be units long, while Bera's path will be units long.
C题题意:二维平面上有n个(n<=10^5)点(xi,yi),有两个起点A(ax,ay)和B(bx,by),一个终点T(tx,ty)(0<=横纵坐标<=10^9)
可以从两个起点中的一个或两个出发,经过所有(xi,yi),且每到达一个(xi,yi)都要回到终点,问所有走过距离的最小值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-;
const int inf =0x7f7f7f7f;
const double pi=acos(-);
const int maxn=+; struct point{
double x,y;
}p[maxn];
point a,b,o;
int a1,a2,b1,b2;
double l[maxn+],mina,minb,seca,secb,ca1,ca2,cb1,cb2; double dis(point a,point b)
{
return pow((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y),0.5);
} void solvea(int i)
{
if(l[i]-dis(a,p[i])>mina)
{
seca=mina;
ca2=ca1;
mina=l[i]-dis(a,p[i]);
ca1=i;
}
else if(l[i]-dis(a,p[i])>seca)
{
seca=l[i]-dis(a,p[i]);
ca2=i;
}
} void solveb(int i)
{
if(l[i]-dis(b,p[i])>minb)
{
secb=minb;
cb2=cb1;
minb=l[i]-dis(b,p[i]);
cb1=i;
}
else if(l[i]-dis(b,p[i])>secb)
{
secb=l[i]-dis(b,p[i]);
cb2=i;
}
} int main()
{
while(~scanf("%lf %lf %lf %lf %lf %lf",&a.x,&a.y,&b.x,&b.y,&o.x,&o.y))
{
mina=minb=seca=secb=-1e20;
ca1=,ca2=,cb1=,cb2=;
int n;double ans=; scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
l[i]=dis(p[i],o);
ans+=*l[i];
solvea(i);
solveb(i);
} if(ca1!=cb1)
{
if(mina<||minb<)
ans-=max(mina,minb);//刚开始没有加这句就wa了,因为即使两人最优的瓶子不是同
//一个,却可能存在其中有人的收益为负的情况,这种情况下就应该取两者中收益较大的一个(满足至少一个,可以///只有一个)
else
ans-=mina+minb;
}
else
{
if(mina<||minb<)
ans-=max(mina,minb);
else
{
if(mina+max(secb,0.0)>minb+max(seca,0.0))
ans-=mina+max(secb,0.0);
else ans-=minb+max(seca,0.0);
}
}
printf("%.16f\n",ans);
}
return ;
}
分析:用图论里的最短路和次短路的思想记录下最短路和次短路就好,,其实写的有点挫,可以直接用pair<double,int>再sort排序就好,,最关键的是,,要分情况讨论,因为两个人至少有一个要去捡,也就是说可以只有一个,所以应分情况讨论,错误点见代码
CF 672C 两个人捡瓶子 最短路与次短路思想的更多相关文章
- 最短路和次短路问题,dijkstra算法
/* *题目大意: *在一个有向图中,求从s到t两个点之间的最短路和比最短路长1的次短路的条数之和; * *算法思想: *用A*求第K短路,目测会超时,直接在dijkstra算法上求次短路; ...
- UESTC30-最短路-Floyd最短路、spfa+链式前向星建图
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
- hdu1688(dijkstra求最短路和次短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题意:第k短路,这里要求的是第1短路(即最短路),第2短路(即次短路),以及路径条数,最后如果最 ...
- POJ 3463 Sightseeing 【最短路与次短路】
题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...
- POJ - 3463 Sightseeing 最短路计数+次短路计数
F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ---3463 Sightseeing 记录最短路和次短路的条数
Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9247 Accepted: 3242 Descr ...
- CF 672C Recycling Bottles[最优次优 贪心]
C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CF 586B 起点到终点的最短路和次短路之和
起点是右下角 终点是左上角 每次数据都是两行的点 输入n 表示有n列 接下来来的2行是 列与列之间的距离 最后一行是 行之间的距离 枚举就行 Sample test(s) input 41 ...
随机推荐
- Ansible 清单与命令解析
在大规模的配置管理工作中我们需要管理不同业务的不同机器,这些机器的信息都存放在 Ansible 的 Inventory 组件里面,在我们工作中配置部署针对的主机必须先存放在 Invento 组里面,这 ...
- java代码--Date类获取当前时间-格式化输出
44:52 阅读数:2299 package cn.Date; import java.text.Format; import java.text.SimpleDateFormat; import ...
- Zookeeper报错Will not attempt to authenticate using SASL解决办法
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq642159746/article/ ...
- 为什么单线程的Redis却能支撑高并发
Redis的高并发和快速原因 redis是基于内存的,内存的读写速度非常快: 核心是基于非阻塞的IO多路复用机制: redis是单线程的,反而省去了很多上下文切换线程的时间: 为什么Redis是单线程 ...
- ZeroMQ 三种模式python3实现
ZeroMQ是一个消息队列网络库,实现网络常用技术封装.在C/S中实现了三种模式,这段时间用python简单实现了一下,感觉python虽然灵活.但是数据处理不如C++自由灵活. Request-Re ...
- Alpha阶段评审
组别 项目名称 学生互评 助教评分 点评建议 第1组 校园物资共享平台 7.97 6.17 界面较为简洁美观,实现物品信息发布,登录注册和个人信息等功能,交易功能待完善: 博客内容比较完善,有alp ...
- 13、Nginx七层负载均衡
1.Nginx负载均衡基本概述 1.1为什么需要使用负载均衡 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡, ...
- EfficientNet学习笔记
EfficientNet是谷歌大脑在2019年提出的,论文地址是:https://arxiv.org/pdf/1905.11946.pdf 这篇文章主要想解决的一个问题是,如何平衡网络的深度.宽度和分 ...
- python-装饰(高阶函数)
python-装饰(高阶函数) 高阶函数 1.把一个函数名当做实参传给另外一个函数(在不修改被装饰函数源代码) 2.返回值 中包含函数名 高阶函数实现1的功能 def bar(): print(&qu ...
- 白盒测试之JUnit与SpringTest的完美结合
通过白盒的单元测试可以验证程序基本功能的有效性,从而保证整个系统的质量,功在一时,利在千秋.目前80%以上公司后台还是基于java,尤其是后台大量采用Spring框架,我们这里采用Junit和Spri ...