• <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)  編輯 收藏 引用 所屬分類: 圖論
            久久久久国产精品三级网| 无遮挡粉嫩小泬久久久久久久| 国产精品久久久久久久 | 久久精品亚洲欧美日韩久久| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 亚洲AV无一区二区三区久久| 久久久精品一区二区三区| 久久久久亚洲爆乳少妇无 | 久久精品欧美日韩精品| 国内精品久久久久久中文字幕| 久久夜色精品国产亚洲| 久久精品人人槡人妻人人玩AV| 久久综合九色综合久99| 99久久精品费精品国产一区二区| 一极黄色视频久久网站| 亚洲国产精品久久久久久| 久久婷婷五月综合成人D啪| 美女写真久久影院| 久久久女人与动物群交毛片| 性做久久久久久久久浪潮| 四虎国产永久免费久久| 亚洲人成伊人成综合网久久久| 久久久免费观成人影院 | 国产一区二区久久久| 婷婷综合久久中文字幕| 久久综合88熟人妻| 色偷偷久久一区二区三区| 一本久久综合亚洲鲁鲁五月天| 久久久久国产精品嫩草影院 | 久久久久99精品成人片试看| 精品伊人久久久| 久久人人爽人人爽人人片AV东京热| 久久国产精品免费| 久久久久一本毛久久久| 国产精品成人久久久久三级午夜电影 | 久久亚洲色一区二区三区| 丰满少妇人妻久久久久久4| 国产精品伦理久久久久久| 久久播电影网| 中文字幕久久亚洲一区| 国产精品99久久久精品无码 |