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

            A Za, A Za, Fighting...

            堅(jiān)信:勤能補(bǔ)拙

            PKU 3259 Wormholes

            問題:
            http://acm.pku.edu.cn/JudgeOnline/problem?id=3259

            思路:
            這題的描述挺有意思,通過某些路徑可以回到過去之類,其實(shí)就是求是否存在負(fù)權(quán)回路的問題
            Bellman-Ford算法的典型應(yīng)用
            一個(gè)問題是,Bellman-Ford用于判斷從某個(gè)源點(diǎn)可達(dá)的負(fù)權(quán)回路,而這里求的是整個(gè)圖,而且也沒有說明該圖一定是connected的
            解決上述問題的一個(gè)方法就是添加一個(gè)頂點(diǎn),然后從該新頂點(diǎn)到每個(gè)其他頂點(diǎn)添加一條權(quán)值為0的邊

            代碼:
             1 /* Bellman-Ford algorithm */
             2 #include<stdio.h>
             3 #include<stdlib.h>
             4 #include<string.h>
             5 #define MAX_N 501
             6 #define MAX_M 2501
             7 #define MAX_W 201
             8 #define INF 0x7FFFFFFF
             9 struct Edge {
            10     int from, to;
            11     int cost;
            12 } edges[MAX_M*2+MAX_W+MAX_N];
            13 int d[MAX_N];
            14 int n, total;
            15 
            16 void
            17 init()
            18 {
            19     int i, m, w, f, t, c;
            20     scanf("%d %d %d"&n, &m, &w);
            21     total = 0/* the number of edges */
            22     for(i=0; i<m; i++) {
            23         scanf("%d %d %d"&f, &t, &c);
            24         edges[total].from = f;
            25         edges[total].to = t;
            26         edges[total++].cost = c;
            27         edges[total].from = t;
            28         edges[total].to = f;
            29         edges[total++].cost = c;
            30     }
            31     for(i=0; i<w; i++) {
            32         scanf("%d %d %d"&f, &t, &c);
            33         edges[total].from = f;
            34         edges[total].to = t;
            35         edges[total++].cost = c*(-1);
            36     }
            37     /* in order to keep connectivity, add one vertex: '0' */
            38     for(i=1; i<=n; i++) {
            39         edges[total].from = 0;
            40         edges[total].to = i;
            41         edges[total++].cost = 0;
            42     }
            43 }
            44 
            45 void
            46 relax(struct Edge *e)
            47 {
            48     if(d[e->from] == INF)
            49         return;
            50     if(d[e->to] > d[e->from]+e->cost)
            51         d[e->to] = d[e->from]+e->cost;
            52 }
            53 
            54 int
            55 bellman_ford()
            56 {
            57     int i, j;
            58     for(i=0; i<=n; i++)
            59         d[i] = INF;
            60     d[0= 0;
            61     for(i=0; i<n; i++)  /* n+1 vertices */
            62         for(j=0; j<total; j++)  /* 2*m+w+n edges */
            63             relax(edges+j);
            64     for(j=0; j<total; j++) {
            65         if(d[edges[j].to] > d[edges[j].from]+edges[j].cost)
            66             return 0;
            67     }
            68     return 1;
            69 }
            70 
            71 int
            72 main(int argc, char **argv)
            73 {
            74     int F;
            75     scanf("%d"&F);
            76     while(F--) {
            77         init();
            78         printf("%s\n", bellman_ford()==1?"NO":"YES");
            79     }
            80 }

            posted on 2010-09-07 22:29 simplyzhao 閱讀(299) 評(píng)論(0)  編輯 收藏 引用 所屬分類: F_圖算法

            導(dǎo)航

            <2011年8月>
            31123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            99久久www免费人成精品| 99久久精品九九亚洲精品| 久久亚洲高清综合| 久久久WWW成人免费毛片| 久久久久亚洲AV综合波多野结衣| 精品久久久无码中文字幕| 韩国三级中文字幕hd久久精品 | 国产精品久久波多野结衣| 久久大香香蕉国产| 久久精品国产精品亜洲毛片| 欧美久久亚洲精品| 少妇久久久久久久久久| 精品免费久久久久国产一区| 久久精品国产亚洲AV香蕉| 91精品国产色综久久 | 久久亚洲精品国产精品婷婷| 久久综合久久美利坚合众国| 久久久久亚洲精品无码蜜桃| 久久精品国产精品亜洲毛片| 无码精品久久久久久人妻中字| 久久久久久久尹人综合网亚洲 | 国产午夜免费高清久久影院| 日日狠狠久久偷偷色综合0| 久久久女人与动物群交毛片| 日本加勒比久久精品| 久久99热狠狠色精品一区| 麻豆av久久av盛宴av| 久久久久噜噜噜亚洲熟女综合| 久久精品无码专区免费东京热| 一级女性全黄久久生活片免费| 国产精品对白刺激久久久| 久久午夜无码鲁丝片秋霞| 欧美日韩中文字幕久久久不卡| 伊人久久综在合线亚洲2019 | 亚洲AV日韩精品久久久久久久| 欧美粉嫩小泬久久久久久久| 91精品无码久久久久久五月天| 精品无码久久久久国产| 日韩乱码人妻无码中文字幕久久| 久久国内免费视频| 中文精品99久久国产|