• <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 閱讀(348) 評論(0)  編輯 收藏 引用 所屬分類: graph

            <2010年11月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導航

            統計

            公告

            統計系統

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久久久久国产精品美女| 国产精品久久亚洲不卡动漫| 久久精品国产欧美日韩| 色噜噜狠狠先锋影音久久| 精品国产综合区久久久久久| 国内精品久久久久久不卡影院| 无码人妻少妇久久中文字幕| 男女久久久国产一区二区三区| 久久夜色精品国产亚洲| 午夜精品久久影院蜜桃| 亚洲AV无码久久精品色欲| 色综合久久最新中文字幕| 亚洲欧美国产日韩综合久久| 国内精品人妻无码久久久影院| 久久久久九九精品影院| 国产情侣久久久久aⅴ免费| 欧美日韩中文字幕久久久不卡| 国产成人精品免费久久久久| 色偷偷88欧美精品久久久| 九九久久99综合一区二区| 久久妇女高潮几次MBA| 成人亚洲欧美久久久久| 中文无码久久精品| 久久久受www免费人成| 久久国产精品99精品国产987| 囯产精品久久久久久久久蜜桃| 久久久久久一区国产精品| 久久国产精品-国产精品| 丁香色欲久久久久久综合网| 久久频这里精品99香蕉久| 久久激情亚洲精品无码?V| 国产精品久久久久久一区二区三区 | 国内精品久久久久久麻豆| 久久天天躁狠狠躁夜夜网站| 久久久久久国产a免费观看黄色大片 | 东方aⅴ免费观看久久av| 亚洲精品午夜国产va久久 | 久久久久99精品成人片| 99精品久久久久久久婷婷| 久久96国产精品久久久| 国产欧美一区二区久久|