20210529-背包

1.最小乘车费用

思路

还是比较简单的,把10种里程处理为10个物品,写为无限背包即可。
错因:看错了题目,以为最大里程是100导致写成了(10 imes10)01背包!

代码

#include <bits/stdc++.h>
#define fo(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f , N = 1e2+5 , M = 1e2+5;
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
	ll ret = 0 ; char ch = ' ' , c = getchar();
	while(!(c >= '0' && c <= '9'))ch = c , c = getchar();
	while(c >= '0' && c <= '9')ret = (ret << 1) + (ret << 3) + c - '0' , c = getchar();
	return ch == '-' ? -ret : ret;
}
int dp[M];
int w[N],v[N],n = 10,m;
signed main(){
	memset(dp,127,sizeof(dp));
	int x;
	for(int i = 1 ; i <= 10 ; i ++){
		x = read();
			w[i] = i , v[i] = x;
	}
	
	m = read();
	dp[0] = 0;
	for(int i = 1 ; i <= n ; i ++)
		for(int j = w[i] ; j <= m ; j ++)
			dp[j] = min(dp[j],dp[j-w[i]]+v[i]);
	printf("%d",dp[m]);
	return 0;
}

T2 T3 T4都比较简单,T2方案数统计,T3有限背包,T4有限背包(转二进制优化)+无限背包+双价值(dp改为二维),就不具体展开写了。

原文地址:https://www.cnblogs.com/Shinomiya/p/14825886.html