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

            糯米

            TI DaVinci, gstreamer, ffmpeg
            隨筆 - 167, 文章 - 0, 評論 - 47, 引用 - 0
            數據加載中……

            POJ 2135 Farm Tour 最小費用最大流

            第一次寫這玩意,還真有點麻煩,出了點問題。然后看了一下別人的代碼,
            發現雙向邊,要分成兩個單向邊來插入。長見識了。
            而且費用的值有正有負,最好用SPFA來找最短路徑。

            思路:
            建立一個超級源點,跟點1連起來,容量為2。
            建立一個超級匯點,跟點N連起來,容量為2。
            其他的邊容量均為1。

            #include <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <string.h>

            #define MAX_COST (1 << 30)
            #define MAX_E (20032 * 4)
            #define MAX_V 2048
            #define MAX_CAP 2
            #define dbp printf

            struct edge_node {
                
            int idx, flow, cap, cost;
                
            struct edge_node *next;
            }
            ;

            struct graph_node {
                
            struct edge_node edges[MAX_E], *map[MAX_V], *path_edge[MAX_V];
                
            int edges_cnt, vertexs_cnt, path_idx[MAX_V];
                
            int min_dis[MAX_V];
            }
            ;

            inline 
            int min(int a, int b)
            {
                
            return a < b ? a : b;
            }


            inline 
            void graph_init(struct graph_node *g, int vertexs_cnt)
            {
                g
            ->vertexs_cnt = vertexs_cnt;
                g
            ->edges_cnt = 0;
                memset(g
            ->map, 0, vertexs_cnt * sizeof(g->map[0]));
            }


            inline 
            void graph_dump(struct graph_node *g)
            {
                
            struct edge_node *e;
                
            int i;

                
            for (i = 0; i < g->vertexs_cnt; i++{
                    dbp(
            "#%d: ", i);
                    
            for (e = g->map[i]; e; e = e->next) 
                        dbp(
            "%dc%d ", e->idx, e->cost);
                    dbp(
            "\n");
                }

            }


            inline 
            void graph_addedge(struct graph_node *g, 
                                      
            int from, int to, 
                                      
            int cost, int cap = 0
                                      )
            {
                
            struct edge_node *e;

                
            if (g->edges_cnt >= _countof(g->edges)) {
                    printf(
            "too many edges\n");
                    
            return ;
                }


                e 
            = &g->edges[g->edges_cnt++];
                e
            ->idx = to;
                e
            ->cost = cost;
                e
            ->cap = cap;
                e
            ->next = g->map[from];
                g
            ->map[from] = e;
            }


            inline 
            int __conn_default(struct edge_node *e)
            {
                
            return 1;
            }


            inline 
            void graph_spfa(struct graph_node *g, int idx, 
                                   
            int (*conn)(struct edge_node *= __conn_default
                                   )
            {
                
            static int queue[MAX_V], vis[MAX_V], tm, head, tail;
                
            int i, val;
                
            struct edge_node *e;

                
            for (i = 0; i < g->vertexs_cnt; i++)
                    g
            ->min_dis[i] = MAX_COST;
                g
            ->min_dis[idx] = 0;
                
                head 
            = tail = 0;
                tm
            ++;
                queue[tail
            ++= idx;

                
            while (head != tail) {
                    idx 
            = queue[head++];
                    vis[idx] 
            = 0;
                    
            for (e = g->map[idx]; e; e = e->next) {
                        
            if (!conn(e))
                            
            continue;
                        val 
            = g->min_dis[idx] + e->cost;
                        
            if (val >= g->min_dis[e->idx])
                            
            continue;
                        g
            ->path_idx[e->idx] = idx;
                        g
            ->path_edge[e->idx] = e;
                        g
            ->min_dis[e->idx] = val;
                        
            if (vis[e->idx] == tm) 
                            
            continue;
                        queue[tail
            ++= e->idx;
                        vis[e
            ->idx] = tm;
                    }

                }

            }


            inline 
            int __conn_mfmc(struct edge_node *e)
            {
                
            return e->cap - e->flow > 0;
            }


            inline 
            void graph_mfmc(struct graph_node *g, int s, int t, int *flow, int *cost)
            {
                
            int i, j, min_c;
                
            struct edge_node *e;

                
            for (i = 0; i < g->edges_cnt; i++)
                    g
            ->edges[i].flow = 0;

                
            *flow = 0;
                
            *cost = 0;
                
            while (1{
                    graph_spfa(g, s, __conn_mfmc);
                    
            if (g->min_dis[t] == MAX_COST)
                        
            break ;
                    min_c 
            = MAX_CAP;
                    
            for (i = t; i != s; i = g->path_idx[i]) {
                        e 
            = g->path_edge[i];
                        min_c 
            = min(min_c, e->cap - e->flow);
                    }

                    
            for (i = t; i != s; i = g->path_idx[i]) {
                        j 
            = g->path_edge[i] - g->edges;
                        
            *cost += g->edges[j].cost * min_c;
                        g
            ->edges[j].flow += min_c;
                        g
            ->edges[j ^ 1].flow -= min_c;
                    }

                    
            *flow += min_c;
                }

            }


            int main()
            {
                
            int N, M, from, to, cost, flow;
                
            static struct graph_node g;

                freopen(
            "e:\\test\\in.txt""r", stdin);

                scanf(
            "%d%d"&N, &M);
                graph_init(
            &g, N + 2);
                
            while (M--{
                    scanf(
            "%d%d%d"&from, &to, &cost);
                    graph_addedge(
            &g, from, to, cost, 1);
                    graph_addedge(
            &g, to, from, -cost, 0);
                    graph_addedge(
            &g, to, from, cost, 1);
                    graph_addedge(
            &g, from, to, -cost, 0);
                }

                graph_addedge(
            &g, 0102);
                graph_addedge(
            &g, 1000);
                graph_addedge(
            &g, N, N + 102);
                graph_addedge(
            &g, N + 1, N, 00);
                graph_mfmc(
            &g, 0, N + 1&flow, &cost);
                printf(
            "%d\n", cost);

                
            return 0;
            }

            posted on 2010-04-06 23:40 糯米 閱讀(788) 評論(4)  編輯 收藏 引用 所屬分類: POJ

            評論

            # re: POJ 2135 Farm Tour 最小費用最大流  回復  更多評論   

            "發現雙向邊,要分成兩個單向邊來插入。"
            請問一下為什么要建雙向邊呢?
            2010-09-02 00:18 | siurz

            # re: POJ 2135 Farm Tour 最小費用最大流  回復  更多評論   

            @siurz
            同樣期待
            2011-02-06 16:43 | .。

            # re: POJ 2135 Farm Tour 最小費用最大流  回復  更多評論   

            @.。
            想明白了,因為當a->b的邊走了之后,b->a有兩條邊,其中一條的費用為正,一條的費用為負。顯然會選擇為負的,而為負的正是剛剛其走過的a->b的反向邊,這樣就相當于剛剛的a->b沒有走。
            2011-02-06 16:58 | .。

            # re: POJ 2135 Farm Tour 最小費用最大流  回復  更多評論   

            如火熱
            2011-08-17 16:24 |
            人妻无码αv中文字幕久久 | 日韩精品无码久久一区二区三| 亚洲午夜精品久久久久久人妖| 久久亚洲精品视频| 人妻无码精品久久亚瑟影视| 久久久这里只有精品加勒比| 久久青青草原亚洲av无码app| 久久国产精品成人免费| 久久久久人妻精品一区三寸蜜桃| 久久久久久精品免费看SSS| 久久亚洲精品中文字幕| 久久se这里只有精品| 久久亚洲私人国产精品vA| 欧美亚洲国产精品久久蜜芽| 色青青草原桃花久久综合| 丁香狠狠色婷婷久久综合| 色8激情欧美成人久久综合电| 久久精品国产亚洲av麻豆小说| 久久精品国产精品亚洲人人| 久久99精品国产自在现线小黄鸭| 久久97久久97精品免视看| 久久99精品久久久久久久久久 | 亚洲国产成人久久综合碰| 久久精品国产亚洲77777| 伊人久久亚洲综合影院| 狠狠人妻久久久久久综合| 久久久精品国产sm调教网站| 久久精品国产久精国产一老狼| 久久久WWW成人免费精品| 久久精品国产免费| 亚洲AV日韩AV天堂久久| 久久经典免费视频| 久久久黄片| 久久久久久av无码免费看大片| 四虎国产精品免费久久5151 | 精品免费tv久久久久久久| 久久综合给久久狠狠97色| 7777精品久久久大香线蕉| 亚洲AV无码久久精品色欲| 丁香色欲久久久久久综合网| 久久亚洲精品成人无码网站|