USACO concom DFS

写哭了,本来感觉是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

时间: 2024-11-07 00:46:42

USACO concom DFS的相关文章

USACO zerosum DFS 1A

USER: Kevin Samuel [kevin_s1] TASK: zerosum LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.003 secs, 3508 KB] Test 2: TEST OK [0.003 secs, 3508 KB] Test 3: TEST OK [0.005 secs, 3508 KB] Test 4: TEST OK [0.000 secs, 3508 KB] Test 5

hdu 4277 USACO ORZ DFS

USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3581    Accepted Submission(s): 1196 Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastur

HDU4277 USACO ORZ(dfs+set)

Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.I. M. Hei, the lead cow pasture architect, is in charge of creating a

USACO Hamming Codes DFS 构造

我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring>

USACO Superprime Rib(dfs)

题目请点我 题解: 这道题其实很简单,dfs+从前往后判断就好了,每递归一层就进行一次判断. USACO第一章节,最后一发.也是放假回家前的最后一道题!! 代码实现: /* ID: eashion LANG: C++ TASK: sprime */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #define

hdu 4277 USACO ORZ(dfs+剪枝)

Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.I. M. Hei, the lead cow pasture architect, is in charge of creating a

USACO Healthy Holsteins DFS

使用排列组合,遍历所有可能的情况C(1)+C(2)+C(3)……C(n)= 2^G种组合 数据规模不大,暴力过去最多也就是2^15 = 23768种情况 所以就暴力咯,不过还是Debug了一会 Source Code: /* ID: wushuai2 PROG: holstein LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #i

[【USACO】The Castle(dfs+枚举)

思路很好像,卡了我很久的就是当最大房间一样的时候判断输出哪个的条件, = = 简直无情 /* ID: 18906421 LANG: C++ PROG: castle */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 55; int mat[maxn][maxn][2] = {0}; // 0 1 下 右 int n,m,vis[m

USACO Prime Palindromes(dfs)

题目请点我 题解: 这道题看起来是一道很简单的题目,但是数据量很大.如果暴力判断会超时,预处理筛法标记素数则会超内存.最后看了网上的解法才知道要逆推,生成回文数,这还是要第一次遇到.并且很重要的一点是偶数位的回文数都能被11整除,所以只需要生成奇数位的,偶数位回文素数符合情况的值可能有11一个.挺考验思维的,重要的是do mathmatics first. 代码实现: /* ID: eashion LANG: C++ TASK: pprime */ #include <iostream> #i