https://www.nowcoder.com/acm/contest/201/L

题意:给你两条平行的直线和n个圆,在直线上面行走和在圆上和在圆内行走不需要耗费体力,除了这些区域外平面上经过任意两点需要走的距离都是欧几里得距离,求从直线1走到直线2所需要消耗的最小体力是多少。

题解:一开始以为是计算几何,但是冷静分析了一波后发现这个题要用最短路写,建边过程有点复杂:

1.从直线1到直线2要建边。

2.有n<1000个点,每个圆之间都应该有边,所以n方将所有圆连接起来,记得在圆内走和在圆上走不消耗体力需要处理一下。

3.从直线1到每个圆需要建边,从直线2到每个圆需要建边。

tips:最后建边的条数为2*(n*n+2*n+2)条,所以要把存边的数组开到1e6

建边完成后就简单的跑最短路就好,记得所有变量都要用double不容易出错

代码如下:

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e6+;
const int inf = 2.1e9;
const LL INF = ;
const int MOD = 1e9+;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
double dpow(double a,LL b){double ans=1.0;while(b){if(b%)ans=ans*a;a=a*a;b/=;}return ans;}
int n;
struct point{
int x,y;
int r;
double d1,d2;
}p[maxn];
double Dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double dis1(int a,int b,int c1,int c2){
return abs(c1-c2)/sqrt(a*a+b*b);
}
double dis2(int a,int b,int c,int x,int y){
return abs(a*x+b*y+c)/sqrt(a*a+b*b);
}
struct node{
int v,nxt;
double w;
bool operator<(const node p)const{return p.w<w;}
}edge[maxn];
int head[maxn];
int tot;
void add(int u,int v,double w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].nxt=head[u];
head[u]=tot++;
}
double dis[maxn];
bool vis[maxn];
void dij(){
fill(dis+, dis + n+, inf);
dis[n+]=;
priority_queue<node>q;
node tmp;
tmp.v=n+;
tmp.w=;
q.push(tmp);
while(!q.empty()){
tmp=q.top();
q.pop();
if(!vis[tmp.v]){
vis[tmp.v]=;
int u=tmp.v;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w){
dis[v]=dis[u]+edge[i].w;
tmp.w=dis[v];
tmp.v=v;
q.push(tmp);
}
}
}
}
}
int main(){
#ifndef ONLINE_JUDGE
FIN
#endif
int a,b,c1,c2;
cin>>n>>a>>b>>c1>>c2;
int ans=dis1(a,b,c1,c2);
// fuck(ans);
memset(head,-,sizeof(head));
tot=;
add(n+,n+,ans);
add(n+,n+,ans);
for(int i=;i<=n;i++){
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].r);
p[i].d1=max(0.0,dis2(a,b,c1,p[i].x,p[i].y)-p[i].r);
add(n+,i,p[i].d1);
add(i,n+,p[i].d1);
p[i].d2=max(0.0,dis2(a,b,c2,p[i].x,p[i].y)-p[i].r);
add(n+,i,p[i].d2);
add(i,n+,p[i].d2);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
double d=Dis(p[i],p[j]);
d=max(0.0,d-p[i].r-p[j].r);
// fuck(d);
add(i,j,d);
add(j,i,d);
}
}
dij();
// for(int i=0;i<n+3;i++){
// printf("%10.6f\n",dis[i]);
// }
printf("%.6f\n",dis[n+]);
}

牛客 国庆七天乐 day1 L的更多相关文章

  1. 牛客国庆集训派对Day1 L New Game!(堆优化dijkstra+建图)

    链接:https://ac.nowcoder.com/acm/contest/201/L来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言2097 ...

  2. 牛客国庆集训派对Day1 L-New Game!(最短路)

    链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  3. 2019牛客国庆集训派对day1(A, B E F K)

    链接:https://ac.nowcoder.com/acm/contest/1099#question A:可知符合条件的图中间肯定存在一个由1构成的矩形,找到由1构成矩形的边界,判断出现的1的数量 ...

  4. 牛客国庆集训派对Day1 Solution

    A    Tobaku Mokushiroku Kaiji 水. #include <bits/stdc++.h> using namespace std; ], b[]; void Ru ...

  5. 牛客国庆集训派对Day1:J:Princess Principal(栈模拟求括号匹配)

    题目描述 阿尔比恩王国(the Albion Kingdom)潜伏着一群代号“白鸽队(Team White Pigeon)”的间谍.在没有任务的时候,她们会进行各种各样的训练,比如快速判断一个文档有没 ...

  6. 2019牛客国庆集训派对day1 K题 双向链表练习题 splay区间翻转

    题目链接: 解法: 先建n颗平衡树,合并的时候将a中最右的结点翻转到根节点,b中最左的结点翻转到根节点,对合并后的根节点进行标记. #include <bits/stdc++.h> usi ...

  7. 牛客国庆集训派对Day1.B.Attack on Titan(思路 最短路Dijkstra)

    题目链接 \(Description\) 给定\(n,m,C\)及大小为\((n+1)(m+1)\)的矩阵\(c[i][j]\).平面上有\((n+1)(m+1)\)个点,从\((0,0)\)编号到\ ...

  8. 牛客国庆集训派对Day1 B. Attack on Titan

    B. Attack on Titan 链接 #include<cstdio> #include<algorithm> #include<cstring> #incl ...

  9. 2019牛客国庆集训派对day1

    C 存每个值存在的位置,枚举末尾的值,再枚举前面的值,哈希二分出最长相同的,即剩下的为不同的 D \(f_{i,j,k}\)为前i位,最后一个3因子在j,次因子在k G bitset处理有多少位置符合 ...

随机推荐

  1. ruby 第三方模块unirest使用

    Creating Requests require 'unirest' response = Unirest.post 'http://httpbin.org/post', headers:{ Acc ...

  2. ruby 数据类型Symbol

    一.符号创建 符号是Symbol类的实例,使用冒号加一个标识符即可创建符号 :a :"This is a symno" 二.符号字符串相互转换 p :symbol.to_s #=& ...

  3. json模块、os模块

    一.eval模拟序列化操作 1.序列化 内存中的数据-------->转成一种中间格式(字符串)---------->存到文件中 dic={'name':'egon','age':18} ...

  4. 004---Linux系统设置

    Linux版本相关命令 查看系统版本:cat /etc/redhat-release 查看系统内核版本以及位数:uname -r [root@hostname1 ~]# cat /etc/redhat ...

  5. 【转】已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭

    在运用Linq to sql 或者 linq to entity等相关linq技术进行数据库访问操作时,如果发生上述异常是因为是因为.NET內部是使用DataReader作数据存取,DataReade ...

  6. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  7. 洛谷P2307 迷宫

    怎么又是一道叫迷宫的题呀QWQ 题目链接 这道题主要是对并查集的考察,需要注意的坑点在于有可能存在的不止一个联通块. 我们只需要对输入的两个数据进行判断,如果在一个集合中证明有多条路则输出0,如果不在 ...

  8. 学习SQLite基本语句

    SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同,SQLite 的安装和运行非 ...

  9. ArcPy:GeoJSON转ArcGIS Geometry

    import arcpy geojson = {"type":"Polygon","coordinates":[[[120.81878662 ...

  10. 最后一片蓝海的终极狂欢-写在Win10发布前夕

    作为一名Windows8.x+系统平台从业者,从工作伊始,耳边不断充斥着Windows将走向没落的言论,Win10今日晚些时候即将发布,笔者借此机会,说说自己的看法. 早在2012年的时候,IDC曾预 ...