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

            poj1861

            Network

            Time Limit: 1000MS Memory Limit: 30000K
            Total Submissions: 9734 Accepted: 3630 Special Judge

            Description

            Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).
            Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
            You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.

            Input

            The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.

            Output

            Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.

            Sample Input

            4 6
            1 2 1
            1 3 1
            1 4 2
            2 3 1
            3 4 1
            2 4 1
            

            Sample Output

            1
            4
            1 2
            1 3
            2 3
            3 4
            

            本來不知道這題是個最小生成樹的,看圖論的一本書寫著,
            然后寫了鄰接表的kruskal,貌似書上這個效率比我的高
            然后就交了,模版題

            #include<algorithm>
            #include
            <iostream>
            #include
            <cstdio>
            #include
            <cstring>
            #include
            <cstdlib>
            using namespace std;
            #define maxn 1001
            #define maxm 20000
            int maxedge;
            struct node
            {
                
            int u,v,w;
            }
             edge[maxm];
            int parent[maxn];
            int n,m;
            int num;
            int ans[maxn];
            void ufset()
            {
                
            int i;
                
            for(i=1; i<=n; i++) parent[i]=-1;
            }

            int find(int x)
            {
                
            int s;
                
            for(s=x; parent[s]>=0; s=parent[s]);
                
            while (s!=x)//壓縮路徑,使后續查找加速
                {
                    
            int tmp=parent[x];
                    parent[x]
            =s;
                    x
            =tmp;
                }

                
            return s;
            }

            void union1(int R1,int R2)
            {
                
            int r1=find(R1),r2=find(R2);
                
            int tmp=parent[r1]+parent[r2];//兩個集合結點個數和
                if (parent[r1]>parent[r2])
                
            {
                    parent[r1]
            =r2;
                    parent[r2]
            =tmp;
                }

                
            else
                
            {
                    parent[r2]
            =r1;
                    parent[r1]
            =tmp;
                }

            }

            /*int cmp(const void *a const void *b)
            {
                node aa=*(struct node *)a;
                node bb=*(struct node *)b;
                return aa.w-bb.w;
            }
            */

            int cmp(struct node a,struct node b)
            {
                
            return a.w<b.w;
            }

            void kruskal()
            {
                
            int i,j;
                
            int sumweight=0;
                
            int u,v;
                num
            =0;
                ufset();
                
            for(i=0; i<m; i++)
                
            {
                    u
            =edge[i].u;
                    v
            =edge[i].v;
                    
            if (find(u)!=find(v))
                    
            {
                        
            if (edge[i].w>maxedge)
                        
            {
                            maxedge
            =edge[i].w;
                        }

                        ans[num]
            =i;num++;
                        union1(u,v);
                    }

                    
            if (num>=n-1)
                    
            {
                        
            break;
                    }

                }

            }

            int main()
            {
                
            int u,v,w;
                
            while (scanf("%d%d",&n,&m)!=EOF)
                
            {
                    
            for(int i=0; i<m; i++)
                    
            {
                        scanf(
            "%d%d%d",&u,&v,&w);
                        edge[i].u
            =u;
                        edge[i].v
            =v;
                        edge[i].w
            =w;
                    }

                    sort(edge,edge
            +m,cmp);
                    maxedge
            =0;
                    kruskal();
                    printf(
            "%d\n",maxedge);
                    printf(
            "%d\n",num);
                    
            for (int i=0;i<num;i++)
                        printf(
            "%d %d\n",edge[ans[i]].u,edge[ans[i]].v);
                }

                
            return 0;
            }

            posted on 2012-04-02 00:16 jh818012 閱讀(235) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            思思久久99热只有频精品66| 狠狠色伊人久久精品综合网 | 久久精品国产72国产精福利| 久久亚洲综合色一区二区三区| 99久久99久久精品国产片| 夜夜亚洲天天久久| 一级a性色生活片久久无| 人人狠狠综合久久88成人| 天天久久狠狠色综合| 久久久久高潮综合影院| 国产精品欧美亚洲韩国日本久久 | 一级做a爰片久久毛片毛片| 热re99久久6国产精品免费| 9999国产精品欧美久久久久久| 久久久久亚洲av成人网人人软件| 久久久久久久尹人综合网亚洲| 亚洲欧美成人久久综合中文网 | 国产午夜精品久久久久九九| 久久这里只精品99re66| 国产精品99久久久久久www| 777午夜精品久久av蜜臀 | 狠狠狠色丁香婷婷综合久久俺| 久久久久亚洲AV成人网| 久久66热人妻偷产精品9| 99久久夜色精品国产网站| 久久久久亚洲av成人无码电影| 久久精品九九亚洲精品天堂 | 亚洲va久久久噜噜噜久久男同| 久久99精品久久久久久9蜜桃 | 久久精品中文无码资源站 | 精品熟女少妇av免费久久| 久久99精品国产麻豆宅宅| 97精品伊人久久久大香线蕉 | 久久婷婷色香五月综合激情| 国产精品久久久久久搜索| 久久久久久久97| 国产精品99久久99久久久| 99久久精品国产高清一区二区| 国内高清久久久久久| 国产99久久久国产精品小说| 久久久久波多野结衣高潮|