<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      書山有徑勤為路>>>>>>>>

      <<<<<<<<學海無涯苦作舟!

      Kruskal算法解決POJ 2421--有點難度

      Description

      There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

      We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

      Input

      The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

      Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

      Output

      You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

      Sample Input

      3
      0 990 692
      990 0 179
      692 179 0
      1
      1 2
      

      Sample Output

      179
       
      首先,先要搞懂這個題目的意思.
      前頭的輸入就求說了.來看看
      1?。?/span>
      是什么意思吧.
      它的意思是說,1 2這兩點間的道路是有了.
      那末,我們要做的工作就是在剩下的那些點中
      找出一條最短的路徑,這個就是Kruskal路徑,
      要使得每個村莊之間都要連通。
      只要把建好的路恰當的處理一下,這個題目
      就非常簡單了,就是純粹的Kurskal算法。
       
       
      
      
      View Code
      #include "iostream"
      #include "algorithm"
      using namespace std;
      structEdge
      {
          int begin;
          int end;
          int length;
      };
      Edge Num[10001];
      int Father[1001], Map[1001][1001]; 
      int Minlen, Edge_Num,i, j,TmpBegin, TmpEnd, counter, Total, BuildNum, From, To;
      int Find(int k)
      {
          return Father[k]==k?k:Father[k]=Find(Father[k]);
      }
      int Cmp(Edge a, Edge b)
      {
          return a.length<b.length;
      }
      int Kruskal()
      {
          Minlen = 0;
          Edge_Num = (Total-1)*Total/2;
          for(i=0; i<Edge_Num; i++)
          {
              TmpBegin = Find(Num[i].begin);
              TmpEnd = Find(Num[i].end);
              if(TmpBegin != TmpEnd)
              {
                  Father[TmpBegin] = TmpEnd;
                  Minlen += Num[i].length;
              }
          }
          return Minlen;
      }
      void Init()
      {
          cin>>Total;
          counter = 0;
          Edge_Num = (Total-1)*Total/2;
          for(i=1; i<=Total; i++)
          {
              for(j=1; j<=Total; j++)
              {
                  cin>>Map[i][j];
                  if(j>i)
                  {
                      Num[counter].begin = i;
                      Num[counter].end = j;
                      Num[counter++].length = Map[i][j];
                  }
              }
              Father[i] = i;
          }
          cin>>BuildNum;
          while(BuildNum--)
          {
              cin>>From>>To;
              TmpBegin = Find(From);  //1
              TmpEnd = Find(To);  //2
              Father[TmpBegin] = TmpEnd;  //3 這個地方就是將已經存在的邊用并查集給合并起來,這就是本題的關鍵了。
                    //標有1 2 3的這三行就是用來處理已經建好的道路的
          }
          sort(Num, Num+counter, Cmp);
      }
      int main()
      {
          Init();
          cout<<Kruskal()<<endl;
          return 0;
      }
      
      

       

       

      posted on 2011-09-22 19:10  More study needed.  閱讀(482)  評論(0)    收藏  舉報

      導航

      書山有徑勤為路>>>>>>>>

      <<<<<<<<學海無涯苦作舟!

      主站蜘蛛池模板: 永久免费AV无码网站大全| 国内精品无码一区二区三区| 久久高清超碰AV热热久久| 久久精品人妻无码一区二区三区| 一区二区三区不卡国产| 无码免费大香伊蕉在人线国产| 中文字幕少妇人妻精品| 黄色大全免费看国产精品| 香蕉eeww99国产在线观看| 狠狠色噜噜狠狠狠狠2021| 激烈的性高湖波多野结衣| 国产精品不卡一区二区久久| 色爱综合另类图片av| 国产成人亚洲综合图区| 国产一区二区在线激情往| 久久国产乱子伦免费精品无码 | 国产专区一va亚洲v天堂| 久久中文骚妇内射| 亚洲熟妇一区二区三个区| 国产成人一区二区不卡| 无码人妻人妻经典| 国产涩涩视频在线观看| 欧美人禽zozo动人物杂交| 国产午夜精品福利免费看| 中文字幕在线精品人妻| 四虎永久播放地址免费| 亚洲中文无码手机永久| 粉嫩jk制服美女啪啪| 日本视频一两二两三区| 动漫精品专区一区二区三区| 久久亚洲精精品中文字幕| 亚洲AV成人片不卡无码| 熟妇女人妻丰满少妇中文字幕| 蜜臀av一区二区三区在线| 激情亚洲一区国产精品| 熟女熟妇乱女乱妇综合网| 性一交一乱一伦| 无码激情亚洲一区| 男人猛戳女人30分钟视频大全| 国产jizzjizz视频| 亚洲一区二区三区蜜桃臀|