• <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>
            posts - 7,comments - 3,trackbacks - 0

             1871: Jogging Trails


            ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
            3s8192K5917Standard
            Gord is training for a marathon. Behind his house is a park with a large network of jogging trails connecting water stations. Gord wants to find the shortest jogging route that travels along every trail at least once.

            Input consists of several test cases. The first line of input for each case contains two positive integers: n <= 15, the number of water stations, and m < 1000, the number of trails. For each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 and n, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. There may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Gord's route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case.

            For each case, there should be one line of output giving the length of Gord's jogging route.

            Sample Input

            4 5
            1 2 3
            2 3 4
            3 4 5
            1 4 10
            1 3 12
            0
            

            Output for Sample Input

            41
            





            古老的圖論問(wèn)題:中國(guó)郵遞員問(wèn)題,曾經(jīng)在某個(gè)圖論書(shū)上看過(guò),之后就沒(méi)有之后了......
            題目大意很簡(jiǎn)單:給一無(wú)向網(wǎng)絡(luò)N,從點(diǎn)1出發(fā),每條邊至少進(jìn)過(guò)一次后回到點(diǎn)1。
            思路:(copy別人的)
            1. 先解釋下"度"的概念, 對(duì)于無(wú)向圖中某結(jié)點(diǎn), 它和n條邊相連, 就稱它的度為n. (有向圖還分出度入度, 這里簡(jiǎn)化了, 不管)

            2. 參考?xì)W拉回路的概念, 無(wú)向圖存在歐拉回路, 當(dāng)前僅當(dāng)所有點(diǎn)度數(shù)均為偶數(shù). 
            證明比較簡(jiǎn)單, 因?yàn)樽咄暌粭l回路, 對(duì)于所有點(diǎn)均進(jìn)去一次, 出來(lái)一次. 故, 對(duì)于任意點(diǎn)的度數(shù),都是成對(duì)的在"消耗".

            3. 題中所描述的回路, 有重復(fù)經(jīng)過(guò)某邊, 這沒(méi)關(guān)系. 現(xiàn)在假設(shè)郵遞員按題目要求走了一條最短的回路P.
            那么, 把P所有重復(fù)經(jīng)過(guò)的邊, 拆開(kāi). 即假設(shè)三次經(jīng)過(guò)邊L, 則我們可以人為的在原圖上多加兩條邊, 對(duì)于所有重復(fù)經(jīng)過(guò)的邊都這樣做. 修改后的圖記為G2. (原圖為G)
            對(duì)于G2, 郵遞員走的回路, 即為此圖的歐拉回路. 這是重點(diǎn). (其實(shí)很好想, 因?yàn)镚2中的每條邊都僅被郵遞員走過(guò)一次.)

            4. 原問(wèn)題就可以轉(zhuǎn)化為, 如何把G添加一些邊, 變成G2
            而G2所有點(diǎn)度數(shù)均為偶數(shù)(這是肯定的, 因?yàn)镚2存在歐拉回路)
            故轉(zhuǎn)化為, 如何把G中某些邊復(fù)制x次, 從而消滅G中的奇點(diǎn)(度數(shù)為奇數(shù)的點(diǎn)).

            5. 可能有點(diǎn)暈, 舉個(gè)例子
            圖G只有兩個(gè)點(diǎn)a和b, 一條邊連著ab, 那么ab均為奇點(diǎn), 要消滅掉, 就把a(bǔ)b之間連線復(fù)制一次(因?yàn)槟悴豢赡茈S便給它加一條不存在的..)
            然后變成兩條邊連著ab. 從G2的角度看, 是歐拉圖了, 對(duì)應(yīng)成G, 就是把這條邊走了兩次而已.

            6. 奇點(diǎn)總是成對(duì)的被消滅, 就算兩個(gè)奇點(diǎn)不互通, 而是經(jīng)過(guò)很多點(diǎn)才能連通, 那也要連出一條路徑來(lái), 以消滅這對(duì)奇點(diǎn).

            7. 那就好辦了.把所有奇點(diǎn)找出來(lái), 然后配對(duì), 如果某種配對(duì)方式代價(jià)最小, 即為所求.
            這里還要用上floyd, 求最短路

            8. 配對(duì)算法也要比較好, 裸著配大概會(huì)超時(shí), 這個(gè)懶得說(shuō)了.. 如果不會(huì), 直接看代碼吧.

            給點(diǎn)扼要注釋, deg記錄度數(shù), mp記錄最短路. u記錄遞歸時(shí)各點(diǎn)是否已配對(duì). (偶點(diǎn)直接被記為已配對(duì). 遞歸時(shí)只路過(guò))
            dfs返回在當(dāng)前此種u記錄的配對(duì)情況下, 把未配對(duì)的匹配完, 最小代價(jià)是多少

            還是提一句吧, 每次配對(duì)時(shí), 配對(duì)中的第一個(gè)可以直接取隊(duì)列中的第一個(gè)沒(méi)有配對(duì)的元素(其實(shí)隨便取都行). 第二個(gè), 循環(huán)一次剩下的即可.
            因?yàn)榕鋵?duì)是不關(guān)心順序的, 所以第一個(gè)可以想怎么取都行. 為了方便, 就第一個(gè)了.

            代碼:
            #include <cstdio>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <iostream>
            #define N 17
            #define inf 1 << 20
            using namespace std;

            int deg[N], map[N][N], u[N];
            int ans, n, m;

            int min(int a, int b)
            {
                
            if (a > b) return b;
                
            return a;
            }

            int slove()
            {
                
            int i, j, rnt = inf;
                
            for (i = 0; i < n; ++i)
                
            if (!u[i]) break;
                
            if (i == n) return 0;
                u[i] 
            = 1;
                
            for (j = i + 1; j < n; ++j)
                {
                    
            if (!u[j]) u[j] = 1, rnt = min(rnt, slove() + map[i][j]), u[j] = 0;
                }
                u[i] 
            = 0;
                
            return rnt;
            }

            int main()
            {
                
            while (scanf("%d"&n) && n)
                {
                    memset(u, 
            0sizeof(u));
                    memset(deg, 
            0sizeof(deg));
                    
            for (int i = 0; i < n; ++i)
                        
            for (int j = 0; j < n; ++j)
                        map[i][j] 
            = inf;
                    ans 
            = 0;
                    scanf(
            "%d"&m);
                    
            for (int i = 1; i <= m; ++i)
                    {
                        
            int a, b, c;
                        scanf(
            "%d%d%d"&a, &b, &c);
                        ans 
            += c;
                        a
            --; b--;
                        deg[a]
            ++;
                        deg[b]
            ++;
                        map[a][b] 
            = map[b][a] = min(map[a][b], c);
                    }
                    
            for (int k = 0; k < n; ++k)
                        
            for (int i = 0; i < n; ++i)
                            
            for (int j = 0; j < n; ++j)
                            {
                                map[i][j] 
            = min(map[i][j], map[i][k] + map[k][j]);
                            }
                    
            for (int i = 0; i < n; ++i)
                    
            if (deg[i] % 2 == 0) u[i] = 1;
                    printf(
            "%d\n", ans + slove());
                }
                
            return 0;
            }
            posted on 2011-10-15 22:13 LLawliet 閱讀(251) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 圖論
            99久久国产亚洲综合精品| 国产精品久久久久久久app| 久久久无码一区二区三区| 亚洲国产精品一区二区久久hs| 亚洲国产精品无码久久久不卡| 天天爽天天狠久久久综合麻豆 | 久久精品国产亚洲麻豆| 91亚洲国产成人久久精品网址| 青青久久精品国产免费看| 久久人人爽人人爽人人av东京热| 久久综合亚洲欧美成人| 久久久久久无码国产精品中文字幕| 国产精品一区二区久久精品涩爱| 久久91综合国产91久久精品| 久久精品国产久精国产一老狼| 久久精品中文无码资源站| 久久这里有精品视频| 国产欧美久久一区二区| 久久伊人精品一区二区三区| 爱做久久久久久| 97久久天天综合色天天综合色hd| 模特私拍国产精品久久| 久久精品国产亚洲5555| 久久精品中文騷妇女内射| 久久亚洲精品成人无码网站| 久久国产精品无码网站| 日本三级久久网| 国产V综合V亚洲欧美久久| 久久九九久精品国产免费直播| 久久久久亚洲爆乳少妇无| 久久亚洲综合色一区二区三区| 人妻无码中文久久久久专区| 久久天天躁狠狠躁夜夜avapp| 久久亚洲国产成人影院网站| 国产免费久久精品99久久| 91精品国产色综久久| 免费精品99久久国产综合精品| 精品久久久久久中文字幕| 精品久久久久久国产| 久久精品国内一区二区三区| 久久精品国产影库免费看 |