码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-13 20:57:26      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:算法   数学   

A. Arbitrage?

Time Limit: 5000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name:Main
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;
}

 

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

标签:算法   数学   

原文地址:http://blog.csdn.net/u012349696/article/details/45030999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!