poj 1167
来源:互联网 发布:北京爱知科技有限公司 编辑:程序博客网 时间:2024/05/17 21:51
给出你一小时完整的公交车时刻表,要求你用规定的公交车数和最少的公交路线将所有的时刻覆盖。
先将公交车所有可能的路线初始化处理下,由于是完整的时刻表,则每条公交路线的发车间隔一定>=初始时刻,且每条公交路线最少两辆车,则可得出每条路线的初始时刻的范围为0-29. 然后对路线深搜,加上几个剪枝便可以过了。 注释部分为记录的公交路线的信息,本题没有要求输出这个。
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int arrive[70],cnt,n,ans,tret[20],ret[20];
struct Route
{
int first,inter,often;
}route[900];
int cmp(Route r1,Route r2)
{
return r1.often>r2.often;
}
void init()
{
for(int f=0;f<=29;f++)
for(int in=f+1;in<=59-f;in++)
{
int c=0,k;
for(k=f;k<=59;k+=in)
{
if(!arrive[k]) break;
c++;
}
if(k>59 && c>=2)
{
route[cnt].first=f;
route[cnt].inter=in;
route[cnt].often=c;
cnt++;
}
}
sort(route,route+cnt,cmp);
}
void dfs(int bus,int now,int s)
{
if(!bus)
{
if(now<ans)
{
ans=now;
// for(int i=0;i<now;i++)
// ret[i]=tret[i];
}
return;
}
while(s<cnt && route[s].often>bus) s++;
if(s>=cnt || now>17) return;
if(now+1+(bus-1)/route[s].often>=ans) return;
for(int i=s;i<cnt;i++)
{
int f;
for(f=route[i].first;f<=59;f+=route[i].inter)
if(!arrive[f]) break;
if(f<=59) continue;
for(int f=route[i].first;f<=59;f+=route[i].inter)
arrive[f]--;
// tret[now]=i;
dfs(bus-route[i].often,now+1,i);
for(int f=route[i].first;f<=59;f+=route[i].inter)
arrive[f]++;
}
}
int main()
{
// freopen("test.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int t;
scanf("%d",&t);
arrive[t]++;
}
init();ans=18;
dfs(n,0,0);
printf("%d\n",ans);
// for(int i=0;i<ans;i++)
// cout<<route[ret[i]].first<<ends<<route[ret[i]].inter<<endl;
return 0;
}
- poj 1167
- POJ 1167 解题报告
- poj 1167 The Buses
- POJ 1167 The Buses
- poj 1167 the buses
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- Struts2重定向(redirect)
- Android与C#Socket之间的收发问题
- 从HTML文件中抽取正文的简单方案
- hdu 4362 Dragon Ball 单调队列优化 dp
- Web Service概述
- poj 1167
- MySQL字符集详解
- having
- usb触摸屏驱动移植
- set names utf8 详解
- struts2 <s:form>传递参数问题
- 【树状数组】 poj2299 Ultra-QuickSort
- HDOJ2828-DLX, 重复覆盖小变形
- C/C++ IDE for linux