- To find a circuit, from a currency to exchange a circle back to their own, see whether the exchange rate of the product is greater than 1
- Extend the General shortest road bellman, calculate to Dist (n), is counted to the loop
- Maxdist (k) [v] = max{maxdist (k-1) [V],maxdist (K-1) [U]*c (U,v)}
- #include
- #include
- #include
- #define MAXN 50
- #define MAXM 1000
- using namespace Std;
- struct Exchange
- {
- int CI,CJ;
- Double CIJ;
- }EX[MAXM];
- Char name[maxn][20],a[20],b[20];
- Double X;
- int n,m;
- Double MAXDIST[MAXN];
- int flag;
- int kase = 0;
- int Readcase ()
- {
- scanf ("%d", &n);
- if (n = = 0)
- return 0;
- for (int i = 0; i < n; i++)
- scanf ("%s", Name[i]);
- scanf ("%d", &m);
- for (int i = 0; i < m; i++)
- {
- int q,w;
- scanf ("%s%lf%s", a,&x,b);
- for (q = 0; strcmp (a,name[q]); q++)
- ;
- for (w = 0; strcmp (b,name[w]); w++)
- ;
- Ex[i].ci = q;
- EX[I].CJ = W;
- EX[I].CIJ = x;
- }
- return 1;
- }
- void Bellman (int v0)
- {
- Flag = 0;
- memset (maxdist,0,sizeof (maxdist));
- Maxdist[v0] = 1;
- for (int k = 1; k <= N; k++)//1---N
- {
- for (int i = 0; i < m; i++)
- {
- if (Maxdist[ex[i].ci]*ex[i].cij > MAXDIST[EX[I].CJ])
- MAXDIST[EX[I].CJ] = MAXDIST[EX[I].CI]*EX[I].CIJ;
- }
- }
- if (Maxdist[v0] > 1.0)
- flag = 1;
- }
- int main ()
- {
- while (Readcase ())
- {
- for (int i = 0; i < n; i++)
- {
- Bellman (i);
- if (flag)
- Break
- }
- if (flag)
- printf ("Case%d:yes\n", ++kase);
- Else
- printf ("Case%d:no\n", ++kase);
- }
- }
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130720/39222.html