写哭了,本来感觉是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; }
下面是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
原文地址:http://blog.csdn.net/kevin_samuel/article/details/36903139