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

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值