码迷,mamicode.com
首页 > 其他好文 > 详细

USACO holstein 超时代码

时间:2014-06-16 20:59:10      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   数据   2014   string   

/*
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 超时代码,布布扣,bubuko.com

USACO holstein 超时代码

标签:class   blog   code   数据   2014   string   

原文地址:http://blog.csdn.net/kevin_samuel/article/details/31081707

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!