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) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- 进制转换器(十进制转n进制)
#include<stdio.h> #include<stdlib.h> #define MAXSIZE 100 /*链栈类型定义*/ typedef struct node ...
- Android开发-开发前的配置
一.JDK (不用安装) JDK是 java 语言的软件开发工具,主要用于移动设备.嵌入式设备上的java应用程序. JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JA ...
- mac终端下运行shell脚本
最近公司要弄关于IOS下自动化打包的东西,研究了用命令行的形式来代替手工的方式来处理.即: 用xcodebuild 和xcrun 语法来进行脚本实现. 但由于语法的结构够了,另一个问题产生了, ...
- String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。
// 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...
- 使用Wireshark捕捉USB通信数据
USB,是英文Universal Serial Bus(通用串行总线)的缩写,而其中文简称为“通串线”,是一个外部总线标准,用于规范电脑与外部设备的连接和通讯.USB接口支持设备的即插即用和热插拔功能 ...
- go 函数
GO 程序设计 函数 1.新的数据类型 mytype 用户可以根据自身需要定义新的数据类型,下面的mytype就是新定义的数据类型,其中第二个是一个结构体,结构体在go语言中是非常有用的数据类型, ...
- Yii2 GridView自定义链接之重写 ActionColumn
最近刚开始用yii2,真是超棒的,但是也有许多不足的地方,今天要说的就是GridView链接问题. <?= GridView::widget([ 'dataProvider' => $ ...
- Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,解决方法
Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,外面机器无法正常连接. 解决: 虚机换个IP即可正常连接,原因不明,有可能为公司网管对该IP做了某些限制. PS:VMware中只需将网络 ...
- Reason we use Camel
Camel is mainly for integration purpose, in our project we also use it inside the single component t ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...