UVA 10306 e-Coins(二维完全背包)

At the Department for Bills and Coins, an extension of today‘s monetary system has newly been proposed, in order to make it fit the new economy better. A number of new so called e-coins will be produced, which, in addition to having a value in the normal
sense of today, also have an InfoTechnological value. The goal of this reform is, of course, to make justice to the economy of numerous dotcom companies which, despite the fact that they are low on money surely have a lot of IT inside. All
money of the old kind will keep its conventional value and get zero InfoTechnological value.

To successfully make value comparisons in the new system, something called the e-modulus is introduced. This is calculated asSQRT(X*X+Y*Y), where X and Y hold the sums of the conventional and InfoTechnological
values respectively. For instance, money with a conventional value of $3 altogether and an InfoTechnological value of $4 will get an e-modulus of $5. Bear in mind that you have to calculate the sums of the
conventional and InfoTechnological values separately before you calculate the e-modulus of the money.

To simplify the move to e-currency, you are assigned to write a program that, given the e-modulus that shall be reached and a list of the different types of e-coins that are available, calculates the smallest amount of e-coins that are needed to exactly
match the e-modulus. There is no limit on how many e-coins of each type that may be used to match the given e-modulus.

Input

A line with the number of problems n (0<n<=100), followed by n times:

  • A line with the integers m (0<m<=40) and S (0<S<=300), where m indicates the number of different e-coin types that exist in the problem, and S states the value
    of the e-modulus that shall be matched exactly.
  • m lines, each consisting of one pair of non-negative integers describing the value of an e-coin. The first number in the pair states the conventional value, and the second number holds the InfoTechnological value
    of the coin.

When more than one number is present on a line, they will be separated by a space. Between each problem, there will be one blank line.

Output

The output consists of n lines. Each line contains either a single integer holding the number of coins necessary to reach the specified e-modulus S or, if S cannot be reached, the string "not possible".

Sample Input:

3

2 5

0 2

2 0

3 20

0 2

2 0

2 1

3 5

3 0

0 4

5 5

Sample Output:

not possible

10

2

二维背包:题意:两个属性x,y,求最少的数使(x1+x2+.xk)^2+(y1+y2+..yk)^2=s*s;

考虑背包:dp[i][j]表示属性为i,j的时候数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
const int maxn=330;
int dp[maxn][maxn];
int w1[55],w2[55];
int t,n,s;
int main()
{
     std::ios::sync_with_stdio(false);
     cin>>t;
     while(t--)
     {
         cin>>n>>s;
         CLEAR(dp,INF);
         dp[0][0]=0;
         REP(i,n)
           cin>>w1[i]>>w2[i];
         REP(i,n)
         {
             REPF(j,w1[i],s)
             {
                 REPF(k,w2[i],s)
                 {
                     if(dp[j-w1[i]][k-w2[i]]!=INF)
                        dp[j][k]=min(dp[j][k],dp[j-w1[i]][k-w2[i]]+1);
                 }
             }
         }
         int ans=INF;
         REPF(i,0,s)
         {
             REPF(j,0,s)
             {
                 if(dp[i][j]!=INF&&i*i+j*j==s*s)
                     ans=min(ans,dp[i][j]);
             }
         }
         if(ans!=INF) cout<<ans<<endl;
         else   cout<<"not possible"<<endl;
     }
     return 0;
}
时间: 2024-08-25 10:35:11

UVA 10306 e-Coins(二维完全背包)的相关文章

HDU 2159 FATE【二维多重背包】

大意:二维多重背包 分析:二维多重背包 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 105; 8 int dp[maxn][maxn]; 9 int a[maxn], b[maxn]; 10 11 int main() { 12 int n,

HDU2159 二维完全背包

FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12533    Accepted Submission(s): 5932 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问

hdu3496 二维01背包

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M个光盘,不能多也不能少,所以就要求把背包装满. 恰好把背包装满,那么在初始化时,除了dp[0]=0,剩下的dp[1~M],均为负无穷(其实设置成-1,到时候在判断一下也是一样的,思想相同) 这样才可以保证最终得到的dp[M]是一种恰好装满背包状态的最优解. 代码: #include<iostream

洛谷 P1509 找啊找啊找GF(复习二维费用背包)

传送门 题目背景 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见." "诶,别再见啊..." 七夕...七夕...七夕这个日子,对于sqybi这种单身的菜鸟来说是多么的痛苦...虽然他听着这首叫做"找啊找啊找GF"的歌,他还是很痛苦.为了避免这种痛苦,sqybi决定要给自己找点事情干.他去找到了七夕模拟赛的负责人zmc MM,让她给自己一个出题的任务.经过几天的死缠烂打,zmc MM终于同意了. 但是,拿到这个任务的sqy

HDU 2159 FATE (二维多重背包)

FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9352    Accepted Submission(s): 4413 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问

codevs1959拔河比赛(二维费用背包)

1959 拔河比赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一个学校举行拔河比赛,所有的人被分成了两组,每个人必须(且只能够)在其中的一组,要求两个组的人数相差不能超过1,且两个组内的所有人体重加起来尽可能地接近. 输入描述 Input Description 数据的第1行是一个n,表示参加拔河比赛的总人数,n<=100,接下来的n行表示第1到第n个人的体重,每个人的体重都是整数(1<=weight<=450).

HDU 2159 FATE(二维完全背包)

中文题目就不用解释了   就是裸的二维完全背包 d[i][j]表示消耗i忍耐杀j个怪最多可获得的经验  然后就用完全背包来做了  二维背包背包不过是多了一重循环 <span style="font-family:Arial Black;">#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 105; int ma

Uva 11297 Census,二维线段树,板子

维护二维矩阵. q  x1, y1, x2, y2 :   查询x1<=x<=x2, y1<=y<=y2的格子(x,y)的最大值和最小值. c    x, y, v:    修改(x,y) = v. 矩阵大小(n,m<=500). 查询次数q<=40000. #include<algorithm> using namespace std; const int INF = 1<<30; const int maxn = 2000 + 10; int

HDU 2159 FATE (二维完全背包)

Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度.当忍耐度降到0或者0以下时,xhd就不会玩这游戏.xhd还说了他最多只杀s只怪.请问他能升掉这最后一级吗? Input 输入数据有多组,对于每组数据第一行输入n,m,k,s(0

POJ2184Cow Exhibition(二维费用背包)

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9067   Accepted: 3441 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to t