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

            poj1860 Currency Exchange: spfa / Bellman Ford

            1) 實質(zhì)就是是確定圖中是否存在正環(huán)--沿著這個環(huán)一直走能夠使環(huán)內(nèi)節(jié)點的權(quán)值無限增長.
            2) 特點是: 每個邊的權(quán)值是動態(tài)變化的, 這點提醒我們適合用Bellman Ford算法來處理(SPFA實質(zhì)上是Bellman Ford的優(yōu)化, 算法思想是一樣的).

            SPFA算法:
             1#include <cstdio>
             2#include <queue>
             3using namespace std;
             4
             5#define EXPNTNUM (100 + 10)
             6#define CURNUM (100 + 10)
             7
             8struct Node {
             9    int to;
            10    Node* next;
            11    double commission, ratio;
            12}
            ;
            13
            14Node nodeHead[CURNUM + 1];
            15Node edge[EXPNTNUM * 2 + 2];
            16int pos = 0;
            17Node* getEdge() {
            18    return edge + pos++;
            19}

            20
            21double maxWeight[CURNUM + 1];
            22
            23void addEdge(int from, int to, double ratio, double commission) {
            24    Node* e = getEdge();
            25    Node* n = nodeHead + from;
            26    e->to = to;
            27    e->ratio = ratio;
            28    e->commission = commission;
            29    e->next = n->next;
            30    n->next = e;
            31}

            32
            33int main() {
            34    int currencyNum, pointNum, currencyNumHas;
            35    int i, j, a, b;
            36    double ratioab, ratioba, comab, comba;
            37    double currencisHas;
            38
            39    scanf("%d%d%d%lf"&currencyNum, &pointNum, &currencyNumHas, &currencisHas);
            40    for (i = 1; i <= currencyNum; ++i) {
            41        maxWeight[i] = 0;
            42        nodeHead[i].next = NULL;
            43    }

            44    for (i = j = 0; i < pointNum; ++i) {
            45        scanf("%d%d"&a, &b);
            46        scanf("%lf%lf%lf%lf"&ratioab, &comab, &ratioba, &comba);
            47        addEdge(a, b, ratioab, comab);
            48        addEdge(b, a, ratioba, comba);
            49    }

            50
            51    maxWeight[currencyNumHas] = currencisHas;
            52
            53    queue<int> q;
            54    q.push(currencyNumHas);
            55    int maxEdgeCount = 0;
            56    while (true{
            57        int u = q.front();
            58        q.pop();
            59        for (Node* tra = nodeHead[u].next; tra != NULL; tra = tra->next) {
            60            double temp = (maxWeight[u] - tra->commission) * 
            61                tra->ratio;
            62            if (maxWeight[tra->to] < temp) {
            63                maxWeight[tra->to] = temp;
            64                q.push(tra->to);
            65            }

            66        }

            67        maxEdgeCount++;
            68        if (maxWeight[currencyNumHas] > currencisHas) {
            69            printf("YES\n");
            70            break;
            71        }

            72        if (q.empty()) {
            73            printf("NO\n");
            74            break;
            75        }

            76    }

            77
            78
            79    return 0;
            80}

            81


            Bellman Ford算法:

             1#include <cstdio>
             2using namespace std;
             3
             4#define EXPNTNUM (100 + 10)
             5#define CURNUM (100 + 10)
             6
             7struct Edge {
             8    int from, to;
             9    double commission, ratio;
            10}
            ;
            11
            12Edge edge[EXPNTNUM * 2 + 2];
            13
            14double maxWeight[CURNUM + 1];
            15
            16void setEdge(Edge &e, int from, int to, double ratio, double commission) {
            17    e.from = from;
            18    e.to = to;
            19    e.ratio = ratio;
            20    e.commission = commission;
            21}

            22
            23int main() {
            24    int currencyNum, pointNum, currencyNumHas;
            25    int i, j, a, b;
            26    double ratioab, ratioba, comab, comba;
            27    double currencisHas;
            28
            29    scanf("%d%d%d%lf"&currencyNum, &pointNum, &currencyNumHas, &currencisHas);
            30    for (i = j = 0; i < pointNum; ++i) {
            31        scanf("%d%d"&a, &b);
            32        scanf("%lf%lf%lf%lf"&ratioab, &comab, &ratioba, &comba);
            33        setEdge(edge[j++], a, b, ratioab, comab);
            34        setEdge(edge[j++], b, a, ratioba, comba);
            35    }

            36
            37    int edgeCount = j;
            38    for (i = 1; i <= currencyNum; ++i) {
            39        maxWeight[i] = 0;
            40    }

            41    maxWeight[currencyNumHas] = currencisHas;
            42
            43    int maxEdgeCount = 0;
            44    bool improved;
            45    while (true{
            46        improved = false;
            47        for (i = 0; i < edgeCount; ++i) {
            48            if (maxWeight[edge[i].from] <= 0{
            49                continue;
            50            }

            51            double temp = (maxWeight[edge[i].from] - edge[i].commission) * 
            52                edge[i].ratio;
            53            if (maxWeight[edge[i].to] < temp) {
            54                maxWeight[edge[i].to] = temp;
            55                improved = true;
            56            }

            57        }

            58        maxEdgeCount++;
            59        if (maxWeight[currencyNumHas] > currencisHas) {
            60            printf("YES\n");
            61            break;
            62        }

            63        if (!improved) {
            64            printf("NO\n");
            65            break;
            66        }

                          // 1) 在提交時不要這個也不會很慢
                          // 2) 下面代碼的意思是: 在已經(jīng)插入了currencyNum(節(jié)點數(shù))個邊的前提下, 
                          // 如果圖中不存在正回路(這個正回路會使回路內(nèi)的節(jié)點權(quán)值無限增加, 
                          // 從而能夠通過兌換貨幣的形式賺錢), 
                          // 則再插入邊不會使任意一個節(jié)點的權(quán)值增加. 反過來, 
                          // 如果已經(jīng)插入了currencyNum條邊, 還能插入邊使節(jié)點權(quán)值增加, 
                          // 則圖中存在正回路.
                          // 3) 這個終止條件可以使外層while循環(huán)至多在currencyNum次后結(jié)束. 
                          // 以避免回路權(quán)值增長很慢導致循環(huán)很多次的極端情況.
            67        if (maxEdgeCount > currencyNum && improved) {
            68            printf("YES\n");
            69            break;
            70        }

            71    }

            72
            73    return 0;
            74}

            75
            76


            posted on 2011-07-04 09:18 cucumber 閱讀(338) 評論(0)  編輯 收藏 引用

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

            導航

            統(tǒng)計

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久99精品久久久大学生| 伊人久久综合精品无码AV专区| 秋霞久久国产精品电影院| 久久久久久青草大香综合精品| 久久亚洲AV无码西西人体| 三级三级久久三级久久| 欧美一区二区精品久久| 99久久精品免费看国产一区二区三区 | 亚洲精品美女久久777777| 72种姿势欧美久久久久大黄蕉| 国产成人久久精品二区三区| 伊人久久大香线蕉av不卡| 国产精品无码久久四虎| 国产情侣久久久久aⅴ免费| 亚洲欧美国产精品专区久久| 麻豆精品久久精品色综合| 久久AV高潮AV无码AV| 国产免费久久久久久无码| 2021久久精品国产99国产精品| 亚洲国产精品综合久久网络| 成人国内精品久久久久影院VR| 日韩AV无码久久一区二区| 伊人久久无码精品中文字幕| 久久精品视频91| 四虎国产精品免费久久久| 69久久精品无码一区二区| avtt天堂网久久精品| 久久久久亚洲精品日久生情| 亚洲欧洲中文日韩久久AV乱码| 国产午夜电影久久| 国产精品99久久久久久www| 久久这里只精品国产99热| 国产精品久久精品| 久久久久99精品成人片直播| 久久精品黄AA片一区二区三区| 一级做a爰片久久毛片免费陪 | 久久久久亚洲AV成人网人人网站 | 色综合合久久天天给综看| 久久午夜无码鲁丝片午夜精品| 久久精品国产亚洲一区二区三区| 久久亚洲精品中文字幕三区|