poj 1167 the buses

本文介绍了一个基于深度优先搜索加剪枝策略的算法,用于解决公交调度问题。该问题旨在找到最少数量的公交线路来满足特定的到站时间记录。通过递归地检查所有可能的线路间隔和起始时间组合,算法确保了找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Buses

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6239 Accepted: 1699

Description

A man arrives at a bus stop at 12:00. He remains there during 12:00-12:59. The bus stop is used by a number of bus routes. The man notes the times of arriving buses. The times when buses arrive are given.
• Buses on the same route arrive at regular intervals from 12:00 to 12:59 throughout the entire hour.

• Times are given in whole minutes from 0 to 59.

• Each bus route stops at least 2 times.

• The number of bus routes in the test examples will be <=17.

• Buses from different routes may arrive at the same time.

• Several bus routes can have the same time of first arrival and/or time interval. If two bus routes have the same starting time and interval, they are distinct and are both to be presented.

Find the schedule with the fewest number of bus routes that must stop at the bus stop to satisfy the input data. For each bus route, output the starting time and the interval.

Input

Your program is to read from standard input. The input contains a number n (n <= 300) telling how many arriving buses have been noted, followed by the arrival times in ascending order.

Output

Your program is to write to standard output. The output contains one integer, which is the fewest number of bus routes.

Sample Input
17
0 3 5 13 13 15 21 26 27 29 37 39 39 45 51 52 53
Sample Output
3


【分析】
dfs+剪枝


【代码】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cstdlib>
int a[65],n,m,p[20];
bool sc(int x,int mm)
{
    if (mm>m) return 0;
    if (x==n) 
    {
        for (int i=1;i<=mm;i++)
          if (p[i]!=-1) return 0;
        return 1;
    }
    int i,j,k,y,z,temp,t,arr[65];
    bool b;
    k=0;
    while (!a[k]) k++;
    for (i=1;i<=mm;i++)
      if (p[i]!=-1&&2*p[i]-k<0)
      {
        b=1;
        t=0;
        memset(arr,0,sizeof(arr));
        for (j=k;j<=59;j+=(k-p[i]))
          if (a[j]) arr[++t]=j;
          else
          {
            b=0;
            break;
          }
        if (b)
        {
            temp=p[i];
            p[i]=-1;
            for (j=1;j<=t;j++)
              a[arr[j]]--;
            if (sc(x+t,mm)) return 1;
            p[i]=temp;
            for (j=1;j<=t;j++)
              a[arr[j]]++;
        }
      }
    if (k<30)
    {
        p[++mm]=k;
        a[k]--;
        if (sc(x+1,mm)) return 1;
        p[mm]=-1;
        a[k]++;
    }
    return 0;
}
int main()
{
    int i,j,x,y,z;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    {
        scanf("%d",&x);
        a[x]++;
    }
    memset(p,0xff,sizeof(p));
    for (m=1;m<=17;m++)
      if (sc(0,0)) 
      {
        printf("%d\n",m);
        return 0;
      }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值