Usaco concom DFS

Source: Internet
Author: User
Tags cmath

I cried, and I thought it was Floyd, but I found that Floyd could not be passed continuously. Then I read the question and wrote a search. I didn't even think of this search =

First paste a Floyd code, first try to use DFS to process the Continuous Holding situation, and then use several cycles to process k1 + k2 + K3 +... kn

In the eighth group of data

/*ID:kevin_s1PROG:concomLANG: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;}

Then, let's look at the question solution and write a search for each company. Use an array to record the company I's holdings of all other companies, a bit like Dijkstra, I did not expect =

The following is the AC code

/*ID:kevin_s1PROG:concomLANG: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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.