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

USACO concom DFS

时间:2014-07-06 09:52:55      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:blog   数据   os   2014   cti   for   

写哭了,本来感觉是floyd,但是发现floyd根本不能连续地传递,然后看了题解写了个搜索,这个搜索我都没有想到= =

先贴个floyd的代码,先试图用DFS处理连续控股的情况,再用几个循环处理k1+k2+k3+...Kn

在第八组数据跪了

/*
ID:kevin_s1
PROG:concom
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 MAXN 100

//dp[i][j] = sum{dp[k][j]}  dp[i][k] > 50

//gobal variable====
int N;
int owner[MAXN][MAXN];
int maxindex;
int hash[MAXN];

int visited[MAXN];
int tmp1;
//==================


//function==========

void DFS(int x){
	visited[x] = 1;
	for(int i = 1; i <= maxindex; i++){
		if(!visited[i] && owner[x][i] > 50){
			owner[tmp1][i] = 51;
			DFS(i);
		}
	}
	return;
}

void solve(){
	for(int i = 1; i <= maxindex; i++){
		memset(visited, 0, sizeof(visited));
		tmp1 = i;
		DFS(i);
	}
}

void floyd(){
	for(int i = 1; i <= maxindex; i++){
		for(int j = 1; j <= maxindex; j++){
			for(int k = 1; k <= maxindex; k++){
				if(owner[i][k] > 50 && (i != j) && (i != k) && (j != k)){
					if(owner[k][j] <= 50)
						owner[i][j] += owner[k][j] ;
				}
			}
		}
	}
}


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

int main(){
	freopen("concom.in","r",stdin);
	freopen("concom.out","w",stdout);
	cin>>N;
	int i, j, share;
	maxindex = 0;
	memset(owner, 0, sizeof(owner));
	memset(hash, 0, sizeof(hash));
	for(int k = 1; k <= N; k++){
		cin>>i>>j>>share;
		owner[i][j] = share;
		maxindex = max(maxindex, max(i, j));
		hash[i] = 1;
		hash[j] = 1;
	}
	solve();
	floyd();
	for(int i = 1; i <= maxindex; i++){
		for(int j = 1; j <= maxindex; j++){
			if(owner[i][j] > 50 && (hash[i] == 1) && (hash[j] == 1) && i != j)
				cout<<i<<" "<<j<<endl;
		}
	}
	return 0;
}

然后看着题解写个搜索,对这每个公司分别搜索。用一个数组记录公司i对其他所有公司的控股情况,有点像dijkstra,自己没想到 = =

下面是AC 代码

/*
ID:kevin_s1
PROG:concom
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 MAXN 110

//gobal variable====
int N;
int owner[MAXN][MAXN];
int visited[MAXN];
int controls[MAXN];

int MAXINDEX;
int hash[MAXN];
//==================


//function==========

void DFS(int i){
	visited[i] = 1;
	for(int j = 1; j <= MAXINDEX; j++){
		controls[j] += owner[i][j];
	}
	for(int j = 1; j <= MAXINDEX; j++){
		if(controls[j] > 50 && !visited[j]){
			DFS(j);
		}
	}
	return;
}



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

int main(){
	freopen("concom.in","r",stdin);
	freopen("concom.out","w",stdout);
	cin>>N;
	int A, B, share;
	memset(hash, 0, sizeof(hash));
	MAXINDEX = 0;
	for(int i = 1; i <= N; i++){
		cin>>A>>B>>share;
		owner[A][B] = share;
		MAXINDEX = max(MAXINDEX, max(A, B));
		hash[A] = 1;
		hash[B] = 1;
	}
	for(int i = 1; i <= MAXINDEX; i++){
		memset(controls, 0, sizeof(controls));
		memset(visited, 0, sizeof(visited));
		DFS(i);
		for(int j = 1; j <= MAXINDEX; j++){
			if(i != j && controls[j] > 50 && hash[i] && hash[j]){
				cout<<i<<" "<<j<<endl;
			}
		}
	}
	
	return 0;
}


USACO concom DFS,布布扣,bubuko.com

USACO concom DFS

标签:blog   数据   os   2014   cti   for   

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

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