洛谷OJ: P1049装箱问题(01背包问题)

思路:其实还是求背包最多能装多少东西,状态转移方程dp[i] = max{dp[i], dp[i-w[j] + v[j]}, 在这一题中w和v是相同的

#include <cstdio>
#include <iostream>
using namespace std;

const int maxn = 20000+10;
int N, W, dp[maxn], temp;

int main() {
	cin >> W >> N;
	for(int i = 1; i <= N; i++) {
		cin >> temp;
		for(int j = W; j >= temp; j--) {
			dp[j] = max(dp[j], dp[j-temp]+temp);
		}
	}
	cout << W - dp[W];
	return 0;
}

洛谷(Luogu)是一个非常受欢迎的在线算法题库网站,它包含了各种计算机科学题目,包括数据结构、算法等。P1862输油管道问题是关于动态规划的一种典型应用,通常涉及到计算最短路径或者最优解。 在这个特定的问题中,输油管道网络可能会包含节点(代表城市)和连接它们的管道,每个管道有容量和成本。目标可能是找到从起点到终点的成本最低的输油路线,使得总成本不超过给定的最大费用,并确保满足每条管道的最大通过量。 以下是解决这个问题的一个基本的C++代码框架,采用深度优先搜索(DFS)或广度优先搜索(BFS),结合贪心策略: ```cpp #include <vector> using namespace std; struct Edge { int to, cap, cost; }; class Solution { public: vector<pair<int, int>> dijkstra(vector<Edge>& edges, int s, int t) { // 初始化距离数组和前驱指针 vector<int> dist(n, INT_MAX); vector<int> prev(n, -1); dist[s] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.push({0, s}); while (!pq.empty()) { pair<int, int> cur = pq.top(); pq.pop(); int u = cur.second; if (dist[u] != cur.first) continue; // 已经是最小距离,跳过 for (auto& edge : edges[u]) { int v = edge.to; int new_cost = cur.first + edge.cost; if (edge.cap > 0 && new_cost < dist[v]) { dist[v] = new_cost; prev[v] = u; pq.push({new_cost, v}); } } } return prev; } private: int n; // 节点数 }; ``` 请注意,这只是一个简化的模板,实际代码需要处理细节如边界条件、数据有效性检查以及最终的输出部分。如果你想要完整的代码,可以在洛谷官网查看该题目的具体描述和样例输入输出,或者在网上找一些详细的教程和博客文章作为参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值