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 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...
随机推荐
- 英特尔Intel
公司名称 英特尔(集成电路公司)Intel Corporation(Integrated Electronics Corporation) 英特尔公司是全球最大的半导体芯片制造商,它成立于1968年, ...
- Photoshop技巧:图层蒙版同步隐藏图层样式
原效果: 添加图层蒙版后,遮住一半,图层样式仍在,如: 进入图层样式,勾选“图层蒙版隐藏效果” 最终效果:
- Eclipse项目和MyEclipse项目
因为Eclipse的项目结构和MyEclipse项目的结构不同,所以两者的项目之间不能直接运行的. 我们在创建Eclipse项目的时候可以进行一些设置,这样在Eclipse中创建的项目可以直接在MyE ...
- Android 类似时间轴的实现
想要实现图片中的的时间轴的效果,设定了三种颜色,但是出来的只有一个黑色,还不是设定好的,而且长度很长的话不能滚动,下面上代码: 布局文件: <LinearLayout xmlns:android ...
- [转载]深入理解JAVA的接口和抽象类
深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...
- yeoman错误提示
运行 yo angular 出现如下提示: $ yo angular grunt-cli: The grunt command line interface. (v0.1.9) Fatal error ...
- CocoaPods requires your terminal to be using UTF-8 encoding
WARNING: CocoaPods requires your terminal to be using UTF-8 encoding. See https://github.com/CocoaPo ...
- 函数mem_area_alloc
/********************************************************************//** Allocates memory from a po ...
- 对象不支持“attachEvent”属性或方法的解决办法
有些脚本在IE11下执行会报错误: 对象不支持“attachEvent”属性或方法 解决办法 解决办法:把attachEvent改为addEventListener即可
- 【二叉树、堆】15轻院校赛-J-堆
原题:http://acm.zzuli.edu.cn/problem.php?cid=1099&pid=9 [描述] [输入] [输出] Sample Input 3 1 10 3 10 5 ...