码迷,mamicode.com
首页 > 其他好文 > 详细

uva--UVA 10201Adventures in Moving - Part IV

时间:2015-01-21 20:09:28      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:dp   uva   algorithm   

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<climits>
#include<cstring>
using namespace std;

int dp[110][210],dis,t,n;
int d[110],p[110];
char str[100];

void input()
{
     gets(str);
     n=0;
     d[0]=0;
     sscanf(str,"%d",&dis);
     while(gets(str)!=NULL)
     {
         if(str[0]=='\0')
            return;
         ++n;
         sscanf(str,"%d %d",&d[n],&p[n]);
         if(d[n]>dis) n--;
     }
}

void solve()
{
    for(int i=0;i<=n;i++)
       for(int j=0;j<=200;j++)
          dp[i][j]=INT_MAX;
    dp[0][100]=0;
    for(int i=1;i<=n;i++)
    {
        int w=d[i]-d[i-1];
        for(int j=0;j+w<=200;j++)
           if(dp[i-1][j+w]!=INT_MAX)
              dp[i][j]=dp[i-1][j+w];
        for(int j=0;j<=200;j++)
          for(int k=0;k<=j;k++)
          {
              if(j-k+w<=200&&dp[i-1][j-k+w]!=INT_MAX)
              {
                  dp[i][j]=min(dp[i][j],dp[i-1][j-k+w]+k*p[i]);
              }
          }
    }
    if(dis-d[n]>100||dp[n][100+dis-d[n]]==INT_MAX)
        printf("Impossible\n");
    else
        printf("%d\n",dp[n][100+dis-d[n]]);
}
int main()
{
    int t;
    gets(str);
    sscanf(str,"%d",&t);
    gets(str);
    while(t--)
    {
        input();
        solve();
        if(t)
           printf("\n");
    }
  return 0;
}</span>


uva--UVA 10201Adventures in Moving - Part IV

标签:dp   uva   algorithm   

原文地址:http://blog.csdn.net/acm_lkl/article/details/42972979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!