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

            pku 1237 The Postal Worker Rings Once 中國郵路問題簡化版。紀念POJ第500題

            題意:
            Given a sequence of streets (connecting given intersections) you are to write a program that determines the minimal cost tour that traverses every street at least once. The tour must begin and end at the same intersection. 
            給力條件:
            There will be at most two intersections with odd degree
            解法:
            分情況討論:
            1、所有節(jié)點的度都為偶數(shù),那么答案就是所有邊權(quán)和
            2、存在兩個奇數(shù)度節(jié)點,那么答案是邊權(quán)和加上這兩個奇數(shù)度節(jié)點間的最短距離

            發(fā)現(xiàn)現(xiàn)在越來越依賴STL了,有點小悲劇。。。

            代碼:
             1 # include <cstdio>
             2 # include <vector>
             3 # include <utility>
             4 # include <functional>
             5 # include <cstring>
             6 using namespace std;
             7 vector<pair<int,int> >g[26];
             8 int main()
             9 {
            10     char str[1024];
            11     while(scanf("%s",str)!=EOF)
            12     {
            13        int total=0;
            14        for(int i=0;i<26;i++) g[i].clear();
            15        while(true)
            16        {
            17           if(!strcmp(str,"deadend")) break;
            18           g[str[0]-'a'].push_back(make_pair<int,int>(str[strlen(str)-1]-'a',strlen(str)));
            19           g[str[strlen(str)-1]-'a'].push_back(make_pair<int,int>(str[0]-'a',strlen(str)));
            20           total+=strlen(str);
            21           scanf("%s",str);
            22        }
            23        vector<int> flag;
            24        for(int i=0;i<26;i++)
            25        {
            26           if(g[i].size()%2==1)
            27             flag.push_back(i);
            28        }
            29        if(flag.empty()) printf("%d\n",total);
            30        else
            31        {
            32            int len[26];
            33            bool used[26];
            34            memset(len,-1,sizeof(len));
            35            memset(used,false,sizeof(used));
            36            len[flag[0]]=0;
            37            while(true)
            38            {
            39              int pos=-1,minnum=0xfffffff;
            40              for(int i=0;i<26;i++)
            41                if(!used[i]&&len[i]!=-1&&len[i]<minnum)
            42                  minnum=len[i],pos=i;
            43              if(pos==-1break;
            44              used[pos]=true;
            45              for(int i=0;i<g[pos].size();i++)
            46                if(!used[g[pos][i].first]&&(len[g[pos][i].first]==-1||len[g[pos][i].first]>len[pos]+g[pos][i].second))
            47                   len[g[pos][i].first]=len[pos]+g[pos][i].second;
            48            }
            49            printf("%d\n",total+len[flag[1]]);
            50        } 
            51     }
            52     return  0;
            53 }
            54 

            posted on 2011-01-26 11:59 yzhw 閱讀(348) 評論(0)  編輯 收藏 引用 所屬分類: graph

            <2011年1月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導航

            統(tǒng)計

            公告

            統(tǒng)計系統(tǒng)

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            无码人妻久久一区二区三区免费 | 伊人久久综在合线亚洲2019| 精品国际久久久久999波多野| 久久不射电影网| 久久精品国产只有精品66| 久久中文字幕视频、最近更新| 蜜桃麻豆WWW久久囤产精品| 国内精品九九久久久精品| 久久无码人妻精品一区二区三区| 午夜天堂av天堂久久久| 久久国产V一级毛多内射| 久久人人爽人人爽人人AV | 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 精品久久久久久久无码| 久久只这里是精品66| 精品久久久久久久| 奇米综合四色77777久久| 久久久青草青青国产亚洲免观| 久久精品国产亚洲av日韩| 婷婷久久综合九色综合绿巨人| 2021精品国产综合久久| 伊人久久大香线蕉av不变影院 | 久久久久久国产a免费观看不卡 | 久久久国产精华液| 26uuu久久五月天| 欧美精品一区二区精品久久| 久久久久亚洲AV成人片| 国内精品人妻无码久久久影院导航| 婷婷久久综合| 香蕉99久久国产综合精品宅男自 | 99久久精品国产一区二区| 久久青草国产精品一区| 国产精品岛国久久久久| 国产精品久久影院| 久久精品亚洲一区二区三区浴池| 久久国产色av免费看| 狠狠色婷婷久久综合频道日韩| 久久久久久国产精品美女| 久久国产欧美日韩精品| 久久亚洲中文字幕精品有坂深雪 | 久久久久人妻一区二区三区|