标签:
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:
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.
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. |
3 1 2 80 2 3 80 3 1 20
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.
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
标签:
原文地址:http://www.cnblogs.com/hhhhhhhhhh/p/5154787.html