Experiment 2: Greedy Algorithm

Experiment 2: Greedy Algorithm

  • I started to experiment with the greedy algorithm tonight. I had contacted it during the freshman summer vacation. The basic idea is not to consider the overall optimum, but to solve the problem from the perspective of the local optimum.
  • Let's start to do the questions now
  • The problem is to give the total capacity of the backpack, and then give the weight and value of the items separately. The items can be divided into any size, and the total value of the items in the backpack is required to be the largest. Find the total value.
  • **Solution ideas:
  • Input: Enter the backpack capacity m and the number of items n in the first line, and enter the capacity and total value of n backpacks in the next n lines.
  • Solution: Step1: Divide the numbers in the r[], j[] array to get the unit price of these n backpacks and put them in a[], and then according to
  • Why is the running result of your own code and the teacher's code different? ? ?
  • **! ! ! **The experiment requirement is a decomposable knapsack problem, but the experiment code gives the code for the 0-1 knapsack problem, and the code written by myself is also correct. It seems that the 0-1 backpack problem is more difficult.
  • Now here is the idea of ​​the decomposable 0-1 knapsack problem. The possible sorting is because the connected capacity and total price of this object must be sorted according to the unit price from high to low, so bubble sorting makes the time complexity high. .
  • step1: Sort according to the unit price from high to low, the connected capacity and total price of this object
  • step2: In the while loop, loop with the current backpack capacity current>=0 as the restriction condition. When in the loop, the total benefit is directly added to the value of the object. When the cycle is not satisfied, the total benefit is added to current*j[i]. When all the objects are put into the backpack and still not full, break.
  • code show as below:
在这里插入代码片
#include <stdio.h>//可分解的背包问题 
int main(){
    
    
	double r[1000],j[1000],a[1000];
	int n;
	double m,t,Q=0;
	scanf("%d%lf",&n,&m);
	for(int i=0;i<n;i++)
	scanf("%lf%lf",r+i,j+i);
	for(int i=0;i<n;i++)
	a[i]=j[i]/r[i];
	for(int i=0;i<n-1;i++){
    
    
		for(int k=0;k<n-i-1;k++){
    
    
			if(a[k]<a[k+1]){
    
    
				t=a[k];a[k]=a[k+1];a[k+1]=t;
				t=r[k];r[k]=r[k+1];r[k+1]=t;
				t=j[k];j[k]=j[k+1];j[k+1]=t;
			}
			//printf("%lf    %lf    %lf   \n",a[k],r[k],j[k]); 
		}
	}	
	double current=m;
//	for(int i=0;i<n;i++)
//	printf("%.2lf    %.2lf    %.2lf   \n",a[i],r[i],j[i]);

	int i=0,b=0;
	while(current-r[i]>=0){
    
    
		current=current-r[i];
		Q+=j[i];//printf("%.2lf\t",Q);
		if(i==n-1){
    
    
			b=1;
			break;
		}
		i++;
	}
	if(b==0)
	Q+=current*a[i];
	printf("%.2lf\n",Q);
}
/*测试样例 
7 150
35 10
30 40
60 30
50 50
40 35
10 40
25 30
结果为190.63
*/

  • ok! ! ! Do experiment three first

Guess you like

Origin blog.csdn.net/CSDN_Ysu/article/details/109033544