• <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>

            pku 2132 Cow Math BFS

            題意:
            N個點,M個邊,每個邊有個標號,定義一條路徑和為所有這條路徑上邊的GCD,求從1到2所有路徑的LCM

            解法:
            BFS,狀態修正下,用{未節點,子路徑的GCD},這樣總狀態50000,GCD的解法用歐幾里得,LCM=a*b/gcd(a,b),注意高精度,我偷懶用了java。。。

            代碼:
             1 import java.util.*;
             2 import java.math.*;
             3 public class Main {
             4 
             5     /**
             6      * @param args
             7      */
             8     static class pair
             9     {
            10         int a,b;
            11         pair(int aa,int bb)
            12         {
            13             a=aa;
            14             b=bb;
            15         }
            16     }
            17     static int gcd(int a,int b)
            18     {
            19         while(b!=0)
            20         {
            21             int t=a%b;
            22             a=b;
            23             b=t;
            24         }
            25         return a;
            26     }
            27     static BigInteger gcd(BigInteger a,BigInteger b)
            28     {
            29         while(!b.equals(BigInteger.ZERO))
            30         {
            31             BigInteger t=a.mod(b);
            32             a=b;
            33             b=t;
            34         }
            35         return a;
            36     }
            37     public static void main(String[] args) {
            38         Scanner in=new Scanner(System.in);
            39         int n=in.nextInt();
            40         int map[][]=new int[n][n];
            41         boolean used[][]=new boolean[26][2005];
            42         LinkedList<pair> q=new LinkedList<pair>();
            43         for(int i=0;i<n;i++)
            44             for(int j=0;j<n;j++)
            45                 map[i][j]=in.nextInt();
            46         BigInteger lcm=BigInteger.ONE;
            47         BigInteger tmp[]=new BigInteger[2005];
            48         tmp[1]=BigInteger.ONE;
            49         for(int i=2;i<tmp.length;i++)
            50             tmp[i]=tmp[i-1].add(BigInteger.ONE);
            51         for(int i=0;i<26;i++)
            52             Arrays.fill(used[i], false);
            53         for(int i=0;i<n;i++)
            54             if(map[0][i]!=0)
            55             {
            56                 used[i][map[0][i]]=used[0][map[0][i]]=true;
            57                 q.add(new pair(i,map[0][i]));
            58             }
            59         while(!q.isEmpty())
            60         {
            61             pair top=q.pollFirst();
            62             //System.out.println(top.a+" "+top.b);
            63             if(top.a==1)
            64             {
            65                 BigInteger t=tmp[top.b].multiply(lcm);
            66                 t=t.divide(gcd(lcm.add(BigInteger.ZERO),tmp[top.b].add(BigInteger.ZERO)));
            67                 lcm=t;
            68                 continue;
            69             }
            70             for(int i=0;i<n;i++)
            71                 if(map[top.a][i]!=0&&!used[i][gcd(top.b,map[top.a][i])])
            72                 {
            73                     used[i][gcd(top.b,map[top.a][i])]=true;
            74                     q.add(new pair(i,gcd(top.b,map[top.a][i])));
            75                 }
            76         }
            77         System.out.println(lcm);
            78 
            79     }
            80 
            81 }

            posted on 2011-03-13 02:23 yzhw 閱讀(248) 評論(0)  編輯 收藏 引用 所屬分類: searchgraph

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導航

            統計

            公告

            統計系統

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久无码AV一区二区三区| 久久免费国产精品| 国产精品久久影院| 久久成人国产精品一区二区| 久久久久久青草大香综合精品| 午夜精品久久久内射近拍高清| 亚洲精品无码久久久久| 亚洲精品高清久久| 久久久噜噜噜久久中文字幕色伊伊| 色88久久久久高潮综合影院 | 亚洲乱码日产精品a级毛片久久| 狠狠色婷婷久久综合频道日韩| 久久久久亚洲精品男人的天堂| 91精品国产色综久久| 国产—久久香蕉国产线看观看| 久久精品国产亚洲av瑜伽| 久久亚洲AV成人无码电影| 国产三级观看久久| 久久精品免费观看| 亚洲中文精品久久久久久不卡| 久久夜色精品国产www| 51久久夜色精品国产| 久久精品aⅴ无码中文字字幕重口| 伊人久久大香线蕉综合网站| 2020最新久久久视精品爱| 99精品久久精品一区二区| 亚洲午夜久久久久久久久久| 亚洲色欲久久久久综合网| 久久久久女教师免费一区| 国内精品伊人久久久久影院对白| 99国产欧美精品久久久蜜芽| 激情伊人五月天久久综合| 亚洲国产另类久久久精品黑人 | 久久精品国产亚洲AV蜜臀色欲 | 久久99精品久久久久子伦| 久久久久久久波多野结衣高潮| 亚洲国产成人精品91久久久 | 国产精品永久久久久久久久久| 日本精品久久久中文字幕| 99久久精品无码一区二区毛片| 91久久九九无码成人网站|