usaco题目分享——Controlling Companies
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了usaco题目分享——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
俗话说的好,暴力出奇迹。
直接用队列或者栈来做,要注意的就是,编号是乱的,并不是从1到N的
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; int n; int f[105][105],g[105][105],u[105][105]; int name[105],vis[105],o,t; struct asdasd { int a,b; }ans[10005]; bool cmp(asdasd a,asdasd b) { if(a.a==b.a) { return a.b<b.b; } return a.a<b.a; } void work(int z) { int i,j; queue<int> s; while(!s.empty()) s.pop(); for(i=1;i<=o;++i) if(i!=z) s.push(i); while(!s.empty()) { i=s.front(); s.pop(); if(i==z) continue; if((f[name[z]][name[i]]>50||g[name[z]][name[i]]>50)&&u[name[z]][name[i]]==0) { u[name[z]][name[i]]=1; for(j=1;j<=o;++j) { if(j==z) continue; g[name[z]][name[j]]+=f[name[i]][name[j]]; if(g[name[z]][name[j]]>50&&u[name[z]][name[j]]==0) s.push(j); } } } for(i=1;i<=o;++i) if(u[name[z]][name[i]]==1) { t++; ans[t].a=name[z]; ans[t].b=name[i]; } } int main() { freopen("concom.in","r",stdin); freopen("concom.out","w",stdout); int i,j; scanf("%d",&n); int a,b,c; for(i=1;i<=n;++i) { scanf("%d%d%d",&a,&b,&c); if(vis[a]==0) { vis[a]=1; o++; name[o]=a; } if(vis[b]==0) { vis[b]=1; o++; name[o]=b; } f[a][b]=c; g[a][b]+=c; } for(i=1;i<=o;++i) work(i); sort(ans+1,ans+1+t,cmp); for(i=1;i<=t;++i) { printf("%d %d\n",ans[i].a,ans[i].b); } return 0; }
以上是关于usaco题目分享——Controlling Companies的主要内容,如果未能解决你的问题,请参考以下文章
usaco-Section 2.3-Controlling Companies
洛谷P1475 控制公司 Controlling Companies