• <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、所有節點的度都為偶數,那么答案就是所有邊權和
            2、存在兩個奇數度節點,那么答案是邊權和加上這兩個奇數度節點間的最短距離

            發現現在越來越依賴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 閱讀(341) 評論(0)  編輯 收藏 引用 所屬分類: graph

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

            導航

            統計

            公告

            統計系統

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久久久一区二区三区| 国产欧美久久一区二区| 久久这里只精品国产99热| 久久久人妻精品无码一区| 99热成人精品免费久久| 青青草国产精品久久| 日本欧美国产精品第一页久久| 欧美久久久久久午夜精品| 亚洲中文字幕伊人久久无码| 久久久av波多野一区二区| 国产成人久久久精品二区三区| 久久婷婷色综合一区二区| 久久棈精品久久久久久噜噜| 日本久久久精品中文字幕| 97久久国产露脸精品国产| 99久久国产亚洲高清观看2024| 久久人人爽人人爽人人av东京热| 久久精品麻豆日日躁夜夜躁| 伊人久久大香线蕉综合网站| 亚洲国产精品热久久| 欧美丰满熟妇BBB久久久| 久久久久国产一区二区| 国产亚洲精品久久久久秋霞| 久久免费视频一区| 99久久国产热无码精品免费久久久久| 日韩久久久久久中文人妻| 久久久久久久精品成人热色戒| 国产精品激情综合久久| 成人国内精品久久久久一区| 精品伊人久久大线蕉色首页| 一本久久综合亚洲鲁鲁五月天| 久久99亚洲综合精品首页| 久久综合中文字幕| 91精品国产91久久久久久| 久久电影网2021| 久久99国产精品久久99果冻传媒| 久久综合精品国产二区无码| 日产精品久久久一区二区| 久久亚洲精品成人AV| 九九久久自然熟的香蕉图片| 亚洲国产精品一区二区久久hs|