【USACO2.3.5】控制公司 BFS
2015-02-17 02:33
387 查看
把每个公司到每个公司,用邻接表保存。
每次搜索A公司控制了哪些公司,就把A塞进队列,把和A有链接的公司的控制率加起来,超过50的,就加进队列。 最后把所有曾经进过队列的公司输出来即可。
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
int n;
struct edge
{
edge *next;
int v, w;
edge()
{
next = NULL;
}
edge(int V, int W, edge *P)
{
v = V;
w = W;
next = P;
}
}*a[105]={NULL};
inline void insert(int x, int y, int w)
{
a[x] = new edge(y, w, a[x]);
}
int s[105], output[105];
bool vis[105];
queue<int>q;
int main()
{
ios::sync_with_stdio(false);
cin >> n;
while (n --)
{
int a, b, c;
cin >> a >> b >> c;
insert(a, b, c);
}
for (int i = 1; i <= 100; ++ i)
{
memset(s, 0, sizeof(s));
memset(vis, 0, sizeof(vis));
q.push(i);
vis[i] = 1;
while (!q.empty())
{
int now = q.front();
q.pop();
for (edge *j = a[now]; j != NULL; j = j -> next)
{
int will = j -> v;
s[will] += j -> w;
if (s[will] > 50)
{
if (!vis[will])
{
vis[will] = 1;
q.push(will);
}
}
}
}
int tail = 0;
for (int j = 1; j <= 100; ++ j)
if (vis[j] && j != i) cout<<i<<" "<<j<<endl;
}
return 0;
}
每次搜索A公司控制了哪些公司,就把A塞进队列,把和A有链接的公司的控制率加起来,超过50的,就加进队列。 最后把所有曾经进过队列的公司输出来即可。
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
int n;
struct edge
{
edge *next;
int v, w;
edge()
{
next = NULL;
}
edge(int V, int W, edge *P)
{
v = V;
w = W;
next = P;
}
}*a[105]={NULL};
inline void insert(int x, int y, int w)
{
a[x] = new edge(y, w, a[x]);
}
int s[105], output[105];
bool vis[105];
queue<int>q;
int main()
{
ios::sync_with_stdio(false);
cin >> n;
while (n --)
{
int a, b, c;
cin >> a >> b >> c;
insert(a, b, c);
}
for (int i = 1; i <= 100; ++ i)
{
memset(s, 0, sizeof(s));
memset(vis, 0, sizeof(vis));
q.push(i);
vis[i] = 1;
while (!q.empty())
{
int now = q.front();
q.pop();
for (edge *j = a[now]; j != NULL; j = j -> next)
{
int will = j -> v;
s[will] += j -> w;
if (s[will] > 50)
{
if (!vis[will])
{
vis[will] = 1;
q.push(will);
}
}
}
}
int tail = 0;
for (int j = 1; j <= 100; ++ j)
if (vis[j] && j != i) cout<<i<<" "<<j<<endl;
}
return 0;
}
相关文章推荐
- 【USACO 2.3.5】控制公司
- usaco 2.3.5控制公司
- [USACO2.3]控制公司 Controlling Companies
- [USACO2.3]控制公司 Controlling Companies
- USACO 2.3 控制公司 (DFS)
- 竞赛于 2016-4-19 T1,公司控制(usaco 原题)
- USACO maze1 BFS
- 【USACO】时钟(BFS)
- bzoj 1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛(BFS)
- 【BZOJ】1627: [Usaco2007 Dec]穿越泥地(bfs)
- bzoj 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机【bfs】
- usaco3.33Camelot(BFS)
- 跪求java 程序 swing加socket 做的反向连接远程控制软件,公司要我做我只用反向连接做了一个屏幕截取的,可是带事件控制的做不出报错一大堆,我都卡住了一周了。无奈啊!跪求java反向连接 带事件控制的完整代码!
- 【bzoj1611】【Usaco2008 Feb】Meteor Shower流星雨 (bfs)题解&代码
- 【bzoj1619】【Usaco2008 Nov】Guarding the Farm (阅读理解+bfs)题解&代码
- BZOJ 3891 [Usaco2014 Dec]Piggy Back BFS
- [Usaco2008 Feb]Meteor Shower流星雨[BFS]
- bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 bfs
- BFS-BZOJ-1603-[Usaco2008 Oct]打谷机
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS