http://poj.org/problem?id=2240
Test instructions: Now you have n kinds of currency, after the currency is exchanged, eventually still need to be converted into the original currency, if one of the currencies increased, output "Yes", otherwise output "No".
At first, tle, because the position of return 1 was misplaced. At first, I did not think, directly to the final judgment ...
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstdlib>#include<cstring>#include<map>#include<queue>using namespacestd;#defineMAXN 150#defineINF 0x3f3f3f3fintHEAD[MAXN], V[MAXN];DoubleDIST[MAXN];intCNT;structnode{intu, V, next; DoubleW;} MAPS[MAXN];voidADD (intUintVDoubleW) {MAPS[CNT].V=v; MAPS[CNT].W=W; Maps[cnt].next=Head[u]; Head[u]= cnt + +;}intSPFA (ints) {memset (V,0,sizeof(v)); Queue<int>Q; Q.push (s); V[s]=1; while(Q.size ()) {intp=Q.front (); Q.pop (); V[P]=0; for(intI=HEAD[P]; i!=-1; I=Maps[i].next) { intQ =maps[i].v; Doublet = dist[p]*MAPS[I].W; if(dist[q]<t) {Dist[q]=T; if(!V[q]) {V[q]=1; Q.push (q); } } } if(Dist[s] >1) return 1; } return 0;}intMain () {intN, M, t=1; Charstr[ -]; while(SCANF ("%d", &N), N) {map<string,int>s; S.clear (); for(intI=1; i<=n; i++) {scanf ("%s", str); S[STR]=i; } scanf ("%d", &m); Chara[ -], b[ -]; DoubleC; CNT=0; memset (Head,-1,sizeof(head)); for(intI=1; i<=m; i++) {scanf ("%s%lf%s", A, &C, B); ADD (S[a], s[b], c); } intAns =0; for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) Dist[j]= -INF; Dist[i]=1; Ans+=SPFA (i); if(ANS) Break; } if(ANS) printf ("Case %d:yes\n", t++); Elseprintf"Case %d:no\n", t++); } return 0;}
View Code
Arbitrage POJ 2240