URAL 1031 Railway Tickets

动态规划。

一道完全自己想然后看了眼别人给的样例测试了一下程序的题。。。。。。

题的意思就是有许多站台,给出的距离是距离第一个的,然后一个长度一个价钱,问从一个站台到另一个站台需要的最小费用

动态规划嘛。。。

写的是n^2的,,应该有n的。。。。注意给的起点终点的大小关系!!!

#include<stdio.h>记得开LONG LONG

#include<string.h>

#include <algorithm>

#include <bits/stdc++.h>

using namespace std;

long long int a[10001];

long long int dp[10001]={0};

int main()

{

long long int l1,l2,l3,c1,c2,c3;

scanf("%lld%lld%lld%lld%lld%lld",&l1,&l2,&l3,&c1,&c2,&c3);

int num;

scanf("%d",&num);

int qi,zhong;

scanf("%d %d",&qi,&zhong);

if(qi>zhong) swap(qi,zhong);

int i,j;

for(i=2;i<=num;i++) scanf("%lld",&a[i]);

for(i=qi;i<=zhong;i++)

{

long long int t=a[i]-a[i-1];

if(t<=l1) dp[i]=dp[i-1]+c1;

if(t>l1&&t<=l2) dp[i]=dp[i-1]+c2;

else dp[i]=dp[i-1]+c3;

}

dp[qi]=0;

for(i=qi;i<=zhong;i++)

{

for(j=1;j<=i-1;j++)

{

//if(j<qi) break;

int juli=a[i]-a[j];

if(juli<=l3)

{

if(juli<=l2)

{

if(juli<=l1) {dp[i]=min(dp[j]+c1,dp[i]); }

else dp[i]=min(dp[i],dp[j]+c2);

}

else dp[i]=min(dp[i],dp[j]+c3);

}

}

}

//for(i=1;i<=zhong;i++) printf("%d ",dp[i]);

printf("%lld\n",dp[zhong]);

return 0;

}

再给一个大神写的吧。。。。。。。15ms...

#include <stdio.h>

long w[10000]={0},f[100000]={0};

int main()

{

long i,s,t,n,l1,l2,l3,c1,c2,c3,s1,s2,s3;

int min(int,int);

scanf("%ld %ld %ld %ld %ld %ld*",&l1,&l2,&l3,&c1,&c2,&c3);

scanf("%ld*",&n);

scanf("%ld %ld*",&s,&t);

if (s>t) s=s+t,t=s-t,s=s-t;

for (i=2;i<=n;i++)

scanf("%d*",&w[i]);

s1=s,s2=s,s3=s;

for (i=s+1;i<=t;i++)

{

while (w[i]-w[s1]>l1) s1++;

while (w[i]-w[s2]>l2) s2++;

while (w[i]-w[s3]>l3) s3++;

if(s1<i)

f[i]=min(min(f[s1]+c1,f[s2]+c2),f[s3]+c3);

else

if (s2<i) f[i]=min(f[s2]+c2,f[s3]+c3);

else

f[i]=f[s3]+c3;

}

printf("%ld",f[t]);

return 0;

}

int min(int s1,int s2)

{

if (s1<s2) return s1;

else return s2;

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-30 09:37:22

URAL 1031 Railway Tickets的相关文章

递推DP URAL 1031 Railway Tickets

题目传送门 1 /* 2 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 3 注意:s1与s2大小不一定,坑! 4 详细解释:http://blog.csdn.net/kk303/article/details/6847948 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <s

POJ2355——Railway tickets

Railway tickets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2710   Accepted: 962 Description The railway line "Ekaterinburg-Sverdlovsk" with several stations has been built. This railway line can be represented as a line segment

DP+高精度 URAL 1036 Lucky Tickets

题目传送门 1 /* 2 题意:转换就是求n位数字,总和为s/2的方案数 3 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 4 高精度直接拿JayYe的:) 5 异或运算的规则: 6 0⊕0=0,0⊕1=1 7 1⊕0=1,1⊕1=0 8 口诀:相同取0,相异取1 9 */ 10 #include <cstdio> 11 #include <cstring> 12 #include <string>

POJ 2355 Railway tickets (线性dp)

OJ题目 :click here~ 题目分析:X为距离 , 当0<X<=L1, 票价为C1. L1<X<=L2 ,票价为C2.L2<X<=L3,票价为C3.给每段车站时间的距离,求某两个车站之间的总票价的最小值. 设dp[ i ] 为到车站 i 的最少票价 . 则转移方程为dp[ i ] = min(dp[ j ] + 从j 到 i 的票价),j 为所有可以直接到 i 的车站. 要注意第一个数字 大于 第二个数字的情况.的确,题目没有说,从a 到 b.只说了a,b之间.

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

Ural 1036 Lucky Tickets

Lucky Tickets Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 103664-bit integer IO format: %lld      Java class name: (Any) You are given a number 1 ≤ N ≤ 50. Every ticket has its 2N-digit number. We call a

URAL1031——DP——Railway Tickets

Description The railway line “Yekaterinburg-Sverdlovsk” with several stations has been built. This railway line can be represented as a line segment, railway stations being points on it. The railway line starts at the station “Yekaterinburg” and fini

URAL 1044 Lucky Tickets. Easy!

算是个动态规划,统计和 sum[位数][差值] 构建个虚拟数组写起来就顺多了 1 import java.util.Scanner; 2 3 public class P1044 4 { 5 private static int save[][] = new int[10][100]; 6 7 private static int getSum(int n, int deta) 8 { 9 return save[n][deta + 50]; 10 } 11 12 private static

URAL 1035 Lucky Tickets

题意:长度为2n的数字,前N位之和和后面的一样,,,加一起是s........问有多少种不同的数字 首先s是奇数肯定就不行了.... 然后n*2*9<s也不行了...... dp[i][j]+=dp[i-1][j-k];就是加的这位不同的情况·~~~ 这题要用高精度,,,, #include<stdio.h> #include<string.h> #include <algorithm> #include <bits/stdc++.h> using n