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) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- POJ 3237
题目大意:指定一颗树上有3个操作:询问操作,询问a点和b点之间的路径上最长的那条边的长度:取反操作,将a点和b点之间的路径权值都取相反数:变化操作,把某条边的权值变成指定的值. #include &l ...
- 戴文的Linux内核专题:10配置内核(6)
转自Linux中国 欢迎来到下一篇关于内核配置文章!还有大量的选项需要配置.这篇文章将主要讨论PCI和ACPI. 这里我们可以启用由ACPI控制的扩展坞和可移动驱动器槽的支持(Dock).记住,ACP ...
- linux内核编译
1,进入内核源码树,如果是第一次编译,建议清理以下内核功能选择文件: make mrproper 2,删除前一次编译的残留文件: make clean 3,配置内核功能 make menuconfig ...
- 对比学习UIKit和AppKit--入门级
UIKit是用来开发iOS的应用的,AppKit是用来开发Mac应用的,在使用过程中他们很相似,可是又有很多不同之处,通过对比分析它们的几个核心对象,可以避免混淆. UIKit和AppKit都有一个A ...
- 关于$.getJson
这是一个Ajax函数的缩写,这相当于: 1 2 3 4 5 6 $.ajax({ dataType: "json", url: url, data: data, success: ...
- xcode插件种类
古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战斗力的绝佳途径!以下是搜集的一些有力的XCode插件. 1.全能搜索家CodePilot 2.0 你要找的是文件?是文件夹? ...
- pod创建的工程找不到库
ld: library not found for -lAFNetworking app工程 和 Pod工程里面的所有库 Build Active Architecuture Only 所有库都设 ...
- intent 传参数
一.传递List<String>和List<Integer>以下以传递List<String>为例,发送List<String>语法为:intent.p ...
- java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
- hdu 2048
PS:WA了两次...主要是没注意到fac的大小好像只能写到9...要用long long型递归求阶乘... 然后就是错排公式...百度下.. 代码: #include "stdio.h&q ...