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 ...
随机推荐
- 【转帖】msvcp100.dll和msvcr100.dll
VS发布软件时去除msvcp100.dll和msvcr100.dll图解说明 https://blog.csdn.net/yu__jia/article/details/82753262 msvcp. ...
- Centos 7 下Gitlab 自启动设置
禁止 Gitlab 开机自启动: systemctl disable gitlab-runsvdir.service 启用 Gitlab 开机自启动: systemctl enable gitlab- ...
- failed to push some refs to 'git@github.com:cq1415583094/MyBatis.git'解决办法
将本地git仓库代码提交到GitHub上时,出现failed to push some refs to 'git@github.com:cq1415583094/MyBatis.git', 导致的原因 ...
- 有人向你扔了一个bug,哈哈哈哈
有人向你扔了一个bug. "26楼会议室的灯亮着.它应该是熄灭着的." bug的备注里写道"你应该能在5分钟内搞定,只要按一下开关就好了."你去了26楼的会议室 ...
- http的导图
- Java并发理论简介
这些文字来自于Java程序员修炼之道,记录一下 一. java线程模型 Java线程模型建立在两个基本概念之上 共享的,默认可见的可变状态 抢占式线程调度 我们从侧面思考一下这两个概念 所有线程可以很 ...
- 简化SpringMVC配置
映射器处理器和适配器是可以省略的 为什么可以省略?因为有默认配置 SpringMVC的默认配置
- Nginx之常用操作
1) 将XXX.com 重定向到 www.XXX.com server { client_max_body_size 20m; listen ; server_name www.xxx.com xxx ...
- angular.js,IE7,8,9兼容性的处理
转........... 这段时间详细了解了谷歌新出的MVVM框架angular.js,并直接在本人所从事的项目中使用了.但是使用新东西都是有风险的,这不,采用了新框架的页面IE7,8各种显示不出来… ...
- 微信小程序获得高度
wx.getSystemInfo({ success: (res) => { wx.createSelectorQuery().select('#scrollbox').boundingClie ...