codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心
题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr
向右跳:abs(b.x-a.x)+a.lr+b.rl,遍历完所有的点,问你最后的花费是多少
思路:每次选一个点的时候,在当前确定的每个点比较一下,选最短的距离。
为什么可以贪心?应为答案唯一,那么路径必定是唯一的,每个点所在的位置也一定是最短的。 #include <bits/stdc++.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int MOD =;
const int N =;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 1e-;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-;ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} struct node{
LL x;
LL rl,rr,ll,lr;
}p[]; LL fun(node a,node b){
if(b.x<a.x) return abs(b.x-a.x)+a.ll+b.rr;
return abs(b.x-a.x)+a.lr+b.rl;
}
LL net[];
int main(){
int n,a,b;
scanf("%d%d%d",&n,&a,&b);
for(int i=;i<=n;i++) scanf("%I64d",&p[i].x);
for(int i=;i<=n;i++) scanf("%I64d",&p[i].rl);
for(int i=;i<=n;i++) scanf("%I64d",&p[i].rr);
for(int i=;i<=n;i++) scanf("%I64d",&p[i].ll);
for(int i=;i<=n;i++) scanf("%I64d",&p[i].lr);
LL tem,sum,ans=;
int k;
net[a]=b;
ans+=fun(p[a],p[b]);
for(int i=;i<=n;i++){
if(i==a||i==b) continue;
sum=1e18;
for(int j=a;j!=b;j=net[j]){
tem=fun(p[j],p[i])+fun(p[i],p[net[j]])-fun(p[j],p[net[j]]);
if(tem<sum){
sum=tem;
k=j;
}
}
ans+=sum;
net[i]=net[k];
net[k]=i;
}
printf("%I64d\n",ans);
return ;
}
codeforces 704B - Ant Man 贪心的更多相关文章
- codeforces 704B - Ant Man [想法题]
题目链接:http://codeforces.com/problemset/problem/704/B ------------------------------------------------ ...
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
- 【贪心】Codeforces 704B & 705D Ant Man
题目链接: http://codeforces.com/problemset/problem/704/B 题目大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的 ...
- Ant Man CodeForces - 704B (图论,贪心)
大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式 |xi - xj| + ci + bj seconds if j< i |xi - xj| ...
- Codeforces 161 B. Discounts (贪心)
题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...
- CodeForces 176A Trading Business 贪心
Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...
- Codeforces 1154D - Walking Robot - [贪心]
题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...
随机推荐
- eclipse配置jdk问题
1.JDK安装成功,eclipse也安装成功,这时候实际上是可以不配置JDK,因为系统已经默认给你配置好了,
- nginx+apache+php+mysql服务器集群搭建
由于需要搭建了一个基本的服务器集群.具体的配置方案先不说了,到有时间的时候再介绍.下面介绍下整 个方案的优点. 我总共准备了四台阿里云的主机,架设分别是A,B1,B2,C,A在集群的最前面,B1和B2 ...
- Android:修改版本
修改AndroidManifest.xml下的Version即可 <uses-sdk android:minSdkVersion="14" android:targetSdk ...
- SVN 目录结构
Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是 svn://proj/|+-trunk+-branches+-ta ...
- 显示Servlet API主要版本,次要版本以及服务器系统信息
package com.mhb; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servle ...
- 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial
Deep Neural Networks are the more computationally powerful cousins to regular neural networks. Learn ...
- jQuery 表单验证插件 jQuery Validation Engine 使用
jQuery 表单验证插件 jQuery Validation Engine 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面),指定使用 jQuery Validation Engi ...
- linux下对符合条件的文件大小做汇总统计的简单命令
(1)统计当前目录下的 *txt 文件du -c -h *txt (2)统计当前目录下的 *txt 文件, 并求出总大小du *txt |awk 'BEGIN{count=0;size=0;} ...
- vssettings 的备份和导入
vssettings 的作用: vssettings的导出(也就是相当于是保存): 现在我们试试导入:找一个别人导出的包,我们导入试试.
- centos使用denyhosts的问题,会将自己的IP自动加到hosts.deny的解决办法。
先吐槽, 很多网站\博客,技术文章也玩伪原创, 害人不浅. 全TMD是COPY, COPY, COPY过来. 拷过来就算了, 你TMD还改了其中的内容... 改成错的.然后众多网站转载, 将错进行到底 ...