题目链接:

  http://codeforces.com/problemset/problem/704/B

题目大意:

  给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式:(题目描述的那个i<j有错!害我WA了好几次)

  • |xi - xj| + ci + bj seconds if x[j] < x[i].
  • |xi - xj| + di + aj seconds otherwise (x[j] > x[i]).

  求从起点到终点,经过N个点恰好一次的最短路。

题目思路:

  【贪心】

  这题首先一看过去觉得像DP题,但是数据好大,一时不知道怎么做。

  我是先把每个点到其他点的距离算出来,然后一个一个往S到T中间插入点,用一个链表记录每个点到达的下一个点。

  如果把当前结点I插在J和K中间比插在其他区间更优就更新答案。

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 5004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
double anss;
int pre[N],next[N];
LL aans,sum;
LL x[N],a[N],b[N],c[N],d[N];
LL dis[N][N];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int s,t,mark;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
scanf("%d%d",&s,&t);
for(i=;i<=n;i++)scanf("%I64d",&x[i]);
for(i=;i<=n;i++)scanf("%I64d",&a[i]);
for(i=;i<=n;i++)scanf("%I64d",&b[i]);
for(i=;i<=n;i++)scanf("%I64d",&c[i]);
for(i=;i<=n;i++)scanf("%I64d",&d[i]);
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if(i==j)continue;
if(x[j]<x[i])dis[i][j]=abs(x[i]-x[j])+c[i]+b[j];
else dis[i][j]=abs(x[i]-x[j])+d[i]+a[j];
}
}
aans=dis[s][t];
next[s]=t;
for(i=;i<=n;i++)
{
if(i==s ||i==t)continue;
for(j=s,sum=1e18;j!=t;j=next[j])
{
k=next[j];
if(dis[j][i]+dis[i][k]-dis[j][k]<sum)sum=dis[j][i]+dis[i][k]-dis[j][k],mark=j;
}
aans+=sum;
j=mark;k=next[j];
next[j]=i;
next[i]=k;
}
printf("%I64d\n",aans);
}
return ;
}
/*
// //
*/

【贪心】Codeforces 704B & 705D Ant Man的更多相关文章

  1. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

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

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

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

  4. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  5. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  6. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

  7. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  8. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

    题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...

  9. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

随机推荐

  1. HTML5 文件域+FileReader 读取文件并上传到服务器(三)

    一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...

  2. Android图表引擎AChartEngine之折线图使用

    最近在帮老师做一个课题,其中app端需要显示折线图以便直观地看数据波动,上网查了些资料后发现了这款图标引擎,另外感谢李坤老师的博客,帮助很大. 废话不多说,下面写代码. 一.AChartEngine是 ...

  3. angularjs-googleMap googleMap api地址解析与反解析

    1.js:根据地址得到经纬度var myplace=$scope.place;//获取输入的地址var geocoder = new google.maps.Geocoder();//创建geocod ...

  4. IOS学习--UIButton常用方法(20150122)

    // 1.创建一个自定义的按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 2.添加按钮 [self.view a ...

  5. Business Intelligence (BI)

    BI, 全称Business Inteligence. 帮助企业更有效地利用数据,提供经营决策支持.让决策管理者随时随地获取关键信息,基于数字决策,最终提高决策水平. 包括范围(层次由低到高):数据报 ...

  6. 颜色rgb

    1.几种基本颜色的rgb 黑色:R.G.B(0.0.0) 白色:R.G.B(255.255.255) 红色:R.G.B(255.0.0) 绿色:R.G.B(0.255.0) 蓝色:R.G.B(0.0. ...

  7. Spring 创建bean的模式

    在默认情况下,spring创建bean是单例模式 scope="singleton ",还有一种方式为多例模式[prototype]     scope          sing ...

  8. Composite 模式的实现

    实现要点: 1.组合模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器. 2 ...

  9. Problem 1183 - 排列

    #include<iostream> #include<vector> #include<algorithm> using namespace std; int c ...

  10. sed 简明教程

    做个标记 http://coolshell.cn/articles/9104.html sed全名叫stream editor,流编辑器,用程序的方式来编辑文本,相当的hacker啊.sed基本上就是 ...