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 贪心的更多相关文章

  1. codeforces 704B - Ant Man [想法题]

    题目链接:http://codeforces.com/problemset/problem/704/B ------------------------------------------------ ...

  2. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  3. 【贪心】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的 ...

  4. 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| ...

  5. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  6. CodeForces 176A Trading Business 贪心

    Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...

  7. Codeforces Gym 100803C Shopping 贪心

    Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...

  8. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

  9. Codeforces 1154D - Walking Robot - [贪心]

    题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...

随机推荐

  1. asp.net中当服务器出错时显示指定的错误页面

    http://blog.csdn.net/helloxiaoyu/article/details/2943537 此篇文章描述了当异常再ASP.NET中发生时怎样使用C#.NET代码去拦截和相应异常. ...

  2. HTML5入门7---"session的会话缓存"和"localStorage的cookie"缓存数据

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 简单的自绘CListBox,重载虚MeasureItem和DrawItem这两个虚函数

    [cpp] view plain copy //例如CNewListBox继承自CListBox,重载虚MeasureItem和DrawItem这两个虚函数,代码如下: void CNewListBo ...

  4. JRE下的rt.jar、tools.jar

    JRE下的rt.jar: 这个文件是极为重要的一个文件,rt是runtime的缩写,即运行时的意思.是java程序在运行时必不可少的文件. 里面包含了java程序员常用的包,如java.lang,ja ...

  5. Event-based Asynchronous Pattern Overview基于事件的异步模式概览

    https://msdn.microsoft.com/zh-cn/library/wewwczdw(v=vs.110).aspx Applications that perform many task ...

  6. TUXEDO错误解决方案

    错误1: root@tfjus:/opt/tuxedo/simpapp# buildclient -f simpcl.c -o simpcl simpcl.c: In function 'main': ...

  7. JasperReports+iReport打印为excel表头重复问题解决

    iReport版本:3.7.4 解决方法很简单,无奈我就是纠结了一个多小时... 首先,点击文件根目录 移到  属性 框里面,找到Ignore pagination项,勾上,忽略分页,一切就OK了.

  8. JS创建类以及类的方法(StringBuffeer类)

    创建StringBuffer类以及toString,append()方法 //创建一个StringBuffer类 ,此类有两个方法:一个是append方法一个是toString方法 function ...

  9. java中的clone

    .clone 要实现cloneable接口: .深度clone和浅度clone .对象.clone() 1. Clone&Copy      假设现在有一个Employee对象,Employe ...

  10. UVa 1328 (KMP求字符串周期) Period

    当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是 ...