POJ2240 & ZOJ1092 [Arbitrage]

这个题目是给你一些货币及其汇率,计算是否存在套汇的嫌疑。Bellman-Ford水之~

Program Arbitrage;
Var
st:array[-1..300]of string;
e,dis:array[-1..6000]of extended;
a,b:array[-1..6000]of longint;
n,m,tot,i,fu:longint;
flag:boolean;
Function cl(s:string):longint;
 var
i:longint;
 begin
for i:=1 to n do if st[i]=s then exit(i); 
 end;
Procedure addgraph(x,y:longint;z:extended);
 begin
inc(tot);
a[tot]:=x;b[tot]:=y;e[tot]:=z;
 end;
Procedure init;
 var
s,nam1,nam2,hl:string;
i,j,l,cut:longint;
tmp:extended;
 begin
readln(n);
if n=0 then halt;
tot:=0;
for i:=1 to n do 
begin
readln(s);
st[i]:=s;
end;
readln(m);
for i:=1 to m do
begin
readln(s);
l:=length(s);
for j:=1 to l do if s[j]=' ' then break;
nam1:=copy(s,1,j-1);
cut:=j+1;
for j:=cut to l do if s[j]=' ' then break;
hl:=copy(s,cut,j-cut);
cut:=j+1;
nam2:=copy(s,cut,l-cut+1);
val(hl,tmp);
addgraph(cl(nam1),cl(nam2),tmp);
end;
 end;
Procedure bellman(v0:longint);
 var
i,j:longint;
 begin
fillchar(dis,sizeof(dis),0);
dis[v0]:=1;
flag:=false;
for i:=1 to n do
for j:=1 to tot do if (dis[a[j]]*e[j]>dis[b[j]])then dis[b[j]]:=dis[a[j]]*e[j];
if dis[v0]>1 then flag:=true;
 end;
Begin
fu:=0;
while not eof do
begin
init;
inc(fu);
for i:=1 to n do
begin
bellman(i);
if flag then break;
end;
if flag then writeln('Case ',fu,': Yes') else writeln('Case ',fu,': No');
readln;
end;
End.
posted @ 2011-10-14 17:20  NoRush  阅读(235)  评论(0编辑  收藏  举报