hdu 1217 floyd+map容器

原创 2016年05月31日 14:55:41

Arbitrage

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:65536/32768 K (Java/Others)
Total Submission(s): 6518    Accepted Submission(s): 3015

Problem Description

Arbitrage is the use of discrepanciesin currency exchange rates to transform one unit of a currency into more thanone unit of the same currency. For example, suppose that 1 US Dollar buys 0.5British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys0.21 US dollar. Then, by converting currencies, a clever trader can start with1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5percent.

Your job is to write a program that takes a list of currency exchange rates asinput and then determines whether arbitrage is possible or not.

 

 

Input

The input file will contain one or moretest 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 nlines each contain the name of one currency. Within a name no spaces willappear. The next line contains one integer m, representing the length of thetable to follow. The last m lines each contain the name ci of a sourcecurrency, a real number rij which represents the exchange rate from ci to cjand a name cj of the destination currency. Exchanges which do not appear in thetable are impossible.
Test cases are separated from each other by a blank line. Input is terminatedby a value of zero (0) for n.

 

 

Output

For each test case, print one linetelling whether arbitrage is possible or not in the format "Case case:Yes" respectively "Case case: No".

 

 

Sample Input

3

USDollar

BritishPound

FrenchFranc

3

USDollar0.5 BritishPound

BritishPound10.0 FrenchFranc

FrenchFranc0.21 USDollar

 

3

USDollar

BritishPound

FrenchFranc

6

USDollar0.5 BritishPound

USDollar4.9 FrenchFranc

BritishPound10.0 FrenchFranc

BritishPound1.99 USDollar

FrenchFranc0.09 BritishPound

FrenchFranc0.19 USDollar

 

0

 

 

Sample Output

Case1: Yes

Case2: No

 

 

Source

Universityof Ulm Local Contest 1996

 

 

 

分析:

题意:给几个国家,然后给这些国家之间的汇率。判断能否通过这些汇率差进行套利交易。

Floyd的算法可以求出任意两点间的最短路径,最后比较本国与本国的汇率差,如果大于1,则可以。否则不可以

这道题的做法和2112的做法是一样的  都是最短路加上map容器

容器的用法有在2112中讲过 这里就不多说了

AC:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define INF 0x3f3f3f3f
#define MAX 100
using namespace std;
int vis[MAX],dis[MAX];
double map1[MAX][MAX];
char currency[31];
int n,m;
void floyd()
{
    for(int k = 0; k < n; k++)
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                if(map1[i][j] < map1[i][k]*map1[k][j])
                    map1[i][j] = map1[i][k]*map1[k][j];
    for(int i = 0; i < n; i++)
        if(map1[i][i] > 1.0)
        {
            printf("Yes\n");
            return ;
        }
    printf("No\n");
    return ;

}
int main()
{
    int case1 = 1;
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        map<string,int>s;
        for(int i = 0; i < n; i++)
        {
            scanf("%s",currency);
            s[currency] = i;
        }
        scanf("%d",&m);
        char p[31],q[31];
        double c;
        memset(map1,0,sizeof(map1));
        for(int i = 0; i < m; i++)
        {
            scanf("%s%lf%s",p,&c,q);
            map1[s[p]][s[q]] = c;
        }
        printf("Case %d: ",case1++);
        floyd();
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hdu 2923 floyd+map容器

Einbahnstrasse TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others) T...

hdu 1217(Floyd变形)

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

HDU1217 - Arbitrage - 思维+乘法求最大的最短路(Floyd+spfa)

1.题目描述: Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...

hdu 1217 Arbitrage(最短路--floyd+对数的性质)

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota...

hdu1217 floyd

#include #include #include #include #include #include #include #define N 1005 const double I...

hdu1217(floyd算法)

Description Arbitrage is the use of discrepancies in currency exchange rates to transform one u...

HDU1217(floyd算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217   package D0801; /* * 题目大意:就是套汇率,如果最后套到的钱...
  • lhfight
  • lhfight
  • 2012年08月01日 17:21
  • 610

hdu 1217/poj 2240 Arbitrage【floyd】

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...

hdu 1217(Floyd 算法)

自己还是比较容易受外界影响,外面一有什么风吹草动,就没心思继续做题了,该死,就不能集中下精神  啊, #include #include #include using namespace st...
  • ljfbest
  • ljfbest
  • 2011年08月01日 11:28
  • 746

hdu 1217 Arbitrage Floyd || SPFA

转载请注明出处:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1217 floyd+map容器
举报原因:
原因补充:

(最多只允许输入30个字)