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 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...
随机推荐
- Generic repository pattern and Unit of work with Entity framework
原文 Generic repository pattern and Unit of work with Entity framework Repository pattern is an abstra ...
- 80. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 基于nginx的tomcat负载均衡和集群
要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务器即可共享SESSION了. 你可以自己写tomcat的扩展来保存SESSI ...
- 腾讯大讲堂ppt全集
腾讯大讲堂ppt全集 腾讯大讲堂ppt全集资料下载 腾讯大讲堂ppt1-62资料下载 最新最全的腾讯大讲堂ppt全集 腾讯大讲堂ppt全集资料下载 腾讯大讲堂ppt1-62资料下载地址 http:// ...
- Java API —— File类
1.File类的概述 文件和目录路径名的抽象表示形式,创建File对象后,仅仅是一个路径的表示,不代码具体的事物一定是存在的. 2.构造方法 · public File ...
- HDU 4483 Lattice triangle(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4483 题意:给出一个(n+1)*(n+1)的格子.在这个格子中存在多少个三角形? 思路:反着想,所有情 ...
- Android开发之创建桌面快捷方式
Android创建桌面快捷方式就是在桌面这个应用上创建一个快捷方式,桌面应用:launcher2 通过查看launcher2的清单文件: <!-- Intent received used to ...
- Android 按键消息处理Android 按键消息处理
在android系统中,键盘按键事件是由SystemServer服务来管理的:然后在以消息的形式分发给应用程序处理.产生键盘按键事件则是有Linux kernel的相关驱动来实现. 键盘消息有别于其他 ...
- UVa 10820 (打表、欧拉函数) Send a Table
题意: 题目背景略去,将这道题很容易转化为,给出n求,n以内的有序数对(x, y)互素的对数. 分析: 问题还可以继续转化. 根据对称性,我们可以假设x<y,当x=y时,满足条件的只有(1, 1 ...
- fiddler2抓包工具使用图文教程
fiddler2抓包工具使用图文教程 三.fiddler实用功能使用说明: 1.fiddler捕获浏览器的会话: 能支持http代理的任意程序都能被fiddler捕获到,由于fiddler的运行机制就 ...