POJ 1416-Shredding Company(DFS+更新路径)

题目链接:传送门

题意:给一个目标值goal,然后再给一个数num,将num分解,比如 给目标值50,num为12346 num可以分解为 1   2   34   6 这么4部分,要求部分和尽量接近目标值但不能大于目标值,求最优分解;

思路:深搜每次分割部分的起点,更新最优解的时候更新一下路径,以前也是被路径打印给困惑了,其实和更新最优值思想一样,可以设一个ans_path[] 数组,更新最优值的时候顺便更新一下最优路径就行了。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include<time.h>
#include <stack>
#include <map>
#include <set>
#define maxn 360
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 1000000007
#define pp pair<int,int>
#define ull unsigned long long
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
char s[100];
int ok,ans,path[20],ans_path[20],len,goal,rej,ans_tot;
int my_pow(int a,int n)
{
	int p=1;
	for(int i=1;i<=n;i++)
		p*=a;
	return p;
}
void dfs(int num,int sum,int tot)
{
	if(sum>goal)return ;
	if(num>=len)
	{
		if(sum>ans&&sum<=goal)
		{
			memcpy(ans_path,path,sizeof(int)*tot);
			ok=1;rej=1;ans=sum;ans_tot=tot;
			return ;
		}
		else if(sum==ans&&sum<=goal)
		{
			rej++;
			return ;
		}
		return ;
	}
	for(int i=1;num+i<=len;i++)
	{
		int j=num+i,tem=0,sb=0;
		while(j>num)tem+=(s[j--]-'0')*(int)my_pow(10,sb++);
		path[tot]=num+i;
		dfs(num+i,sum+tem,tot+1);
		path[tot]=-1;
	}
}
void print()
{
	if(rej>1)
	{
		puts("rejected");
		return ;
	}
	if(ok)
	{
		printf("%d",ans);
		for(int i=0;i<ans_tot;i++)
		{
			printf(" ");
			for(int j=(i!=0?ans_path[i-1]+1:1);j<=ans_path[i];j++)
			printf("%c",s[j]);
		}
		puts("");
	}
	else
	puts("error");
}
int main()
{
	while(scanf("%d%s",&goal,s+1)&&(goal!=0&&s[1]!='0'))
	{
		memset(path,-1,sizeof(path));
		s[0]=2;len=strlen(s)-1;
		ans=-INF;ok=0;rej=0;dfs(0,0,0);
		print();
	}
	return 0;
}
时间: 2024-11-13 10:59:45

POJ 1416-Shredding Company(DFS+更新路径)的相关文章

poj 1416 Shredding Company (dfs)

链接:poj 1416 题意:有一种新的碎纸机,要用新的碎纸机将纸条上的数字切成几部分, 求切完后的和最接近而不超过target的值. 比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分, 分别是1.2.34.6.所得到的和43 (= 1 + 2 + 34 + 6) 是不超过50的最大和 比如1, 23, 4, 和6 ,和为34,比43小, 而12, 34, 6不可以,因为它们的和超过50了. 碎纸还有以下三个要求: 1.如果target的值等于纸条上的值,则不能切.

poj 1416 -- Shredding Company

Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4114   Accepted: 2324 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would just shre

搜索+剪枝 POJ 1416 Shredding Company

POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accepted: 2964 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would

POJ 1416 Shredding Company【dfs入门】

题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6860   Accepted: 3710 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "

poj 1416 Shredding Company (DFS)

Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4595   Accepted: 2633 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would just shre

DFS/POJ 1416 Shredding Company

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int mmax,sum; 5 char s[10]; 6 bool v[10],way[10]; 7 bool rejected; 8 void dfs(int dep,int value,int before) 9 { 10 int now=0; 11 for (int i=before;i<dep;i++) 12 { 13 now=now*10

poj 1416 Shredding Company 模拟+枚举

题意: 给一个目标数和一个待分割数,可以对待分割数进行任意划分,比如将带分割数12305分为12,30,5,问将分好的数加起来最接近且不超过目标数的分割方案. 分析: 关键是在对带分割数的任意划分,直接for循环枚举状态,比如状态10101(二进制)表示将12305分为1,23,05. 代码: #include <iostream> #include <vector> using namespace std; int t,len; char a[12]; int vis[10000

POJ 3216 Repairing Company(最小路径覆盖)

POJ 3216 Repairing Company 题目链接 题意:有m项任务,每项任务的起始时间,持续时间,和它所在的block已知,且往返每对相邻block之间的时间也知道,问最少需要多少个工人才能完成任务,即x最少是多少 思路:先floyd求出每两个block之间的最小距离,然后就是最小路径覆盖问题,一个任务之后能赶到另一个任务就建边 代码: #include <cstdio> #include <cstring> #include <algorithm> #i

POJ1416——Shredding Company(DFS)

Shredding Company DescriptionYou have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would just shred sheets of paper into little pieces so that the contents would become unreadable, t