您的位置:首页 > 其它

poj 1040 Transportation

2011-07-22 16:56 176 查看
#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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: