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 ...
随机推荐
- json与String的转化
String转成jsonObject JsonObject json = JsonObject.fromObject(String str) String转成JsonArray J ...
- Minicom 简单使用
一. 什么是minicom 1.1. minicom 是linux 下的一个串口调试工具 二. minicom的使用 2.1. 进入设置 sudo minicom -s 2.1.1. 串口设置 i. ...
- # [爬虫Demo] pyquery+csv爬取猫眼电影top100
目录 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 代码君 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 https://maoyan.co ...
- php和java语法区别
Java和PHP的基本语法基本相同,其实大部分的语言的基本语法也都相同,但是他们还是有一些细微的区别: 1.PHP是一种脚本语言,代码在服务器上执行,而结果以纯文本返回浏览器. 2.PHP能够运行在各 ...
- 关于tomcat部署项目的问题
问题是这样的 之前用tomcat8.5部署的项目,结果启动项目一直三个端口被占用,浏览器也打不开目标网页 卸了8,装了9.先配置的一大堆,结果可以打开Tomcat的主页locahost:8080,到此 ...
- Linq Distinct 自定义比较
private class MyMenuComparer : IEqualityComparer { public bool Equals(ParMenu x, ParMenu y){ return ...
- Stream 分布式数据流的轻量级异步快照
1. 概述 分布式有状态流处理支持在云中部署和执行大规模连续计算,主要针对低延迟和高吞吐量.这种模式的一个最根本的挑战就是在可能的失败情况下提供处理保证.现有方法依赖于可用于故障恢复的周期性全局状态快 ...
- java的移位和异或运算
Java移位运算种类 基础:我们知道在Java中int类型占32位,可以表示一个正数,也可以表示一个负数.正数换算成二进制后的最高位为0,负数的二进制最高为为1 例子: -5换算成二进制后为:1111 ...
- 查看Linux服务器硬件信息
一:查看cpu# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数# 查看物理CPU个数cat /proc/cpui ...
- Layedit 编辑页面赋值
1.编辑页面 $("[name=Experience]").val(data.Experience);//直接赋值然后再进行build experience = layedit.b ...