Uva--301(暴力DFS)

2014-07-15 10:50:22

题意&思路:直接暴力,要注意order数等于0的情况。所以程序能鲁棒尽量鲁棒。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 int n,m,norder,tmax,pasn[1000];
 8 
 9 struct order{
10     int st,ed,num;
11 };
12 order o[1000];
13 
14 void Dfs(int cur,int earn){
15     if(cur >= norder){
16         tmax = max(tmax,earn);
17         return;
18     }
19     int flag = 1;
20     for(int i = o[cur].st; i < o[cur].ed; ++i){
21         if(pasn[i] + o[cur].num > n){
22             flag = 0;
23             break;
24         }
25     }
26     if(flag){
27         for(int i = o[cur].st; i < o[cur].ed; ++i)
28             pasn[i] += o[cur].num;
29         //printf("yes: %d\n",earn + o[cur].num * (o[cur].ed - o[cur].st));
30         Dfs(cur + 1,earn + o[cur].num * (o[cur].ed - o[cur].st));
31         for(int i = o[cur].st; i < o[cur].ed; ++i)
32             pasn[i] -= o[cur].num;
33     }
34     //printf("no: %d\n",earn);
35     Dfs(cur + 1,earn);
36 }
37 
38 int main(){
39     while(scanf("%d %d %d",&n,&m,&norder) == 3){
40         if(!n && !m && !norder)
41             break;
42         memset(pasn,0,sizeof(pasn));
43         memset(o,0,sizeof(o));
44         for(int i = 0; i < norder; ++i){
45             scanf("%d %d %d",&o[i].st,&o[i].ed,&o[i].num);
46         }
47         tmax = 0;
48         Dfs(0,0);
49         printf("%d\n",tmax);
50     }
51     return 0;
52 }

 

posted @ 2014-07-15 12:11  Naturain  阅读(114)  评论(0编辑  收藏  举报