(省赛选拔系列--团体赛) Arbitrage? (Floyd 优化)

A. Arbitrage?

Time Limit: 5000ms

Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name:Main

SubmitStatus
PID: 4291

If you are going to travel to the World Finals, you cannot rely on Czech Crowns. You would have to exchange your money for various foreign currencies. This problem deals with multiple currencies and their exchange rates. Your task is to verify that some
set of exchange rates is safe, namely detect a possibility of so-called arbitrage.

An arbitrage is a risk-free combination of buy and sell operations that gains profit from imbalance in market prices. The prices may apply to various things, typically stock exchange but also currencies.

Input

The input consists of several test cases. Each case begins with a line containing one positive integer numberC, 1
≤ C ≤ 200, the number of currencies.

The second line of each test case contains C currency codes separated by a space. Each code is composed of 3 uppercase letters and all codes in one test case are different.

The third line contains one integer number R, 0 ≤ R ≤ C ·
(C ? 1), the number of exchange rates available. Each of the following
R
lines contains one exchange rate in the following format: first currency code, space, second currency code, space, integer numberAi, colon (“:”), and integer number
Bi. The meaning is as follows: If you payAi units of the first currency, you will get
Bi units of the second currency. You may assume that 1≤ Ai,Bi ≤
100 and that the two currencies are different.

Output

For each test case, print one line of output. If there exists any possible sequence of currency exchange operations that would result in a profit, the line should contain the word “Arbitrage”. Otherwise, simply print “Ok”.

The word profit in this case means that you start with any amount of any currency and after performing any number of exchanges you will have strictly higher amount of the same currency.

Sample Input

2
CZK EUR
2
CZK EUR 25:1
EUR CZK 1:25
2
GBP USD
2
USD GBP 8:5
GBP USD 5:9
3
BON DEM CZK
3
DEM BON 1:6
BON CZK 1:5
DEM CZK 1:20
3
CZK EUR GBP
3
CZK EUR 24:1
EUR GBP 5:4
GBP CZK 1:30
3
CZK USD GBP
4
CZK USD 28:1
CZK GBP 31:1
GBP CZK 1:31
USD GBP 1:1
0

Sample Output

Ok
Arbitrage
Ok
Ok
Arbitrage
题意:你要参加WF了 会去c个国家 每去一个国家都需要换钱
他们直接由n中汇率
问你回来的时候是否会赚钱
思路:1.把汇率看做权值 每个地方就是图上的点(注意是单向边)
2. 判断是否会回来 没由回来 自然就是ok 了
3 回来了 判断你的钱是否变多了 多了就
Arbitrage

4.用floyd求出 任意n个点之间的汇率

#include<bits/stdc++.h>
using namespace std;
struct point
{
    char str[25];
    int id;
} p[300];
double Map[300][300];
int n;

int findid(char *s)
{
    for(int i=1; i<=n; i++)
    {
        if(strcmp(p[i].str,s)==0)
            return p[i].id;
    }
}
void Floyd()
{
    int i,j,k;
    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            for(k=1; k<=n; k++)
            {
                if(Map[i][j]<Map[i][k]*Map[k][j])
                {
                    Map[i][j]=Map[i][k]*Map[k][j];
                }
            }
}
int main()
{

    while(~scanf("%d",&n),n)
    {
        memset(Map,0,sizeof(Map));
        for(int i=1; i<=n; i++)
        {
            scanf("%s",p[i].str);
            p[i].id=i;
        }
        int m;
        scanf("%d",&m);
        while(m--)
        {
            char ss[25];
            char s[25];
            double k,kk;
            scanf("%s %s %lf:%lf",s,ss,&k,&kk);
            int a,b;
            a=findid(s);
            b=findid(ss);
            Map[a][b]=(kk*1.0/k);
//            printf("%d %d %lf\n",a,b,Map[a][b]);
        }

        Floyd();
        bool f=1;
//                                                    for(int i=1; i<=n; i++)
//                                                    {
//                                                        printf("\n");
//                                                        for(int j=1; j<=n; j++)
//                                                        {
//                                                            printf("%lf ",Map[i][j]);
//                                                        }
//                                                    }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                if(Map[i][j]*Map[j][i]>1.000000001)
                {
                    f=0;
//                    printf("%d %d %lf\n",i,j,Map[i][j]*Map[j][i]);
                    goto th;
                }
            }
th:
        if(f)
            printf("Ok\n");
        else
            printf("Arbitrage\n");
    }
    return 0;
}
时间: 2024-11-05 16:00:28

(省赛选拔系列--团体赛) Arbitrage? (Floyd 优化)的相关文章

(省事选拔系列---团体赛)God Save the i-th Queen

God Save the i-th Queen Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 4299 Did you know that during the ACM-ICPC World Finals a big chessboard is installed every year and is avai

20160423/24省赛选拔总结

最近一直在重温c语言,没有时间总结比赛,眼看还有一年就要准备实习了,大部分精力都放在为找工作做准备的阶段,没有太多精力刷题,能做的只是总结每一场训练赛中的失误与不足 省赛选拔虽然选上了,但还是很不顺利,,,,,,,,,,,我和小王去年国赛因为题意的问题错失了一枚铜牌,这次再次因为题意翻译出了问题卡了一场比赛:: 第一天:::刚发下来,我们3个人还算很准确,分别翻译A,B,C, 我先把B题翻译出来了,明确了思路,我直接上去敲B题,一遍AC过,我昨晚,学长翻译完了A题,并且有了思路,确定了用状压DP

学渣乱搞系列之dp斜率优化

学渣乱搞系列之dp斜率优化 By 狂徒归来 貌似dp的斜率优化一直很难搞啊,尤其是像我这种数学很挫的学渣,压根不懂什么凸包,什么上凸下凸的,哎...说多了都是泪,跟wdd讨论了下,得出一些结论.本文很大部分参考了大神Accept的文章,不过此神貌似早已绝迹江湖,这篇文章写得好,也写得很差,前半部分叙述得很好,可是关键,关键部分说得很乱,有些许错误,很多大神都进行了评论指出,但是大神Accept貌似没有修改的意思,故重新总结下,以便自己以后查阅和复习啊. 下面看一个例题Print Article.

acm省赛选拔组队赛经验谈

省赛组队赛已经进行5场了,过半了. 从曾经的不会组队到如今逐渐磨合,尽管每次都有遗憾,可是我认为我们一直在进步.有些失误是要记录下来下次不能再犯的! 经验: 1:上场開始一定要有人(英语能力和算法综合能力较强者)读全然部题目,对全部题目的难易程度做一个大概推断,以确定做题顺序,不要在比赛完了发现有水题没有看! 2:对于一个自己没有100%把握AC的题目,最好拉一个队友讲一下思路,假设队友认可了再写程序,防止一道题目花费了一个多小时代码敲到快完了发现思路是不可行的,并且这样在你一次不能AC的情况下

[转] 擎天哥as3教程系列第二回——性能优化

所谓性能优化主要是让游戏loading和运行的时候不卡. 一  优化fla导出的swf的体积? 1,  在flash中,舞台上的元件最多,生成的swf越大,库里面有连接名的元件越多,swf越大.当舞台上没有元件且库里面的元件没有连接名的话生成的swf最小. 2,  一个flash动画有10帧,10帧上面全部是位图和用一个位图播放器播放这10张图片谁消耗的cpu更高? 答:flash动画播放消耗性能更高,因为swf文件里虽然也是位图,但是swf里面的播放机制是能播放位图,矢量图,声音,视频等.所以

POJ2240——Arbitrage(Floyd算法变形)

Arbitrage DescriptionArbitrage 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

hdu 1217 Arbitrage Floyd||SPFA

转载请注明出处:http://acm.hdu.edu.cn/showproblem.php?pid=1217 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

mysq&#39;l系列之10.mysql优化&amp;权限控制

网站打开慢如何排查 1.打开网页, 用谷歌浏览器F12, 查看network: 哪个加载时间长就优化哪个 2.如果是数据库问题 2.1 查看大体情况 # top # uptime  //load average 负载 mysql> show full processlist; 2.2 查看慢查询日志: long_query_time = 1 log-slow-queries = /data/3306/slow.log 日志分析工具: mysqldumpslow  mysqlsla  myprof

Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)

描述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