Uva 301 Transportation

以ticket order爲節點進行遍歷,那麼每個節點有兩個狀態:選與不選

#include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
 #include <string.h>
 #include <stack>
 #include <queue>
 #include <iostream>
 #include <algorithm>
 
 using namespace std;
 
 #define MAXN  10
 #define NEGINF -1e8
 
 int cap,m,t;	/* capacity and the number of the station */
 
 int maxSum;
 int cnts[MAXN];
 
 struct Node {
 	int start,end,num;
 };
 
 Node nodes[30];
 
 bool judge() {
 	int i,j;
 	for( i = 0; i <= m; i++ )
 		if( cnts[i] > cap )
 			return false;
 	return true;
 }
 
 void dfs(int cur,int sum) {
 
 	/*printf("dept=%d cur=%d sum=%d\n",dept,cur,sum);*/
 	if( cur == t ) {
 		if( sum > maxSum )
 			maxSum = sum;
 		return;
 	}
 
 	int i,j;
 	for( i = nodes[cur].start; i <= nodes[cur].end - 1; i++ )
 		cnts[i] += nodes[cur].num;
 	if( judge() )
 		dfs(cur+1,sum+(nodes[cur].end - nodes[cur].start) * nodes[cur].num);	/* choose */
 
 	for( i = nodes[cur].start; i <= nodes[cur].end - 1; i++ )
 		cnts[i] -= nodes[cur].num;
 	dfs(cur+1,sum);																/* not choose */
 }
 
 int main() {
 	#ifndef ONLINE_JUDGE
 		freopen("data.in","r",stdin);
 		//freopen("data.out","w",stdout);
 	#endif
 
 	int i,j;
 	int n,a,b,c;
 	while( scanf("%d %d %d",&cap,&m,&t) && (cap || m || t ) ) {
 		memset(cnts,0,sizeof(cnts));
 		for( i = 0; i < t; i++ ) {
 			scanf("%d %d %d",&a,&b,&c);
 			nodes[i].start = a; nodes[i].end = b; nodes[i].num = c;
 		}
 
 		if( cap == 0 || m == 0 || t == 0 ) {
 			printf("0\n");
 			continue;
 		}
 		maxSum = NEGINF;
 		dfs(0,0);
 		printf("%d\n",maxSum);
 
 	
 	}
 	
 
 	return 0;
 }
 
 
 

 
 /*
 1000 30 6
 0 4 4
 3 5 3
 4 9 32
 9 22 34
 12 21 2
 22 30 10
 10 5 4
 3 5 10
 2 4 9
 0 2 5
 2 5 8
 0 0 0
 */
 
 /*
 722
 34
 */
 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章