题目链接:

  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. JAVA 安装与配置

    JDK是整个java的核心,包括java的运行环境.java工具和java基础类库. 一.安装JDK 获得JDK,登录oracle网站http://www.oracle.com/technetwork ...

  2. 客户端session与服务端session

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  3. StringHelper类,内容截取,特别适合资讯展示列表

    public class StringHelper    {        /// <summary>        /// 截字符串        /// </summary> ...

  4. C#超时处理(转载)

    /// <summary>    /// 超时处理    ///    ///    /// </summary>    public class TimeoutChecker ...

  5. Uri、UriMatcher、ContentUris详解

    http://blog.csdn.net/feng88724/article/details/6331396 1.Uri 通用资源标志符(Universal Resource Identifier, ...

  6. Activity和Fragment生命周期变化

    情形一:启动应用加载Activity和Fragment Activity::onCreate Fragment::onAttach Fragment::onCreate Fragment::onCre ...

  7. C#获取类中所有方法

    var t = typeof(HomeController); //获取所有方法 System.Reflection.MethodInfo[] methods = t.GetMethods(); // ...

  8. javascript社交平台分享-新浪微博、QQ微博、QQ好友、QQ空间、人人网

    整理的五个社交平台的分享 <!doctype html> <html lang="en"> <head> <meta charset=&q ...

  9. >=ios8 应用内跳转到系统设置界面-openURL

    iOS8以后,苹果允许从应用内跳转到系统设置,但是调试结果表明,跳不到具体的设置项,使用前应该判断当前是否能够跳转到系统设置. 代码: NSURL *url = [NSURL URLWithStrin ...

  10. SGU 125.Shtirlits

    时间限制:0.25s 空间限制:4M 题意: 有N*N的矩阵(n<=3),对所有i,j<=n有G[i][j]<=9,定义f[i][j]为G[i][j]四周大于它的数的个数(F[i][ ...