zoukankan      html  css  js  c++  java
  • 2.3.5 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
    

    {
    ID: makeeca1
    PROG: concom
    LANG: PASCAL
    }
    program concom;
    const maxn=100;
    var n,m:longint;
        map:array[0..maxn,0..maxn]of longint;
        c:array[0..maxn]of longint;
        b:array[0..maxn]of boolean;
    function max(xx,yy:longint):longint;
    begin if xx>yy then exit(xx)else exit(yy);end;
    procedure init;
    var x,y,z,i:longint;
    begin
      readln(m);
      for i:=1 to m do
      begin
        readln(x,y,z);
        n:=max(n,max(x,y));
        map[x,y]:=z;
      end;
    end;
    procedure dfs(x:longint);
    var i:longint;
    begin
      b[x]:=true;
      for i:=1 to n do inc(c[i],map[x,i]);
      for i:=1 to n do
      if (c[i]>50) and not b[i]then dfs(i);
    end;
    procedure work;
    var i,j:longint;
    begin
      for i:=1 to n do
      begin
        fillchar(c,sizeof(c),0);
        fillchar(b,sizeof(b),0);
        dfs(i);
        for j:=1 to n do
         if (i<>j)and(c[j]>50)then writeln(i,' ',j);
      end;
    end;
    begin
      assign(input,'concom.in');reset(input);
      assign(output,'concom.out');rewrite(output);
      init;
      work;
      close(output);
    end.
  • 相关阅读:
    数据量你造吗-JAVA分页
    编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础
    概率论快速学习03:概率公理补充
    概率论快速学习02:概率公理
    项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度
    项目ITP(五) spring4.0 整合 Quartz 实现任务调度
    编写高质量代码改善java程序的151个建议——导航开篇
    概率论快速学习01:计数
    改善JAVA代码01:考虑静态工厂方法代替构造器
    Python快速学习10: 循环的对象及设计 (生活的规律)
  • 原文地址:https://www.cnblogs.com/makeecat/p/3274588.html
Copyright © 2011-2022 走看看