zoukankan      html  css  js  c++  java
  • 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 = =

    作者:FreeAquar
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    模拟_大数字符串(HDU_2054)
    DP_字串匹配(HDU_1501)
    动态字典树_字串标记查找+大数(HDU_4099)
    动态字典树_字串查找匹配(HDU_1075)
    动态字典树+DFS(HDU_1298)
    动态字典树_拆分查找(HDU_1247)
    动态字典树_统计前缀子串(HDU_1251)
    动态字典树_统计子串(HDU_2846)
    字典树讲解
    HTML5语义标签的实践(blog页面)
  • 原文地址:https://www.cnblogs.com/FreeAquar/p/2091431.html
Copyright © 2011-2022 走看看