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;
}


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

ACM-最短路(SPFA,Dijkstra,Floyd)之最短路——hdu2544

ACM 最短路(SPFA,Dijkstra,Floyd) 最短路 hdu2544
  • lx417147512
  • lx417147512
  • 2014年05月27日 11:59
  • 3352

HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自...
  • qq_26525215
  • qq_26525215
  • 2016年05月27日 21:11
  • 3261

Dijkstra算法、Floyd算法的区别与联系,并由此谈到greedy和DP

首先,Dijkstra算法与Floyd算法都是广度优先搜索的算法。都可以用来求单源点到其他所有点的最短路径。那么这两者的原理分别是怎样?彼此又有什么区别呢?求此有向图中起点1到其他所有点的最短距离在本...
  • liuyanling_cs
  • liuyanling_cs
  • 2017年02月21日 22:44
  • 2268

hdu 2923 floyd+map容器

Einbahnstrasse TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others) T...
  • qq_27437781
  • qq_27437781
  • 2016年05月31日 14:57
  • 127

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

1.题目描述: Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • dragon60066
  • dragon60066
  • 2017年03月30日 21:13
  • 152

hdu 1217(Floyd变形)

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • hexianhao
  • hexianhao
  • 2016年05月15日 15:34
  • 114

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

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota...
  • qq_24451605
  • qq_24451605
  • 2015年04月09日 08:43
  • 466

hdu 1217 Arbitrage 两种算法AC代码,Floyd+Bellman-Ford 大水题一枚 注意是有向图~~

Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform on...
  • Lionel_D
  • Lionel_D
  • 2015年02月17日 21:38
  • 752

hdu 1217 Arbitrage Floyd || SPFA

转载请注明出处:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...
  • u012860063
  • u012860063
  • 2014年05月07日 20:48
  • 969

HDU-1217 Arbitrage (有向图最大环[Floyd])

大致思路:本题是看的hdu最小环的题目推荐的,所以很容易就像到了最小环的求法,不过本题是有向图,而且求最大环,将加法换成乘法,floyd不变即可,因为有向图的环可以由2个点构成,复杂度为:O(n^2)...
  • idealism_xxm
  • idealism_xxm
  • 2016年04月15日 17:00
  • 379
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1217 floyd+map容器
举报原因:
原因补充:

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