• <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個點(diǎn),M個邊,每個邊有個標(biāo)號,定義一條路徑和為所有這條路徑上邊的GCD,求從1到2所有路徑的LCM

            解法:
            BFS,狀態(tài)修正下,用{未節(jié)點(diǎn),子路徑的GCD},這樣總狀態(tài)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 閱讀(255) 評論(0)  編輯 收藏 引用 所屬分類: searchgraph

            <2010年11月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導(dǎo)航

            統(tǒng)計

            公告

            統(tǒng)計系統(tǒng)

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久夜色撩人精品国产| 狠狠色综合网站久久久久久久高清| 精品久久久久久中文字幕人妻最新 | 无码精品久久久天天影视| 国产成人无码精品久久久性色| 亚洲中文字幕无码久久2017| 久久综合九色综合97_久久久| 久久久午夜精品福利内容| 国产亚洲婷婷香蕉久久精品| 久久综合亚洲鲁鲁五月天| 久久国产亚洲精品麻豆| 狠狠精品久久久无码中文字幕| 欧美伊香蕉久久综合类网站| 一本久久a久久精品亚洲| 国产午夜精品久久久久九九| 久久精品无码专区免费青青| 蜜桃麻豆www久久国产精品| 97久久超碰国产精品2021| 国产成人精品综合久久久| 久久性生大片免费观看性| 久久无码av三级| 999久久久无码国产精品| 色欲久久久天天天综合网精品| 亚洲七七久久精品中文国产 | 亚洲精品国产第一综合99久久| 天天久久狠狠色综合| 99久久99久久| 成人久久久观看免费毛片| 久久婷婷激情综合色综合俺也去| 国产精品久久新婚兰兰| 色妞色综合久久夜夜| 亚洲一区精品伊人久久伊人| 久久久久国产| 欧美久久一级内射wwwwww.| 久久精品无码av| 精品水蜜桃久久久久久久| 午夜不卡888久久| 99久久夜色精品国产网站| 色综合久久中文综合网| 久久亚洲AV永久无码精品| 思思久久好好热精品国产|