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;
}

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

内容概要:本文介绍了一个针对电力系统连锁故障传播路径的N-k多阶段双层优化及故障场景筛选模型,该模型基于混合整数线性规划(MILP)方法构建,旨在全面评估电力系统在遭受多重故障时的脆弱性与恢复能力。通过引入故障传播路径的概念,模型能够动态模拟故障在电网中的逐级扩散过程,并结合多阶段优化策略,实现对关键故障场景的有效识别与优先排序。整个框架不仅考虑了初始故障元件的选取,还涵盖了后续因潮流转移引发的级联跳闸行为,从而提升了风险评估的准确性与时效性。该研究已在Matlab平台上完成代码实现,具备良好的可复现性和工程应用价值,适用于提升现代电网的安全防御水平。; 适合人群:电力系统、能源安全及相关领域的科研人员、高校研究生以及从事电网规划与运行管理的工程技术人员。; 使用场景及目标:①用于电力系统安全评估中识别最危险的N-k故障组合;②支撑电网应急预案制定与薄弱环节改造;③作为学术研究中关于级联故障建模与优化求解的教学与验证工具;④服务于智能电网背景下抵御蓄意攻击或极端事件的风险防控决策。; 阅读建议:建议读者结合Matlab代码深入理解模型的数学 formulation 与求解流程,重点关注目标函数设计、约束条件构建及双层优化结构的实现逻辑,同时可通过调整系统参数和故障设定进行仿真对比分析,以掌握不同因素对连锁故障演化的影响规律。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值