跳至正文

USACO 2.3 Controlling Companies 控制公司 解题报告

Controlling Companies 

Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford owns 12% of Mazda. It is said that a company A controls company B if at least one of the following conditions is satisfied: 

Company A = Company B 
Company A owns more than 50% of Company B 
Company A controls K (K >= 1) companies denoted C1, ..., CK with each company Ci owning xi% of company B and x1 + .... + xK > 50%. 
Given a list of triples (i,j,p) which denote company i owning p% of company j, calculate all the pairs (h,s) in which company h controls company s. There are at most 100 companies. 

Write a program to read the list of triples (i,j,p) where i, j and p are positive integers all in the range (1..100) and find all the pairs (h,s) so that company h controls company s. 

PROGRAM NAME: concom 
INPUT FORMAT 
Line 1: n, the number of input triples to follow 
Line 2..n+1: Three integers per line as a triple (i,j,p) described above. 

SAMPLE INPUT (file concom.in) 
3 
1 2 80 
2 3 80 
3 1 20 

OUTPUT FORMAT 
List 0 or more companies that control other companies. Each line contains two integers that denote that the company whose number is the first integer controls the company whose number is the second integer. Order the lines in ascending order of the first integer (and ascending order of the second integer to break ties). Do not print that a company controls itself. 
SAMPLE OUTPUT (file concom.out) 
1 2 
1 3 
2 3 

题目
有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分。例如,福特公司拥有马自达公司12%的股票。据说,如果至少满足了以下三个条件之一,公司A就可以控制公司B了:

公司A = 公司B。
公司A拥有大于50%的公司B的股票。
公司A控制K(K >= 1)个公司,记为C1, …, CK,每个公司Ci拥有xi%的公司B的股票,并且x1+ …. + xK > 50%。
给你一个表,每行包括三个数(i,j,p);表明公司i享有公司j的p%的股票。计算所有的数对(h,s),表明公司h控制公司s。至多有100个公司。

写一个程序读入N组数(i,j,p),i,j和p是都在范围(1..100)的正整数,并且找出所有的数对(h,s),使得公司h控制公司s。

INPUT FORMAT
第一行: N,表明接下来三对数的数量。{即(i,j,p)的数量}

第二行到第N+1行: 每行三个整数作为一个三对数(i,j,p),如上文所述。{表示公司 i 拥有公司j p%的股份}

SAMPLE INPUT (file concom.in)
3
1 2 80
2 3 80
3 1 20
OUTPUT FORMAT
输出零个或更多个的控制其他公司的公司。每行包括两个整数A、B,表示A公司控制了B公司。将输出的数对以升序排列。

请不要输出控制自己的公司。

[编辑] SAMPLE OUTPUT (file concom.out)
1 2
1 3
2 3

==================== 华丽的分割线 ====================
这题对我来说好难,, 实在是难弄出来,, 后来看了提示之后才想到用两个矩阵分别存储公司控制的情况, 如: cont[i][j] 但如果为1 的话代表i公司控制了j公司. 还有一个是占有的股权的矩阵, 如: owns[i][j] 代表i公司直接或间接地占有j公司多少股权..
C语言:

/*
LANG: C
ID: zqy11001
PROG:concom
*/
#include <stdio.h>
#define MAX 101
#define getint(i) scanf(%d, &i)

int n;
int owns[MAX][MAX];
int cont[MAX][MAX];

void addcont(int a, int b)
{
 int i;
 if(cont[a][b]){
 return;
 }
 cont[a][b] = 1;
 for(i = 1; i < MAX; i++){
 owns[a][i] += owns[b][i];
 }
 for(i = 1; i < MAX; i++){
 if(cont[i][a]){
 addcont(i, b);
 }
 }
 for(i = 1; i < MAX; i++){
 if(owns[a][i] > 50){
 addcont(a, i);
 }
 }
}

void addown(int a, int b, int t)
{
 int i;

 for(i = 1; i < MAX; i++){
 if(cont[i][a]){
 owns[i][b] += t;
 }
 }
 for(i = 1; i < MAX; i++){
 if(owns[i][b] > 50){
 addcont(i, b);
 }
 }
}

int main(void)
{
 int a, b, t;
 int i, j, k;
 freopen(concom.in, r, stdin);
 freopen(concom.out, w, stdout);
 getint(n);
 for(i = 1; i < MAX; i++){
 cont[i][i] = 1;
 }
 for(i = 1; i <= n; i++){
 getint(a);
 getint(b);
 getint(t);
 addown(a, b, t);
 }
 for(i = 1; i < MAX; i++){
 for(j = 1; j < MAX; j++){
 if(cont[i][j] && i != j){
 printf(%d %d\\n, i, j);
 }
 }
 }
// getch();
 return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注