Link:
http://acm.hdu.edu.cn/showproblem.php?pid=1217
Topic:
Arbitrage
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2594 accepted Submission (s): 1167
Problem Description
Arbitrage is the ' use ' discrepancies in currency exchange rates to transform one unit of a currency into more than one UN It's the same currency. For example, suppose 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc Buys 0.21 US dollar. Then, by converting currencies, a clever trader can-start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, MA King a profit of 5 percent.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether is possible or not.
Input
The input file would contain one or more test cases. Om the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' of each ' test case there ' is ' an integer n (1<=n<=30) S. The next n lines each contain the name of one currency. Within a name no spaces would appear. The next line contains one integer m and representing the length of the table to follow. The last m lines each contain the name CI to a source currency, a real number Rij which represents the exchange rate from CI to CJ and a name CJ of the destination currency. Exchanges which does not appear in the table are impossible.
Test cases are separated from each of the blank line. The Input is terminated by a value of zero (0) for N.
Output
For each test case, print one line telling whether arbitrage are possible or not in the format ' case Case:yes ' respectivel Y "Case Case:no".
Sample Input
3
USDollar
Britishpound
Frenchfranc
3
USDollar 0.5 Britishpound
Britishpound 10.0 Frenchfranc
Frenchfranc 0.21 USDollar
3
USDollar
Britishpound
Frenchfranc
6
USDollar 0.5 Britishpound
USDollar 4.9 Frenchfranc
Britishpound 10.0 Frenchfranc
Britishpound 1.99 USDollar
Frenchfranc 0.09 Britishpound
Frenchfranc 0.19 USDollar
0
Sample Output
Case 1:yes
Case 2:no
Source
University of ULM Local Contest 1996
Recommend
Eddy
Analysis and Summary:
According to the need to know, as long as you find the positive ring can be judged to be yes, so you can spfa the ring, or to build the adjacency matrix map with Floyd algorithm, judge whether there is d[i][i]>1 can be.
1.Floyd
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <map>
#include <string> using namespace std;
typedef double TYPE;
const int INF = 0X7FFFFFF;
const int VN = 35;
const int EN = VN*VN;
Map<string, int>mp;
int n,size,cnt;
int HEAD[VN];
Type D[VN][VN];
void Init () {size=0; cnt=0;
Mp.clear ();
for (int i=1; i<=n; ++i) {d[i][i] = 1;
for (int j=i+1; j<=n; ++j) d[i][j] = D[j][i] = 0; } void Floyd () {for (int k=1; k<=n; ++k) for (int i=1; i<=n; ++i) for (int j=1; j<=n;
++J) {D[i][j] = max (D[i][j], d[i][k]*d[k][j]);
int main () {char str1[50], str2[50];
int cas=1,m,u,v;
Double W;
while (scanf ("%d", &n) &&n) {init ();
for (int i=0; i<n; ++i) {scanf ("%s", str1); MP[STR1] = ++cnt;
} scanf ("%d", &m);
for (int i=0; i<m; ++i) {scanf ("%s%lf%s", STR1,&W,STR2);
U = mp[str1], v = mp[str2];
D[U][V] = W;
BOOL Flag=false;
Floyd ();
for (int i=1; i<=n; ++i) if (d[i][i]>1.0) {flag=true; break;}
printf ("Case%d:", cas++);
if (flag) puts ("Yes");
Else puts ("No");
return 0; }
2.SPFA Positive Ring
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <queue&
Gt
#include <map> using namespace std;
typedef double TYPE;
const int INF = 0X7FFFFFF;
const int VN = 35;
const int EN = VN*VN;
Map<string, int>mp;
int n,size,cnt;
int HEAD[VN];
BOOL INQ[VN];
int COUNTER[VN];
Type D[VN]; struct Edge{int v,next; Type W;}
E[en];
void Addedge (int u,int v,type W) {e[size].v=v, e[size].w=w;
E[size].next = Head[u];
Head[u] = size++;
} void Init () {size=0; cnt=0;
Memset (Head,-1, sizeof (head));
Mp.clear ();
BOOL SPFA (int src) {memset (counter, 0, sizeof (counter));
memset (inq, 0, sizeof (INQ));
for (int i=1; i<=n; ++i) d[i]=0;
D[SRC] = 1;
queue<int>q;
Q.push (SRC); while (!q.empty ()) {int u = q.front ();
Q.pop ();
Inq[u] = false; for (int e=head[u]; e!=-1; e=e[e].next) {Type tmp = D[U]*E[E].W;
if (D[E[E].V] > tmp) {if (d[e[e].v]<tmp) {D[E[E].V] = tmp;
if (!INQ[E[E].V]) {INQ[E[E].V] = true;
Q.push (E[E].V);
if (++counter[e[e].v]>=n) {return true;
}}} return false;
int main () {char str1[50], str2[50];
int cas=1,m,u,v;
Double W;
while (scanf ("%d", &n) &&n) {init ();
for (int i=0; i<n; ++i) {scanf ("%s", str1);
MP[STR1] = ++cnt;
} scanf ("%d", &m);
for (int i=0; i<m; ++i) {scanf ("%s%lf%s", STR1,&W,STR2);
U = mp[str1], v = mp[str2];
Addedge (U,V,W);
BOOL Flag=false; for (int i=1; i<=n; ++i) {if (SPFA (i)) {flag=true;
Break
printf ("Case%d:", cas++);
if (flag) puts ("Yes");
Else puts ("No");
return 0; }
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/