poj 1040 Transportation

#include<iostream>            //dfs
#include<algorithm>
using namespace std;
struct node
{
int s,t,num;
bool operator<(const node& o)
{
if(s==o.s)
return t<o.t;
return s<o.s;
}
}pas[
30];
int cap,b,order,arr[10],res;
void dfs(int i,int sum)
{
if(i==order)
{
res
=max(res,sum);
return;
}
int temp=sum; //剪枝
for(int j=i;j<order;++j)
temp
+=(pas[j].t-pas[j].s)*pas[j].num;
if(temp<=res)
return;

for(int j=i;j<order;++j)
{
if(arr[pas[j].s]+pas[j].num>cap)
{
if(j==order-1) //这一步不可缺少
res=max(res,sum);
continue;
}
for(int k=pas[j].s;k<pas[j].t;++k) //注意不是k<=pas[j].t
arr[k]+=pas[j].num;
dfs(j
+1,sum+(pas[j].t-pas[j].s)*pas[j].num);
for(int k=pas[j].s;k<pas[j].t;++k)
arr[k]
-=pas[j].num;
}
}
int main()
{
//freopen("F:\\c++.txt", "r", stdin ) ;
while(cin>>cap>>b>>order&&cap)
{
for(int i=0;i<order;++i)
cin
>>pas[i].s>>pas[i].t>>pas[i].num;
sort(pas,pas
+order);
memset(arr,
0,sizeof(arr));
res
=0;
dfs(
0,0);
cout
<<res<<endl;
}
return 0;
}

  

posted on 2011-07-22 16:56  sysu_mjc  阅读(194)  评论(0编辑  收藏  举报

导航