[kuangbin带你飞]专题四 最短路练习 I - Arbitrage(判断负环)

I - Arbitrage

题目链接:https://vjudge.net/contest/66569#problem/I

题目:

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

题意:给你n种货币,和m种货币转换关系还有之间的单位货币数,问能否经过这些关系使得原先货数增加思路:题没有给原先货币数,故令原先货币数为1,这题不是计算路权和而是路权积,利用spfa算法即可快速判断,由于数据范围小可直接用邻接矩阵来存图,用时79s

//
// Created by hy on 2019/7/20.
//
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
#include <map>
#include<memory.h>
using namespace std;
const int maxn=105;
#define MAX 0x3f3f3f3f
int n,m;
double road[maxn][maxn];
double d[maxn];
int book[maxn];
int rnt[maxn];
bool spfa(int s)
{
    memset(book,0,sizeof(book));
    memset(d,MAX,sizeof(d));
    memset(rnt,0,sizeof(rnt));
    queue<int>qu;
    qu.push(s);
    book[s]=1;
    d[s]=1;
    rnt[s]++;
    while(!qu.empty())
    {
        int now=qu.front();
        qu.pop();
        book[now]=0;
        for(int i=1;i<=n;i++)
        {
            if(d[i]<d[now]*road[now][i])
            {
                d[i]=d[now]*road[now][i];
                if(!book[i])
                {
                    qu.push(i);
                    book[i]=1;
                    rnt[i]++;
                    if(rnt[i]>n-1)
                        return true;
                }
            }
        }
    }
    return false;
}
int main() {

    int res=0;
    while (~scanf("%d", &n)) {
        if(n==0)
            break;
        string str;
        map<string, int> mp;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                if(i==j)
                    road[i][j]=0;
                else
                    road[i][j]=MAX;
            }
        }
        for (int i = 1; i <= n; i++) {
            cin >> str;
            mp[str] = i;

        }
        string str1, str2;
        double num;
        scanf("%d",&m);
        for (int i = 1; i <= m; i++) {
            cin >> str1 >> num >> str2;
            road[mp[str1]][mp[str2]] = num;
        }
        res++;
        if (spfa(1))
            printf("Case %d: Yes\n",res);
        else
            printf("Case %d: No\n",res);

    }
    return 0;
}



原文地址:https://www.cnblogs.com/Vampire6/p/11219628.html

时间: 2024-11-11 21:46:39

[kuangbin带你飞]专题四 最短路练习 I - Arbitrage(判断负环)的相关文章

【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 邻接矩阵实现的单源最短路 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include

[ An Ac a Day ^_^ ][kuangbin带你飞]专题四 最短路练习 POJ 2240 Arbitrage spfa求负环

题意就是问倒腾外币能不能升值 不用spfa 用其他的最短路算法也可以 松弛条件换成dist[v]<dist[u]*e[u][i].value 当然 貌似只有spfa有这个坑…… 有A  (value>1.0) A 这种情况……我的天 用Dij Floyd都只用判断如果松弛到了自己 那么一定有环 直接跳出就行 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<

[kuangbin带你飞]专题四 最短路练习

A. POJ 2387  Til the Cows Come Home 模板题. #include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<string> #include<map> #include<queue> #in

[ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home

求1到N的最短路 注意有重边 跑一遍dijkstra就行 1 /* *********************************************** 2 Author :SunYuefeng 3 Created Time :2016/10/22 14:18:06 4 File Name :A.cpp 5 ************************************************ */ 6 7 #include<cstdio> 8 #include<io

[kuangbin带你飞]专题四 最短路练习 POJ 2253 Frogger

求第一个点到第二个点的所有通路上最长的边 dijkstra的变形 每次松弛的是每条边通路上的的最长的边 WA了好几次是因为用了%lf 改成%f就过了…… 1 /* *********************************************** 2 Author :SunYuefeng 3 Created Time :2016/10/22 14:18:06 4 File Name :A.cpp 5 ******************************************

[kuangbin带你飞]专题四 最短路练习 E - Currency Exchange

E - Currency Exchang 题目链接:https://vjudge.net/contest/66569#problem/E 题目: Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with thes

[kuangbin带你飞]专题四 最短路练习 F - Wormholes (判断负环)

F - Wormholes 题目链接:https://vjudge.net/contest/66569#problem/F 题目: While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of