HDU 1217 Arbitrage(spfa算法+链式前向星)

Problem 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 file 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
这道题的题意就是给你n种货币,对应货币兑换有不同的汇率。
问你在各种货币之间随意兑换是否能够赚钱,能输出Yes,否则输出No.
这里用到的是spfa算法(出现环,例如1->2,2->3,3->1)。
先建立一个有向图,边权为汇率。
一般我们都是以1,2,3代表点,这里给出的是货币的名称,那么我们就用到map,map的下标是货币名字,对应的key值
就是点的标号(1,2,3,4,5,....),这样处理就方便我们接下来的操作。
我们把不同货币的汇率设置为0,相同的设置为1,之后输出两种货币以及汇率,边权就是两种货币的汇率。
然后就是对每种货币(即每个点(作为源点))进行一次spfa算法,再进行判断即可。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 36;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
double G[maxn][maxn],dis[maxn];
bool vis[maxn];
int spfa(int st,int n)
{
    queue<int> que;
    mes(vis,false);
    mes(dis,0);
    dis[st]=1.0;
    vis[st]=true;
    que.push(st);
    while(!que.empty()){
        int u=que.front();
        que.pop();
        vis[u]=false; //用过的点设置为false,因为最后的结果一定出现环,所以该点我们还需要用到。
        for(int i=1;i<=n;++i){
            if(G[u][i]*dis[u]>dis[i]){
                dis[i]=G[u][i]*dis[u];
                if(dis[st]>1.0){ //进行一次松弛操作,成功之后判断源点的值,若大于1,直接返回1表示能够赚钱,到这里即可结束spfa。
                    return 1;
                }
                if(!vis[i]){
                    vis[i]=true;
                    que.push(i);
                }
            }
        }
    }
    return 0;
}
int main()
{
    fin;
    int n,m;
    double w;
    char money[20];
    map<string,int> mp;
    char s1[20],s2[20];
    int cnt=0;
    while(~scanf("%d",&n)&&n){
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                G[i][j]=(i==j)?1.0:0;
            }
        }
        for(int i=1;i<=n;++i){
            scanf("%s",money);
            mp[money]=i;
        }
        m=read();
        while(m--){
            scanf("%s%lf%s",s1,&w,s2);
            G[mp[s1]][mp[s2]]=w;
        }
        bool flag=false;
        for(int i=1;i<=n;++i){
            if(spfa(i,n)){
                flag=true;
                break;
            }
        }
        printf("Case %d: ",++cnt);
        cout<<(flag?"Yes":"No")<<endl;
    }
    return 0;
}
链式前向星
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 36;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}

struct edge{
    int to,next;
    double w;
};
edge G[maxn*maxn]; //有向图,这里需要注意
int head[maxn],tot;
double dis[maxn];
bool vis[maxn];
void init()
{
    mes(head,-1);
    tot=1;
}
void addEdge(int u,int v,double w)
{
    G[tot].to=v;
    G[tot].next=head[u];
    G[tot].w=w;
    head[u]=tot++;
}
int spfa(int st)
{
    queue<int> que;
    mes(vis,false);
    mes(dis,0);
    dis[st]=1.0;
    vis[st]=true;
    que.push(st);
    while(!que.empty()){
        int u=que.front();
        que.pop();
        vis[u]=false;
        for(int i=head[u];~i;i=G[i].next){
            int v=G[i].to;
            //double w=G[i].w; 之前没注意到这里。
            if(G[i].w*dis[u]>dis[v]){ //G[i].w真扑街,double的,妈的之前用int w=G[i].w...细节啊。
                dis[v]=G[i].w*dis[u];
                if(dis[st]>1.0){
                    return 1;
                }
                if(!vis[v]){
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
    return 0;
}
int main()
{
   //fin;
    int n,m;
    double w;
    char money[20];
    map<string,int> mp;
    char s1[20],s2[20];
    int cnt=0;
    while(~scanf("%d",&n)&&n){
        init();
        for(int i=1;i<=n;++i){
            scanf("%s",money);
            mp[money]=i;
        }
        m=read();
        while(m--){
            scanf("%s%lf%s",s1,&w,s2);
            addEdge(mp[s1],mp[s2],w);
        }
        bool flag=false;
        for(int i=1;i<=n;++i){
            if(spfa(i)){
                flag=true;
                break;
            }
        }
        printf("Case %d: ",++cnt);
        cout<<(flag?"Yes":"No")<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值