uva--UVA 10201Adventures in Moving - Part IV

<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>
时间: 2024-07-30 01:26:51

uva--UVA 10201Adventures in Moving - Part IV的相关文章

uva 10201 Adventures in Moving - Part IV (DP)

uva 10201 Adventures in Moving - Part IV 题目大意:借了一辆车,车里有100单位的油.要到达N米外的目的地(每走一米消耗一个单位的油),在这一段路程中,有若干个加油站,给出的数据是每个加油站的位置和加一单位油的价格.要求到达目的地且剩下100单位油的最小消费.(到达不了则输出Impossible) 解题思路:dp[i][j]数组代表的是第i个加油站油量为j的最小费用. 状态转移方程: dp[i][j]=min(dp[i][j],dp[i?1][j+(mil

UVa 10201 Adventures in Moving - Part IV

https://vjudge.net/problem/UVA-10201 题意: 给出到达终点的距离和每个加油站的距离和油费,初始油箱里有100升油,计算到达终点时油箱内剩100升油所需的最少花费. 思路: 我们用d[i][j]来表示车子在第 i 个加油站时还剩 j 升油量的最小花费. 先说一下转移方程吧,d[i][j] = min(d[i][j], d[i - 1][j + l - k] + k*b[i]),k代表的是在 i 这个加油站所加的油量,加了之后的总油量就是 j . 需要注意的是,油

uva10201 - Adventures in Moving - Part IV(01背包)

题目:uva10201 - Adventures in Moving - Part IV(01背包) 题目大意:一辆车要走D距离,然后它有个200L油箱,并且一开始有100L,现在给你一路上你会遇到的加油站,和这个加油站每升油的价钱,要求你最后到终点的时候油需要大于等于100L,问你加油最少的费用.如果到达不了目标地点就输出Impossible. 解题思路:首先要先到达这个加油站,然后就相当这个加油站你选不选择加油,选择加油了那么又要加多少油.dp[j][i]代表到达第i个加油站还有jL油,dp

UVA UVA - 1395 Slim Span

点很少,按边权值排序,枚举枚举L和R,检查连通性.一旦连通,那么更新答案. 判断连通用可以O(1),之前我是O(n)判的,之前写的过了,后来写的T了... #include<bits/stdc++.h> using namespace std; const int maxn = 101; const int maxe = maxn*maxn>>1; int n,m; int u[maxe],v[maxe],w[maxe]; int pa[maxn]; inline bool cmp

uva 10201

H - Adventures in Moving - Part IV Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10201 Description Problem A: Adventures in Moving - Part IV To help you move from Waterloo to the big city, you are c

UVA 10201 DP

Adventures in Moving - Part IV 题意: 汽车邮箱容量200升,最初有100升油,要求到达终点油箱中的油不少于100升的最小花费,不能到达终点输出Impossible. 汽车走1单位距离消耗1升油. 输入t组数据 输入n表示要求从起点到距离为n的点 输入若干个加油站的a,b,表示加油站距离起点的距离和每升油的价格: 代码: //dp[i][j]=min(dp[i][j],dp[i-1][j+dis-k]+k*w),dp[i][j]表示到达 //i加油站时还剩j升油,d

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"