• <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 閱讀(253) 評論(0)  編輯 收藏 引用 所屬分類: searchgraph

            <2011年2月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272812345
            6789101112

            導航

            統計

            公告

            統計系統

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            国内精品久久久久久久97牛牛| 色诱久久久久综合网ywww| 久久精品水蜜桃av综合天堂| 久久ww精品w免费人成| 久久99精品国产麻豆蜜芽| 亚洲国产婷婷香蕉久久久久久| 久久天天婷婷五月俺也去| 亚洲AV日韩AV天堂久久| 国产精品99精品久久免费| 久久国产香蕉一区精品| 无码AV波多野结衣久久| 国产精品伦理久久久久久| 亚洲∧v久久久无码精品| 国产香蕉97碰碰久久人人| 亚洲AV无码一区东京热久久| 国产日韩久久免费影院| 久久99精品久久久久久动态图| 久久久久久av无码免费看大片| 久久天天躁狠狠躁夜夜avapp| 久久久久国产精品嫩草影院| 久久夜色精品国产亚洲| 色欲综合久久躁天天躁蜜桃| 亚洲人成无码网站久久99热国产| 高清免费久久午夜精品| 一本久久a久久精品亚洲| 亚洲国产综合久久天堂| 国产99久久九九精品无码| 国产V综合V亚洲欧美久久| 亚洲精品乱码久久久久久按摩| 久久亚洲精品国产精品婷婷| 久久久久无码精品| 久久WWW免费人成—看片| 久久精品国产69国产精品亚洲| 久久精品亚洲一区二区三区浴池 | 无码人妻少妇久久中文字幕蜜桃| 久久夜色撩人精品国产| 久久久久亚洲av毛片大| 日本国产精品久久| 久久精品国产清自在天天线| 精品久久久无码中文字幕天天| 精品久久久久中文字|