USACO 2.3.5 Controlling Companies 解题报告

分类:图论

作者:ACShiryu

时间:2011-7-18

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

题目意思就是打印所有的符合公司A能管理B的情况。其中AB满足下面三个条件中的至少一个,就可以说A能管理B

·           公司A = 公司B

·           公司A拥有大于50%的公司B的股票。

·           公司A能管理K(K >= 1)个公司,记为C1, ..., CK,每个公司Ci拥有xi%的公司B的股票,并且x1+ .... + xK > 50%

这题可以将i1出发到100,找到没被i管理的公司j,求出sum=∑map[k][j](其中,k是被i管理的公司)

如果sum>50,i控制j。再继续寻找下一个j。如果一直没找到可被管理的公司,这再从下一个i开始查找

数据分析:数据量不大,最坏的情况是有100个公司,程序的时间复杂度为On^3),不会超时,但还要注意自己的能被自己所管理,也就是说当使用第三条规则时还要加上a管理a的情况,刚开始时没加上,提交一直WA

参考代码:

 1 #include<iostream> 
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 int map [ 105 ] [ 105 ] = { 0 } ; //初始时i拥有j的股份的多少
9 int nmap [ 105 ] [ 105 ] = { 0 } ; //通过传递时,i拥有j股份的多少
10 bool con [ 105 ] [ 105 ] = { false } ; //当为true时表示i能管理j
11 int main()
12 {
13 //因为USACO都是通过读取文件的形式来验证程序的
14 freopen("concom.in","r",stdin);
15 freopen("concom.out","w",stdout);
16 int n ;
17 cin >> n ;
18 int i , j , k ;
19 for ( i = 0 ; i < n ; i ++ )
20 {
21 int a , b , c ;
22 cin >> a >> b >> c ; //a拥有b的c%的股份
23 map [ a ] [ b ] = c ;
24 if ( c >= 50 ) //当股份超过50%时则说明a能管理b
25 con [ a ] [ b ] = true ;
26 }
27
28 for ( i = 1 ; i <= 100 ; i ++ )
29 {
30 con [ i ] [ i ] = true ;
31 bool tag = true ;//判断是否又有新公司能被i管理,当为false时
32 //说明已无新公司可被i管理,则从下一个i开始
33 while ( tag )
34 {
35 tag = false ; //初始时标记为无公司加入
36 for ( j = 1 ; j <= 100 ; j ++ )
37 {
38 if ( ! con [ i ] [ j ])
39 { //如果i不能管理j,则看规则是否能符合第三条
40 int sum = 0 ; //定义i间接拥有j的股份
41 for ( k = 1 ; k <= 100 ; k ++ )
42 {
43 if ( con [ i ] [ k ] )
44 {//i能管理k,,则i间接拥有j股份值要加上k拥//j有的股份值
45 sum += map [ k ] [ j ] ;
46 }
47
48 }
49 if ( sum > 50 )
50 { //如果间接拥有股份值超过50%,则i能管理j
51 tag = true ; //标记为true,有新公司被管理
52 con [ i ] [ j ] = true ;
53 }
54 }
55 }
56 }
57 }
58 for ( i = 1 ; i <= 100 ; i ++ )
59 {
60 for ( j = 1 ; j <= 100 ; j ++ )
61 { //输出i能管理j的情况,要排除i=j
62 if ( con [ i ] [ j ] && i != j )
63 cout << i << ' ' << j << endl ;
64 }
65 }
66 return 0;
67 }

  

转载于:https://www.cnblogs.com/ACShiryu/archive/2011/07/18/2109336.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值