poj1167 The Buses

算法:IDDFS

思路

  1. 筛出所有可行的线路;
  2. 限制搜索深度 d e p dep dep 1 1 1开始,搜索不同的路线组合尝试覆盖所有的公交车

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
int bus[maxn],ans[maxn];
int n,tot,dep;
struct node
{
	int st,inter,num;
}path[maxn];
inline bool cmp(node a,node b)
{
	return a.num>b.num;
}
inline int read()
{
	int st=0,f=1;
	char c=getchar();
	while (c<'0'||c>'9')
	{
		if (c=='-')
		{
			f=-1;
		}
		c=getchar();
	}
	while (c>='0'&&c<='9')
	{
		st=(st<<1)+(st<<3)+c-'0';
		c=getchar();
	}
	return st*f;
}
inline void write(int x)
{
	if (x<0)
	{
		x=~(x-1);
		putchar('-');
	}
	if (x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
inline bool check(int s,int t)
{
	for (int i=s;i<60;i+=t)
	{
		if (!bus[i])
		{
			return false;
		}
	}
	return true;
}
inline bool dfs(int cur,int pa,int cov)
{
	if (cur==dep)
	{
		if (cov>=n)
		{
			return true;
		}
		return false;
	}
	if (cov+(dep-cur)*path[pa].num<n)
	{
		return false;
	}
	for (int i=pa;i<=tot;i++)
	{
		if (check(path[i].st,path[i].inter))
		{
			int s=path[i].st,t=path[i].inter;
			for (int j=s;j<=59;j+=t)
			{
				bus[j]--;
			}
			if (dfs(cur+1,i,cov+path[i].num)==true)
			{
				return true;
			}
			for (int j=s;j<=59;j+=t)
			{
				bus[j]++;
			}
		}
	}
	return false;
}
int main()
{
	n=read();
	for (int i=1;i<=n;i++)
	{
		int x=read();
		bus[x]++;
	}
	tot=0;
	for (int i=0;i<=29;i++)
	{
		if (bus[i]==0)
		{
			continue;
		}
		for (int j=i+1;j<=59-i;j++)
		{
			if (check(i,j))
			{
				path[++tot].st=i;
				path[tot].inter=j;
				path[tot].num=1+(59-i)/j;
			}
		}
	}
	/*
	cout<<tot<<endl;
	for (int i=1;i<=tot;i++)
	{
		cout<<path[i].st<<" "<<path[i].inter<<" "<<path[i].num<<endl;
	}
	*/
	sort(path+1,path+tot+1,cmp);
	while (dfs(0,1,0)==false)
	{
		dep++;
	}
	cout<<dep<<endl;
	return 0;
}
/*
17
0 3 5 13 13 15 21 26 27 29 37 39 39 45 51 52 53
*/
/*
43
0 1 2 3 5 6 9 10 12 12 13 14 15 17 18 21 22 24 25 26 27 28 29 30 33 34 36 37 38 39 41 42 44 45 46 48 49 50 51 53 54 57 58
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值