4602: [Sdoi2016] Gears

Source: Internet
Author: User

4602: [Sdoi2016] GearsDescriptionThe existing drive system consists of n combination gears and M chains. Each chain is connected with two combination gears U and V, and provides a transmission ratio of x: Y. That is, if only the two combined gears are considered, the gear with the number U is rotated by the X-ring, and the gear with the number v turns the Y-circle. Transmission ratio is a positive indication if the numberThe Gears for u are rotated clockwise, and the gears numbered v rotate clockwise. A negative transmission ratio indicates that if the gear with the number U is rotated clockwise, the number is vthe gears will turn counterclockwise. If the transmission ratios of different chains are incompatible, some gears cannot be rotated. We want to know that the N-combined teeth in the systemcan the wheel rotate at the same time. Inputwith multiple sets of data, the first line is given an integer t, which represents the total number of data groups, followed by the T-group data. The first line of each set of data is given an integer n andm, indicating the total number of gears and the total number of chains. Then there is M-line, which in turn describes each chain, where each line is given four integers u,v,x and y, indicatingonly in the case of this set of linkage, the gear with the number U rotates the X-ring, and the gear with the number V rotates the Y-circle. Note that x is a positive integer, and y isnot 0 integers, but y is likely to be negative. t<=32,n<=1000,m<=10000 and the absolute value of x and Y does not exceedOutputoutput T line, corresponding to each set of data. First, you should output the identification, which is the first set of data, see sample output. After the output of the decision result, if the n combinationThe gear can run normally at the same time, then output yes, otherwise output No. Sample Input2
3 3
1 2 3 5
2 3 5-7
1 3 3-7
3 3
1 2 3 5
2 3 5-7
1 3 3 7Sample OutputCase #1: Yes
Case #2: NoThe following:Dfs, as required to connect edge, edge right is the ratio, and then directly assigned to search, encountered contradictions when exiting. But this problem still has the precision question, can make a few just as well.
#include <stdio.h>#include<iostream>using namespacestd;Const intn=1005;Const intm=10005;#defineEPS 1e-8intT,n,m,i,x,y,u,v,ok,case,p[n];inttot,head[n],next[m<<1],to[m<<1];Doublea[m<<1],f[n];voidAddintUintVDoublex) {Tot++; To[tot]=v; A[tot]=x; Next[tot]=Head[u]; Head[u]=tot;}BOOLDfsintx) {    inti; P[X]=1;  for(i=head[x];i!=-1; i=Next[i]) {        inty=To[i]; if(p[y]==0) {F[y]=f[x]*A[i]; if(Dfs (y) = =0)return 0; } Else        if((F[x]*a[i]-f[y]) >eps)return 0; }    return 1;}intMain () {scanf ("%d",&T); case=0;  while(t--) {scanf ("%d%d",&n,&m); Tot=0;  for(i=1; i<=n;i++) Head[i]=-1, p[i]=0, f[i]=0;  for(i=1; i<=m;i++) {scanf ("%d%d%d%d",&u,&v,&x,&y); Add (U,v,1.0*x/y); Add (V,u,1.0*y/x); } OK=0;  for(i=1; i<=n;i++)            if(p[i]==0) {F[i]=1.0; if(Dfs (i) = =0) {OK=1;  Break; }} case++; printf ("Case #%d:", case); if(ok==0) printf ("yes\n");Elseprintf"no\n"); }    return 0;}

4602: [Sdoi2016] Gears

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.