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

            Why so serious? --[NKU]schindlerlee

            2010-02-04.ural1063-pku1756 graph theory and IDFS 此題在pku上42人AC

            2010-02-04.ural1063-pku1756
            ALGO:graph theory and IDFS
            題目中給給出了m(1 ≤ m ≤ 100)條無向邊,點(diǎn)是1-6,意思就是在這6個(gè)點(diǎn)上添加一些邊組成歐拉回路,
            并且要求點(diǎn)的和最小

            歐拉路存在的條件:奇度數(shù)頂點(diǎn)的個(gè)數(shù)為0或2且圖是連通的。
            我一開始的想法是:
            最多只有6個(gè)點(diǎn),完全圖的邊的數(shù)量不過5+4+3+2+1=15條,我就想枚舉這些邊的組合。
            一共 2^15 種可能才 32 × 1024種可能。看了樣例才發(fā)現(xiàn)可以添加重邊。。。

            然后就只能搜了,怎么搜呢,迭代加深搜最好,但是要注意,有可能多添一條邊,但是花費(fèi)卻變少了。
            然后就是最壞的情況也只能添加5條邊
            6
            1 1
            2 2
            3 3
            4 4
            5 5
            6 6

            然后就是寫代碼了,題目還是比較不好寫正確的
                1756    Domino Puzzle    42
            pku只有42人過了。。

              1 
              2 const int N = 8;
              3 const int M = 512;
              4 int edge[M][2],m,top;
              5 int vis[N],g[N][N],deg[N];
              6 int st[N],n; //the node exists in graph
              7 int exist[N],res;
              8 int save[M][2],sp;
              9 //http://www.shnenglu.com/schindlerlee/
             10 void dfs2(int u)
             11 {
             12   vis[u] = 1;
             13   for (int i = 1;i <= 6;i++) {
             14       if (g[u][i] && !vis[i]) {
             15           dfs2(i);
             16       }
             17   }
             18 }
             19 
             20 bool connected()
             21 {
             22   memset(vis,0,sizeof(vis));
             23   dfs2(st[0]);
             24   for (int i = 0;i < n;i++) {
             25       if (!vis[st[i]]) {
             26           return false;
             27       }
             28   }
             29   return true;
             30 }
             31 
             32 int goaldepth;
             33 bool dfs(int depth,int curres,int begi,int begj)
             34      //加上begi和begj能從980ms -> 16ms
             35 {
             36   int i,j;
             37   if (depth == goaldepth) {
             38       int cnt = 0;
             39       for (i = 1;i <= 6;i++) {
             40           if (deg[i] & 1) {
             41               cnt++;
             42           }
             43       }
             44       if (curres < res && (cnt == 0 || cnt == 2&& connected()) {
             45           res = curres;
             46           sp = top - m;
             47           for (i = 0;i < sp;i++) {
             48               save[i][0= edge[i+m][0];
             49               save[i][1= edge[i+m][1];
             50           }
             51           return true;
             52       }
             53       return false;
             54   }
             55   for (i = begi;i < n;i++) {
             56       for (j = begj;j < n;j++) {
             57           if (i != j) {
             58               int a = st[i],b = st[j];
             59               if (curres + a + b >= res) { continue; }
             60               g[a][b]++;
             61               g[b][a]++;
             62               deg[a]++;
             63               deg[b]++;
             64               edge[top][0]=a, edge[top][1]=b,top++;
             65               dfs(depth+1,curres + a + b,i,j);
             66               top--;
             67               g[a][b]--;
             68               g[b][a]--;
             69               deg[a]--;
             70               deg[b]--;
             71           }
             72       }
             73   }
             74 }
             75 
             76 int main()
             77 {
             78   int i,j,k;
             79   scanf("%d",&m);
             80   for (i = 0;i < m;i++) {
             81       int a,b;
             82       scanf("%d%d",&a,&b);
             83       edge[i][0= a;
             84       edge[i][1= b;
             85       exist[a] = 1;
             86       exist[b] = 1;
             87       g[a][b] ++;
             88       g[b][a] ++;
             89       deg[a]++;
             90       deg[b]++;
             91   }
             92   for (i = 1;i <= 6;i++) {
             93       if (exist[i]) {
             94           st[n++= i;
             95       }
             96   }
             97 
             98   res = maxint;
             99   for (i = 0;i <= 5;i++) {
            100       top = m;
            101       goaldepth = i;
            102       dfs(0,0,0,0);
            103   }
            104 
            105   printf("%d\n",res);
            106   printf("%d\n",sp);
            107   for (i = 0;i < sp;i++) {
            108       printf("%d %d\n",save[i][0],save[i][1]);
            109   }
            110 
            111   return 0;
            112 }
            113 


            posted on 2010-02-04 14:42 schindlerlee 閱讀(1101) 評論(0)  編輯 收藏 引用 所屬分類: 解題報(bào)告

            狠狠久久亚洲欧美专区| 亚洲中文字幕久久精品无码喷水| 久久精品亚洲福利| 久久久99精品成人片中文字幕| 人妻无码中文久久久久专区| 久久亚洲精精品中文字幕| 久久久一本精品99久久精品88| 久久综合给合久久国产免费 | 人妻少妇久久中文字幕| 一本久久综合亚洲鲁鲁五月天| 色天使久久综合网天天| 97久久婷婷五月综合色d啪蜜芽| 一本色综合久久| 亚洲午夜久久久影院伊人| 久久久婷婷五月亚洲97号色| 久久久91人妻无码精品蜜桃HD| 精品人妻伦一二三区久久| 伊人久久大香线蕉av不卡| 亚洲精品乱码久久久久久按摩| 国产精品久久久久久久久久免费| 国产精品久久久久久久久软件 | 久久国产精品一区二区| 91性高湖久久久久| 国内精品人妻无码久久久影院| 免费精品久久天干天干| 久久久无码精品亚洲日韩按摩| 99精品国产在热久久无毒不卡| 色偷偷91久久综合噜噜噜噜| 久久WWW免费人成一看片| 国产欧美久久久精品| 久久婷婷人人澡人人| 免费国产99久久久香蕉| 色欲综合久久躁天天躁蜜桃| 久久精品国产亚洲麻豆| 亚洲人成无码久久电影网站| 99久久免费国产特黄| 狠狠色丁香婷婷久久综合五月| 精品精品国产自在久久高清| 一本色道久久99一综合| 91精品国产91热久久久久福利| 国产69精品久久久久久人妻精品|