题意:和poj1860差不多的题意,给n种货币和m个兑换点,问能不能通过兑换使得原来手上的钱增加
思路:Bellman-ford求正环,外加map处理输入字符串
吐槽:把Yes打成YES…WA一次…
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100000
#define LL long long
int cas=1,T;
struct Edge
{
int from,to;
double rab;
Edge(int u,int v,double ra):from(u),to(v),rab(ra){}
};
int n,m,num;
struct Bellford
{
vector<Edge>edges;
vector<int>G[305];
bool inq[305];
double d[305];
int cnt[305];
void init()
{
for (int i = 0;i<=n;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,double ra)
{
int mm;
edges.push_back(Edge(from,to,ra));
mm = edges.size();
G[from].push_back(mm-1);
}
bool bellman_ford(int s)
{
queue<double> q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
for (int i = 0;i<=n;i++)
d[i]=0;
d[s]=1;
inq[s]=1;
q.push(s);
while (!q.empty())
{
int u = q.front();q.pop();
inq[u]=0;
for (int i = 0;i<G[u].size();i++)
{
Edge&e = edges[G[u][i]];
if (d[e.to] < d[e.from]*e.rab)
{
d[e.to] = d[e.from]*e.rab;
if (!inq[e.to])
{
q.push(e.to);
inq[e.to]=1;
if (++cnt[e.to] > n)
return false;
}
}
}
}
return true;
}
};
map <string,int> mapp;
int main()
{
while (scanf("%d",&n) && n)
{
printf("Case %d: ",cas++);
Bellford bf;
bf.init();
mapp.clear();
for (int i = 0;i<n;i++)
{
int j = 1;
string temp;
cin >> temp;
mapp[temp]=i+1;
}
scanf("%d",&m);
for (int i = 0;i<m;i++)
{
double r;
string s1,s2;
cin >> s1 >>r >> s2;
bf.AddEdge(mapp[s1],mapp[s2],r);
}
if (!bf.bellman_ford(1))
puts("Yes");
else
puts("No");
}
//freopen("in","r",stdin);
//scanf("%d",&T);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
题目
Description
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 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, making 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 arbitrage is possible or not.
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of 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 do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format “Case case: Yes” respectively “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