URAL 1031 Railway Tickets

Source: Internet
Author: User

Dynamic planning.

A complete self-thought and then looked at the eye others gave the sample test the program of the problem ...

The point is that there are many platforms, the distance is the first one, then a price for a length, asking the minimum cost from one platform to another

Dynamic planning ...

Writing is n^2, there should be n .... Pay attention to the size relationship of the beginning end point!!!

#include <stdio.h> Remember to open 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", &AMP;L1,&AMP;L2,&AMP;L3,&AMP;C1,&AMP;C2,&AMP;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&LT;=L1) dp[i]=dp[i-1]+c1;
if (T&GT;L1&AMP;&AMP;T&LT;=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&LT;QI) break;
int JULI=A[I]-A[J];
if (JULI&LT;=L3)
{
if (JULI&LT;=L2)
{
if (JULI&LT;=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;
}


Write to another great God .... 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*", &AMP;L1,&AMP;L2,&AMP;L3,&AMP;C1,&AMP;C2,&AMP;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]&GT;L1) s1++;
while (W[I]-W[S2]&GT;L2) s2++;
while (W[I]-W[S3]&GT;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&LT;S2) return S1;
else return S2;
}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

URAL 1031 Railway Tickets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.