Poj1860-currency exchange-is sentenced to positive ring

Source: Internet
Author: User

The exchange of the two currencies can be treated as two sides, and the Bellman_ford algorithm will be better after the graph is built.

The Bellman_ford algorithm can be used to handle negative edges, so it is possible to determine if a negative ring exists. In turn, it is possible to determine whether positive loops exist.

1 /*--------------------------------------------------------------------------------------*/2 //Helica ' s header3 //Second Edition4 //2015.11.75 //6#include <algorithm>7#include <iostream>8#include <cstring>9#include <ctype.h>Ten#include <cstdlib> One#include <cstdio> A#include <vector> -#include <string> -#include <queue> the#include <stack> -#include <cmath> -#include <Set> -#include <map> +  - //Debug function for a n*m array + #defineDebug_map (n,m,g) printf ("\ n"); for (int i=0;i< (N); i++) A{ for(intj=0;j< (M); J + +){ atprintf"%d", G[i][j]);} printf"\ n");}  - //Debug function for int,float,double,etc. - #defineDebug_var (X) cout<< #X "=" <<X<<endl; - /*--------------------------------------------------------------------------------------*/ - using namespacestd; -  in Const intMAXN = -+Ten; - intn,m,t,s; to intTol; + DoubleV; -  the structEdge * { $     intu,v;Panax Notoginseng     DoubleC,r; -}e[Ten*MAXN]; the  + DoubleDIST[MAXN]; A  the BOOLBellman_ford (intStartintN) + { -memset (Dist,0,sizeofDist); $Dist[start] =V; $  -      for(intI=1; i<n;i++) -     { the         BOOLFlag =false; -          for(intj=0; j<tol;j++)if(DIST[E[J].V] < (DIST[E[J].U]-E[J].C) *E[J].R)Wuyi         { theDIST[E[J].V] = (DIST[E[J].U]-E[J].C) *E[J].R; -Flag =true;  Wu         } -         if(!flag)return false; About     } $  -      for(intj=0; j<tol;j++) -     { -         if(DIST[E[J].V] < (DIST[E[J].U]-E[J].C) *E[J].R) A             return true; +     } the     return false; - } $  the intMain () the { the      while(~SCANF ("%D%D%D%LF",&n,&m,&s,&V)) the     { -         intb; in         Doubler1,r2,c1,c2; theTol =0; the          for(intI=0; i<m;i++) About         { thescanf"%D%D%LF%LF%LF%LF",&a,&b,&r1,&c1,&r2,&C2); theE[TOL].U = A; E[TOL].V =b; theE[TOL].C = C1; E[TOL++].R =R1; +E[TOL].U = b; E[TOL].V =A; -e[tol].c = C2; E[TOL++].R =R2; the         }Bayi         if(Bellman_ford (s,n)) theprintf"yes\n"); the         Else -printf"no\n"); -  the     }     the}

Poj1860-currency exchange-is sentenced to positive ring

Related Article

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.