• <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 糯米 閱讀(793) 評論(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 |
            亚洲欧美成人久久综合中文网| 久久久青草青青国产亚洲免观| 性欧美丰满熟妇XXXX性久久久| 精品国产日韩久久亚洲| 偷窥少妇久久久久久久久| yy6080久久| 色综合久久久久| 久久亚洲中文字幕精品一区| 日本久久久久亚洲中字幕 | 精品综合久久久久久97超人| 国产2021久久精品| 亚洲国产精品无码久久| 一本伊大人香蕉久久网手机| 香蕉久久夜色精品升级完成| 国产高清美女一级a毛片久久w| 久久精品视频一| 久久国产成人午夜aⅴ影院| 久久人人爽人人爽人人片av高请| 国产AⅤ精品一区二区三区久久 | 国产AⅤ精品一区二区三区久久 | 婷婷综合久久狠狠色99h| 久久这里都是精品| 国产香蕉97碰碰久久人人| 久久久久久无码Av成人影院| 日日狠狠久久偷偷色综合96蜜桃 | 伊人久久综在合线亚洲2019| 中文无码久久精品| 久久精品综合一区二区三区| 久久久久国产精品| 国产精品青草久久久久婷婷| 久久青青草原精品国产| 久久精品国产99久久久| 色欲综合久久中文字幕网| 久久人人爽人人爽人人爽| 久久影院久久香蕉国产线看观看| 伊人久久免费视频| 久久国产V一级毛多内射| 久久久免费观成人影院| 综合久久给合久久狠狠狠97色 | 久久久久久久综合日本亚洲| 久久99久久99精品免视看动漫|