• <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 糯米 閱讀(810) 評論(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 |
            青草国产精品久久久久久| 久久综合色老色| 99久久人妻无码精品系列蜜桃 | 狠狠精品久久久无码中文字幕| 日产精品久久久一区二区| 精品久久久久香蕉网| 久久国产精品波多野结衣AV| 久久亚洲AV无码精品色午夜麻豆| 72种姿势欧美久久久久大黄蕉| 久久毛片一区二区| 99久久超碰中文字幕伊人| 日韩亚洲国产综合久久久| 久久最近最新中文字幕大全| 成人久久综合网| 青青草原综合久久大伊人| 91久久精品电影| 精品久久人人做人人爽综合| 国内精品久久久久伊人av| 色天使久久综合网天天| 亚洲国产精品久久久久婷婷老年| 亚洲精品乱码久久久久久蜜桃图片| 久久久黄色大片| 国产精品综合久久第一页| 亚洲精品美女久久久久99小说| 理论片午午伦夜理片久久 | 999久久久免费国产精品播放| 久久久久久久久久久久久久| 久久久久久久精品成人热色戒| 国产精品成人无码久久久久久| 国内精品久久国产大陆| 青青青国产成人久久111网站| 欧洲精品久久久av无码电影| 亚洲国产欧洲综合997久久| 99蜜桃臀久久久欧美精品网站| 久久精品aⅴ无码中文字字幕重口| 狠狠色丁香久久婷婷综| 久久婷婷国产综合精品| 久久久无码一区二区三区| 三上悠亚久久精品| 国产V综合V亚洲欧美久久| 国产成人精品久久一区二区三区 |