Problem A. Go Sightseeing Google Kickstart Round D 2017

题意:从city 1到city N,相邻两个city只能作bus,bus定点开出,到达每个city可以选择花费一定的时间去sightseeing,要求在deadline前必须到达city N。问最多可以go sightseeing多少city。

看起来就很像dp,开始的想法是把时间作为状态,dp[i[t]表示t时间之前到达city i, go sightseeing了多少city。状态转移就和背包问题一样,是否go sightseeing city i。但是问题中Time=1e9,large input就挂了。后来发现city N的deadline只是一个constraint不是optimization goal,只要以visited city为状态,dp的值对应花费时间。因为city数目只有2000,最后只要找到go sightseeing最多的city且花费时间

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

//Kcikstart 2017 Round D Problem A. Go Sightseeing
const int maxn=2010;
int T;
int N;
int Ts;
int Tf;
int S[maxn];
int F[maxn];
int D[maxn];
int dp[maxn][maxn];//dp[i][j] means minimum time used to visit j cities in first 0,...,i-1 cities
int ans;
int main()
{
    freopen("input.txt","r",stdin);

    freopen("A-large-practice.in","r",stdin);
    freopen("A.txt","w",stdout);
    cin>>T;
    for(int ca=1;ca<=T;ca++)
    {
        memset(dp,0x3f,sizeof(dp));
        ans=0;
        memset(S,0,sizeof(S));
        memset(F,0,sizeof(F));
        memset(D,0,sizeof(D));
        scanf("%d %d %d",&N,&Ts,&Tf);
        for(int i=0;i=0;j--)
        {
            if(dp[N-1][j]<=Tf)
            {
                ans=j;
                flg=true;
                break;
            }
        }
        if(flg==false)
        {
            printf("Case #%d: IMPOSSIBLE\n",ca);
        }
        else
        {
            printf("Case #%d: %d\n",ca,ans);
        }


    }
    return 0;
}

附上small input

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

//Kcikstart 2017 Round D Problem A. Go Sightseeing
const int maxn=5010;
int T;
int N;
int Ts;
int Tf;
int S[maxn];
int F[maxn];
int D[maxn];
int dp[17][maxn];//dp[i][t] means maximum number of visited cities by time t
int ans;
int main()
{
//    freopen("input.txt","r",stdin);

    freopen("A-small-practice.in","r",stdin);
    freopen("A.txt","w",stdout);
    cin>>T;
    for(int ca=1;ca<=T;ca++)
    {
        memset(dp,0x3f,sizeof(dp));
        ans=0;
        memset(S,0,sizeof(S));
        memset(F,0,sizeof(F));
        memset(D,0,sizeof(D));
        scanf("%d %d %d",&N,&Ts,&Tf);
        for(int i=0;i=0&&t-time_one_road_plus>=0)
                {
                    if(i==N-1)//no need to sightseeing the last city
                    {
                        dp[i][t]=dp[i-1][t-time_on_road];
//                        cout<=0&&t-time_one_road_plus<0)
                {
                    dp[i][t]=dp[i-1][t-time_on_road];
//                    cout<

 

你可能感兴趣的:(Problem A. Go Sightseeing Google Kickstart Round D 2017)