描述:现在突然对回溯一点也不懂了,不知道为什么……
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct train
{
int start;
int destnation;
int road;
};
train root[30];
int cmp(const void *p1,const void *p2)
{
return ((train *)p1)->start - ((train *)p2)->start;
}
int n,m,l,count,i,num[10];
void dfs(int cur,int *flag)
{
if(cur>=l)
{
for( i=flag[9]=0; i<m; i++) flag[9]+=flag[i];
if(flag[9]>count) count=flag[9];
return;
}
int p[10],c(0);
for(i=0; i<m; i++) p[i]=flag[i];
for(i=root[cur].start; i<root[cur].destnation; i++)
{
p[i]=flag[i]+root[cur].road;
if(p[i]>n)
{
c=1;
break;
}
}
dfs(cur+1,flag);
if(!c) dfs(cur+1,p);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("a.txt","r",stdin);
#endif
while(scanf("%d%d%d",&n,&m,&l)!=EOF)
{
if(!n&&!m&&!l) break;
for(i=0; i<l; i++) scanf("%d%d%d",&root[i].start,&root[i].destnation,&root[i].road);
count=0;
qsort(root,l,sizeof(train),cmp);
memset(num,0,sizeof(num));
dfs(0,num);
printf("%d\n",count);
}
return 0;
}
301 - Transportation
最新推荐文章于 2018-07-03 14:32:27 发布