pku 2240 Arbitrage

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#define eps 1e-6
using namespace std;
int m, n, count[32];
double Map[32][32];
map<string, int> money;
string type;

bool spfa()
{
queue<int> q;
double dist[32];
int visited[32];

memset(visited, 0, sizeof(visited));
memset(count, 0, sizeof(count));
for(int i=1; i<=n; i++)
dist[i]=0;
dist[1]=1;
visited[1]=1;
q.push(1);

while(!q.empty())
{
int x=q.front();
q.pop();
if(count[x]>n) return 1;
visited[x]=0;

for(int i=1; i<=n; i++)
{
if(dist[x]*Map[x][i]>dist[i]+eps)
{
dist[i]=dist[x]*Map[x][i];
if(!visited[i])
{
visited[i]=1;
q.push(i);
count[i]++;
}
}
}
}
return 0;
}

int main()
{
int cas=1;
while(scanf("%d", &n), n)
{
for(int i=1; i<=n; i++)
{
cin>>type;
money[type]=i;
}
scanf("%d", &m);
for(int i=1; i<=m; i++)
{
string coin1, coin2;
double rate;
cin>>coin1>>rate>>coin2;
Map[money[coin1]][money[coin2]]=rate;
}
printf("Case %d: ", cas++);
if(spfa()) puts("Yes");
else puts("No");
}
return 0;
}


题意:先输入N,表示有N种货币,接下来N行输入N种货币的名字

   再输入M,表示有M种换法, 输入货币A,汇率RAB, 货币B

   要求算出是否能通过兑换汇率赚钱

思路:spfa,用count[]计数,如果有一个的计数超过N,即为有负权环,然后,输出是Yes不是YES = =

 

转载于:https://www.cnblogs.com/FreeAquar/archive/2011/06/27/2091431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值