URAL 1291 Gear-wheels(BFS)

Posted 贱人方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了URAL 1291 Gear-wheels(BFS)相关的知识,希望对你有一定的参考价值。

Gear-wheels

Time limit: 1.0 second
Memory limit: 64 MB
— Arny! What happened with coordinator?
Bad 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.
Despite 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.
After 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

First line of input contains the number of gear-wheels in mechanism N (1 ≤ N ≤ 1000). The next N lines contain the information about the gear-wheels. i-th line contains K (1 ≤ K ≤ 1000) — the number of cogs on the i-th gear-wheel followed by the list of gears, that are connected to the i-th one. Zero ends the list.
The 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

Output should contain N lines. In the i-th line there is a speed of rotation of i-th 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

inputoutput
4
10 2 3 0
20 1 0
40 1 4 0
100 3 0
1 6
6/1
-3/1
-3/2
3/5
Problem Author: Pavel Egorov
【分析】给你n个齿轮,然后下面n行,每行第一个数为齿轮的齿数,后面为与他链接的齿轮编号,最后一行给出发动机所在齿轮编号及转速,求其余齿轮的速度,用最简分数表示。
#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+10;
const int M = 124750+10;
const int mod=1e9+7;
int n,m,k,tot=0,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==0)return y;
    return gcd(x-y,y);
}
void bfs(){
    queue<int>q;
    q.push(s);vis[s]=1;ans[s].f=t;ans[s].s=1;
    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!=-1;i=edg[i].next){
            int v=edg[i].to;
            if(!vis[v]){
                vis[v]=1;
                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,0);met(head,-1);
    int u,v;
    scanf("%d",&n);
    for(int u=1;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=1;i<=n;i++){
        if(ans[i].f>0){
            printf("%d/%d\n",ans[i].f,ans[i].s);
        }else if(ans[i].f==0){
            printf("0/1\n");
        }else {
            printf("-%d/%d\n",-ans[i].f,ans[i].s);
        }
    }
    return 0;
}

 

以上是关于URAL 1291 Gear-wheels(BFS)的主要内容,如果未能解决你的问题,请参考以下文章

CF1291

1291 火车线路

leetcode1291

Codeforces 1291D

codevs 1291 火车线路

leetcode1291. Sequential Digits