hdu 2955 Robberies 01背包

本文通过解决一个关于银行抢劫的问题,介绍了如何利用01背包算法来确定最大可获得利益的同时确保被抓的概率低于一定阈值。文章详细解释了算法实现过程及核心代码。

Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25589    Accepted Submission(s): 9432

Problem Description
The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.


For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.
His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.  

Input
The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj . 
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .  

Output
For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.

Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
 
Sample Input
  
3 0.04 3 1 0.02 2 0.03 3 0.05 0.06 3 2 0.03 2 0.03 3 0.05 0.10 3 1 0.03 2 0.02 3 0.05
 
Sample Output
  
2 4 6
 
Source

哇这个01背包真的是,以后好好学英语嗯 微笑
已经从网上看了无数个版的题解,终于。
嗯路还长慢慢走。

题意:想抢银行,但是有风险。
第一行是测试组数t,
第二行是抢银行需 要低于的概率(浮点型)p和计划抢的银行数n,
接下来n行,第一个数是银行钱数,第二个是被抢概率(浮点型)。
 题解:需要转换,把银行钱数j当成背包 价值v,被抓概率p当成重量p,求最大逃跑概率,然后输出最多的钱。
状态转移方程: dp[j] = max(dp[j],dp[j-bag[i].v]*(1-bag[i].p));  ( 概率是 (1-p1)* (1-p2)*(1-p3)...
#include<stdio.h> 
#include<string.h> 
#include<algorithm>  
using namespace std;  
const int maxn = 50005;
double dp[maxn]; 
struct zeroonebag  //定义01背包
{  
    int v;  
    double p;  
}bag[maxn];  
 
int main()  
{  
    int t,n;  
    double p;  
    scanf("%d",&t); 
    while(t--)  
    {  //需要低于的概率p,n是银行数, 
        scanf("%lf%d",&p,&n); 
        int sum = 0;  
        for(int i=0; i<n; i++)  
        { //bag[i].v 是银行钱数 bag[i].p 抢劫概率 
            scanf("%d%lf",&bag[i].v,&bag[i].p);  
            sum = sum+bag[i].v;  
        }  
        memset(dp,0,sizeof(dp));  
        dp[0] = 1;  //如果不抢就不会被抓 
 	//外层循环是银行数目,内层循环是银行钱数
        for(int i=0; i<n; i++)  
        {  
            for(int j=sum; j>=bag[i].v; j--)  
            {  
                dp[j] = max(dp[j],dp[j-bag[i].v]*(1-bag[i].p));  
            }  
        }  
        //抢钱的数目和逃跑的概率成反比,抢的越少,越容易跑 
        for(int i=sum; i>=0; i--)  
        {  
            if(dp[i] > 1-p)  //最大的逃跑概率大于被抓概率 ,这样才不会被抓
            {  
                printf("%d\n",i);  
                break;  
            }  
        }  
    }  
    return 0;  
}  

再附上一个把0.1背包拿出来的,enmmm反正也都一样..o(´^`)o

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 50005;
double dp[maxn];
int v[maxn];
double w[maxn];
int sum,n;

void zerobag(int cost,double weight)
{
	for(int i=sum; i>=cost; i--)
		dp[i] = max(dp[i],dp[i-cost]*(1-weight));
}

int main()
{
	int t;
	double p;
	scanf("%d",&t);
	while(t--){
		scanf("%lf%d",&p,&n);
		sum=0;
		for(int i=0; i<n; i++)
		{
			scanf("%d%lf",&v[i],&w[i]);
			sum += v[i];
		}
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		for(int i=0; i<n; i++)
			zerobag(v[i],w[i]);
		for(int i=sum; i>=0; i--)
			if(dp[i]>=1-p)
			{
				printf("%d\n",i);
				break;
			}
	}
	return 0;
}

一花一天堂 一草一世界 一树一菩提  一土一如来 一方一净土 一笑一尘缘 一念一清净。

【SCI复现】含可再生能源与储能的区域微电网最优运行:应对不确定性的解鲁棒性与非预见性研究(Matlab代码实现)内容概要:本文围绕含可再生能源与储能的区域微电网最优运行展开研究,重点探讨应对不确定性的解鲁棒性与非预见性策略,通过Matlab代码实现SCI论文复现。研究涵盖多阶段鲁棒调度模型、机会约束规划、需求响应机制及储能系统优化配置,结合风电、光伏等可再生能源出力的不确定性建模,提出兼顾系统经济性与鲁棒性的优化运行方案。文中详细展示了模型构建、算法设计(如C&CG算法、大M法)及仿真验证全过程,适用于微电网能量管理、电力系统优化调度等领域的科研与工程实践。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事微电网、能源管理相关工作的工程技术人员。; 使用场景及目标:①复现SCI级微电网鲁棒优化研究成果,掌握应对风光负荷不确定性的建模与求解方法;②深入理解两阶段鲁棒优化、分布鲁棒优化、机会约束规划等先进优化方法在能源系统中的实际应用;③为撰写高水平学术论文或开展相关课题研究提供代码参考和技术支持。; 阅读建议:建议读者结合文档提供的Matlab代码逐模块学习,重点关注不确定性建模、鲁棒优化模型构建与求解流程,并尝试在不同场景下调试与扩展代码,以深化对微电网优化运行机制的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值