• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj2240

               Arbitrage


            Time Limit: 1000MS Memory Limit: 65536K
            Total Submissions: 9210 Accepted: 3920

            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 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
            
             
            總結:跟1860相似,但是這個沒有確定的值,不明白為什么按照1860的代碼會wa,找了題解后明白是求是否存在負環回路(變形),可以用bellman-ford做(寫spfa即可)
            判斷時判斷每個節點被更新的次數,如果超過n次,則說明存在負環回路,退出。。。why?
            不明白撒
            也可以用floyd來寫,這個好寫,也好明白,思路跟spfa也差不多
            不明白為什么寫spfa時候用dis來判斷是錯的
            代碼1 floyd
             1#include<stdio.h>
             2#include<string.h>
             3#include<math.h>
             4#define MAX 100
             5#define MAXN 50000
             6char name[40][1000];
             7double f[MAX][MAX];
             8int n,m,t;
             9void init()
            10{
            11    int i,j,a,b;
            12    double rat;
            13    char s1[1000],s2[1000];
            14    for (i=1; i<=n ; i++ )
            15    {
            16        scanf("%s",&name[i]);
            17    }

            18    memset(f,1,sizeof(f));
            19    scanf("%d",&m);
            20    for(i=1; i<=m; i++)
            21    {
            22        scanf("%s%lf%s",&s1,&rat,&s2);
            23        for (j=1; j<=n ; j++ )
            24        {
            25            if (strcmp(s1,name[j])==0) a=j;
            26            if (strcmp(s2,name[j])==0) b=j;
            27        }

            28        f[a][b]=rat;
            29    }

            30}

            31void work()
            32{
            33    int i,j,k;
            34    short flag;
            35    flag=0;
            36    for (k=1; k<=n ; k++ )
            37        for (i=1; i<=n ; i++ )
            38            for (j=1; j<=n ; j++ )
            39                if (f[i][j]<f[i][k]*f[k][j])
            40                {
            41                    f[i][j]=f[i][k]*f[k][j];
            42                }

            43    for (i=1;i<=n ;i++ )
            44    if (f[i][i]>1)
            45    {
            46        flag=1;
            47        break;
            48    }

            49    if (flag==1)
            50    {
            51        printf("Case %d: Yes\n",t);
            52    }

            53    else
            54        printf("Case %d: No\n",t);
            55}

            56int main()
            57{
            58    t=0;
            59    while (scanf("%d",&n)!=EOF&&n!=0)
            60    {
            61        t++;
            62        init();
            63        work();
            64    }

            65    return 0;
            66}

            67
            代碼2 spfa 負權回路
              1#include<stdio.h>
              2#include<string.h>
              3#include<math.h>
              4#define MAX 10000
              5#define MAXN 50000
              6char name[35][1000];
              7int n,m;
              8int e[MAX+5],next[MAX+5],link[MAX+5];
              9double rate[MAX+5],dis[MAX+5];
             10short vis[MAX+5];
             11double total;
             12int t,s;
             13int g[MAX+1];
             14int queue[MAXN+1],head,tail;
             15void add(int x,int y,double rat1)
             16{
             17    s++;
             18    e[s]=y;
             19    rate[s]=rat1;
             20    next[s]=link[x];
             21    link[x]=s;
             22}

             23void init()
             24{
             25    int i,j;
             26    char s1[1000],s2[1000];
             27    int a,b;
             28    double rn;
             29    for (i=1; i<=n ; i++ )
             30    {
             31        scanf("%s",&name[i]);
             32    }

             33    s=0;
             34    memset(next,0,sizeof(next));
             35    memset(link,0,sizeof(link));
             36    for (i=1;i<=n ;i++ )
             37    {
             38        add(0,i,1);
             39    }

             40    scanf("%d",&m);
             41    for(i=1; i<=m; i++)
             42    {
             43        scanf("%s%lf%s",&s1,&rn,&s2);
             44        for (j=1; j<=n; j++)
             45            if (strcmp(s1,name[j])==0) a=j;
             46        for (j=1; j<=n; j++)
             47            if (strcmp(s2,name[j])==0) b=j;
             48        add(a,b,rn);
             49    }

             50}

             51int spfa()
             52{
             53    int i,u,j;
             54    memset(vis,0,sizeof(vis));
             55    memset(g,0,sizeof(g));
             56    queue[1]=0;
             57    for (i=1; i<=n; i++)
             58    {
             59        dis[i]=0;
             60    }

             61    dis[0]=1;
             62    vis[0]=1;
             63    head=0;
             64    tail=1;
             65    while (head!=tail)
             66    {
             67        head++;
             68        u=queue[head];
             69        j=link[u];
             70        while (j!=0)
             71        {
             72            if (dis[u]*rate[j]>dis[e[j]])
             73            {
             74                dis[e[j]]=dis[u]*rate[j];
             75                g[e[j]]++;
             76                if (g[e[j]]>n)
             77                {
             78                    return 1;
             79                }

             80                if (!vis[e[j]])
             81                {
             82                    vis[e[j]]=1;
             83                    tail++;
             84                    queue[tail]=e[j];
             85                }

             86            }

             87            j=next[j];
             88        }

             89        vis[u]=0;
             90    }

             91    return 0;
             92}

             93void work()
             94{
             95    int i,flag;
             96    flag=spfa();
             97    if (flag==1)
             98    {
             99        printf("Case %d: Yes\n",t);
            100    }

            101    else
            102        printf("Case %d: No\n",t);
            103}

            104int main()
            105{
            106    t=0;
            107    while (scanf("%d",&n)!=EOF&&n!=0)
            108    {
            109        t++;
            110        init();
            111        work();
            112    }

            113    return 0;
            114}

            115







            我的代碼一向冗雜……






            posted on 2012-02-12 17:53 jh818012 閱讀(157) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            中文字幕精品久久| 人人狠狠综合久久亚洲婷婷| 久久久综合香蕉尹人综合网| 久久天天躁狠狠躁夜夜不卡| 久久久黄片| 久久精品国产亚洲AV蜜臀色欲| 久久精品国产色蜜蜜麻豆| 伊人色综合久久天天人手人婷 | 国产成人精品综合久久久| 色妞色综合久久夜夜| 久久99中文字幕久久| 蜜臀av性久久久久蜜臀aⅴ麻豆| 成人久久精品一区二区三区| 久久精品成人免费国产片小草| 久久人人爽人人爽人人av东京热 | 婷婷五月深深久久精品| 久久免费视频观看| 久久精品中文字幕一区| 国产叼嘿久久精品久久| 伊人色综合久久天天网| 久久综合九色综合久99| 亚洲午夜久久久久久久久电影网| 精品熟女少妇aⅴ免费久久| 亚洲va中文字幕无码久久不卡 | 亚洲国产另类久久久精品黑人| 亚洲狠狠久久综合一区77777| 日本欧美久久久久免费播放网 | 996久久国产精品线观看| 久久久久久亚洲精品成人| 久久久久亚洲AV无码专区体验| 97精品国产97久久久久久免费| 人妻无码αv中文字幕久久 | 亚洲性久久久影院| 91精品国产综合久久香蕉| 久久综合香蕉国产蜜臀AV| 中文字幕久久精品无码| 久久热这里只有精品在线观看| 亚洲精品午夜国产va久久| 精品无码人妻久久久久久| 久久激情五月丁香伊人| 久久精品国产99久久丝袜|