Controlling companies
Control Company
Some companies are part owners of other companies because they have obtained a portion of the shares issued by other companies. For example, Ford owns 12% of Mazda's shares. It is said that if at least one of the following three conditions is met, Company A can control company B:
- Company A = Company B.
- Company A owns more than 50% of Company B's shares.
- Company A controls K (k> = 1) companies as C1 ,..., CK, each company CI owns the shares of Company B of Xi %, and X1 + .... + XK> 50%. (Including the shares originally held by the company)
Here is a table. Each row contains three numbers (I, j, P), indicating that company I enjoys P % of Company J. Calculate all the number pairs (H, S), indicating that the company H controls the Company S. There are up to 100 companies.
Write a program to read N groups (I, j, P), I, J, and P are all in the range (1 .. 100), and find all the number pairs (H, S), so that the company H controls the Company S.
Input Format
The first line: N, indicating the number of the next three numbers, namely (I, j, P.
Line 2 to line n + 1: each row has three integers as a three logarithm (I, j, P), indicating that company I owns Company J P % of the shares.
Sample input (File concom. In)
31 2 802 3 803 1 20
Output Format
Outputs zero or more companies that control other companies. Each row contains two integers, A and B, indicating that Company A controls Company B. Sort the number pairs in ascending order.
Do not output your own company (do not output yourself, or do companies with mutual control need to output ).
Sample output (File concom. out)
1 21 32 3
Analysis:
At the beginning, this question was quite troublesome and I didn't want to write it. However, after careful research, we can find that it is a very simple search question.
F [I] [J] is the number of shares directly controlled by company I, and P [I] [J] is the number of shares directly controlled by company I + indirectly, H [I] [J] indicates whether company I can control company J.
First, add all directly controllable relationships, and then start to enumerate and update the indirect shareholding relationships of all companies. If not, check whether a new control relationship is added, each time you add a group of control relationships, you need to update all the relationships (When I saw this, I suddenly thought of Dijkstra. Every time I made a point, I had to relax. then I suddenly realized that learning an algorithm should not only understand the surface, but also learn the ideas contained in it ~~~)
Code:
/*ID: 138_3531PROB: concomLANG: C++*/#include<iostream>#include<fstream>#include<cstring>using namespace std;int f[101][101],p[101][101];char h[101][101];int n;ifstream fin("concom.in");ofstream fout("concom.out");int main(){ int max=0; fin>>n; memset(h,0,sizeof(h)); memset(f,0,sizeof(f)); for (int i=0;i<n;i++) { int x,y; fin>>x>>y; if (x>max) max=x; if (y>max) max=y; fin>>f[x][y]; if (f[x][y]>50) h[x][y]=1; } int ok=1; while (ok) { ok=0; memset(p,0,sizeof(p)); for (int i=1;i<=max;i++) for (int j=1;j<=max;j++) p[i][j]=f[i][j]; for (int i=1;i<=max;i++) for (int j=1;j<=max;j++) if (h[i][j]) for (int k=1;k<=max;k++) p[i][k]+=f[j][k]; for (int i=1;i<=max;i++) for (int j=1;j<=max;j++) if ((p[i][j]>50)&&(h[i][j]==0)) { h[i][j]=1; ok=1; } } for (int i=1;i<=max;i++) for (int j=1;j<=max;j++) if ((h[i][j])&&(i!=j)) fout<<i<<' '<<j<<endl; return 0;}