USACO holstein 超时代码

/*
ID:kevin_s1
PROG:holstein
LANG:C++
*/第八组数据跪了,半天都不出结果

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath>

using namespace std;

#define MAXV 26
#define MAXG 16
#define INF 32766


//gobal variable====
int V;
int requirement[MAXV];
int G;
int feed[MAXG][MAXV];
int result;
int visited[MAXG];

int vit[MAXV];
int res[MAXG];
//==================


//function==========
bool check(){
	bool flag = true;
	for(int i = 1; i <= V; i++){
		if(vit[i] < requirement[i])
			flag = false;
	}
	return flag;
}

void DFS(int t, int a[]){
	if(t > V)
		return;
	if(t > result)
		return;
	if(check()){
		if(t < result){
			result = t;
			for(int i = 0; i < t; i++){
				res[i] = a[i];
			}
		}
		return;
	}
	for(int i = 1; i <= G; i++){
		if(visited[i] == 0){
			visited[i] = 1;
			for(int j = 1; j <= V; j++){
				vit[j] += feed[i][j];
			}
			a[t] = i;
			DFS(t + 1, a);
			for(int j = 1; j <= V; j++){
				vit[j] -= feed[i][j];
			}
			visited[i] = 0;
		}
	}
	return;
}

//==================

int main(){
	freopen("holstein.in","r",stdin);
	freopen("holstein.out","w",stdout);
	cin>>V;
	for(int i = 1; i <= V; i++){
		cin>>requirement[i];
	}
	cin>>G;
	for(int i = 1; i <= G; i++){
		for(int j = 1; j <= V; j++){
			cin>>feed[i][j];
		}
	}
	result = 32766;
	int ans[10000];
	for(int i = 1; i <= G; i++){
		memset(visited, 0, sizeof(visited));
		memset(vit, 0, sizeof(vit));
		visited[i] = 1;
		for(int j = 1; j <= V; j++){
			vit[j] += feed[i][j];
		}
		ans[0] = i;
		DFS(1, ans);
	}
	cout<<result;
	for(int i = 0; i < result; i++){
		cout<<" "<<res[i];
	}
	cout<<endl;
	return 0;
}

你可能感兴趣的:(USACO holstein 超时代码)