• <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 閱讀(236) 評論(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
            • 評論內容較長,點擊標題查看
            • --王私江
            日韩精品无码久久一区二区三| 亚洲国产成人乱码精品女人久久久不卡 | 久久国产免费观看精品3| 久久受www免费人成_看片中文| 无码精品久久一区二区三区| 色综合久久久久无码专区| 久久精品国产亚洲沈樵| 亚洲国产一成久久精品国产成人综合 | 国产午夜福利精品久久| 久久精品国产亚洲Aⅴ香蕉| 日产精品久久久一区二区| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 性做久久久久久久久久久| 久久精品国产99久久久 | 精品少妇人妻av无码久久| 亚洲午夜久久久| 久久精品无码免费不卡| 国内精品伊人久久久久| 亚洲午夜久久久久久久久久 | 狠狠色婷婷久久综合频道日韩| 国产精品熟女福利久久AV| 国内精品久久久久影院一蜜桃| 久久久精品人妻一区二区三区蜜桃| 久久这里只精品国产99热| 少妇高潮惨叫久久久久久| 国产美女亚洲精品久久久综合 | 欧美午夜精品久久久久免费视| 亚洲人成无码网站久久99热国产| 国产精品日韩深夜福利久久| 好久久免费视频高清| AV色综合久久天堂AV色综合在| 久久天天躁狠狠躁夜夜2020一 | 亚洲?V乱码久久精品蜜桃 | 久久99久久无码毛片一区二区 | 少妇熟女久久综合网色欲| 久久精品国产WWW456C0M| 一本伊大人香蕉久久网手机| 久久精品国产91久久麻豆自制 | 久久国产精品成人免费| 久久综合久久综合久久| 欧美精品一区二区精品久久|