URAL 1291 Gear-wheels(BFS)
Gear-wheels
Memory limit: 64 MB
working coordinator was the everlasting trouble of their spaceship.
Arny already had been working under this trouble while his not very
attentive and responsible mate just noticed the breakage.
Judging by schematics the broken module of coordinator consists of the
set of special gears connected with each other in order to transfer the
traction from the kinetic generator to the lot of antenna driving
engines.
the extraterrestrial origin of these gears, they are connected by usual
terrestrial method: the cogs of one gear-wheel get into the slots
between cogs of another gear-wheel. So the rotation of the first
gear-wheel is transmitted to the second gear-wheel.
the multiple Arny’s revisions, no unnecessary gears stayed in the
coordinator. It means that there is no cycles in gears connection graph.
The only problem now is to check that all the gears have right
directions and speeds of rotation.
Input
last line of input contains the number of gear-wheel, that is connected
to the kinetic-generator and the speed of its rotation V (1 ≤ V ≤ 1000). This gear-wheel rotates in counter-clockwise direction.
Output
gear-wheel in the form of irreducible fraction. Numerator and
denominator of this fraction should be separated by the sign ‘/’. If
speed is negative, it is assumed that the gear-wheel rotates in
clockwise direction (in this case the minus sign should be displayed
before numerator). Otherwise the gear-wheel rotates in counter-clockwise
direction. If speed equals zero than numerator should be equal 0 and
denominator should be equal to 1. It is guaranteed that neither
numerator nor denominator of all speeds will be greater than 106.
Sample
| input | output |
|---|---|
4 |
6/1 |
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e3+;
const int M = +;
const int mod=1e9+;
int n,m,k,tot=,s,t,v;
int head[N],vis[N],speed[N],sum[N];
struct ANS{int f,s;}ans[N];
struct EDG{int to,next;}edg[N*N];
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
int gcd(int x,int y){
int xx=x,yy=y;
x=max(xx,yy);
y=min(yy,xx);
if(x%y==)return y;
return gcd(x-y,y);
}
void bfs(){
queue<int>q;
q.push(s);vis[s]=;ans[s].f=t;ans[s].s=;
while(!q.empty()){
int u=q.front();q.pop();//printf("!!!%d %d %d\n",u,ans[u].f,ans[u].s);system("pause");
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(!vis[v]){
vis[v]=;
int g=gcd(abs(sum[u]*ans[u].f),sum[v]*ans[u].s);
ans[v].f=-sum[u]*ans[u].f/g;ans[v].s=sum[v]*ans[u].s/g;
q.push(v);
}
}
}
}
int main()
{
met(ans,);met(head,-);
int u,v;
scanf("%d",&n);
for(int u=;u<=n;u++){
scanf("%d",&sum[u]);
while(~scanf("%d",&v)&&v){
add(u,v);add(v,u);
}
}
scanf("%d%d",&s,&t);
bfs();
for(int i=;i<=n;i++){
if(ans[i].f>){
printf("%d/%d\n",ans[i].f,ans[i].s);
}else if(ans[i].f==){
printf("0/1\n");
}else {
printf("-%d/%d\n",-ans[i].f,ans[i].s);
}
}
return ;
}
URAL 1291 Gear-wheels(BFS)的更多相关文章
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- 恢复drop数据
select * from recyclebin r where r.original_name = 'MSM_EXAINVITEBIDSCHEMEHEAD' ; flashback table MS ...
- SQL with PL/SQL
DDL commands --> create user / table / view / sequence alter DML --> data manipulation languag ...
- Visual Studio 2013 Update 2 and with Update 2
Microsoft 的开发工具 Visual Studio 2013 迎来 Update2 更新.本次更新将为普通开发者带来更多全新功能.修复之前旧版 Bugs.提升性能以及稳定性.之前已经安装 VS ...
- Be a person
做人不能太实诚 尤其是干我们这行的 多久时间能做完 你自己心里要有个估算 然后把时间再往后延 别他妈给自己找罪受
- Android Studio开发环境变量配置
1,Android Studio官网可以下载 可以选择下载带有SDK版本 2,如果没有配置SDK,AVD虚拟机是没法使用的,真机调试也用不了. 有时会提醒adb is not connected. 和 ...
- openstack 中 log模块分析
1 . 所在模块,一般在openstack/common/log.py,其实最主要的还是调用了python中的logging模块: 入口函数在 def setup(product_name, vers ...
- (spring-第11回【IoC基础篇】)BeanWrapper--实例化Bean的第四大利器
重复是理解和记忆的最好方法.在讲实例化Bean的每个步骤之前,我都会先复习一下Bean实例化的整个过程: 结合图片我们回顾一下具体的过程: ResourceLoader加载配置信息, 由BeanDef ...
- BZOJ 3260 跳
YY一下发现答案基本上就是(n+1)+ΣC(n+i,i),其中i=1...m. 然后发现后面每一项可以递推,只要处理1..m的逆元就好了. 这题很容易爆long long,每一步都要取模. #incl ...
- 如何去除内联元素(inline-block元素)之间的间距(转载)
如何去除内联元素(inline-block元素)之间的间距 前几天写一个专题页 div{width:900px;}div a{ display:inline-block; width:300px; ...
- linux命令:cat
1:命令介绍: cat用来打印标准输入或连接文件.tac是其相反命令,从最后一行开始打印. 2:命令格式: cat [选项] 文件 3:命令参数: -A, --show-all 等 ...