• <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
            • 評論內容較長,點擊標題查看
            • --王私江
            免费精品久久久久久中文字幕| 污污内射久久一区二区欧美日韩| 久久99精品久久久久久不卡 | 丰满少妇人妻久久久久久| 午夜天堂精品久久久久| 国产精品VIDEOSSEX久久发布| 色婷婷综合久久久久中文| 精品熟女少妇av免费久久| 久久久亚洲欧洲日产国码aⅴ | 久久国产乱子伦免费精品| 亚洲精品美女久久777777| 日韩av无码久久精品免费| 久久国产色AV免费观看| 狠狠精品久久久无码中文字幕| 久久久久国产精品人妻| 国产精品中文久久久久久久| 精品久久人人爽天天玩人人妻| 国产成人无码精品久久久免费| 亚洲人成无码www久久久| 久久精品国产亚洲AV电影 | 亚洲AV日韩精品久久久久| av国内精品久久久久影院| 国产成人久久精品一区二区三区| 国产精品热久久毛片| 久久无码专区国产精品发布| 国产国产成人精品久久| 国产精品亚洲综合专区片高清久久久 | 久久国产视屏| 久久精品国产亚洲AV无码偷窥| 久久久久人妻精品一区二区三区| 亚洲国产精品综合久久网络| 99精品国产在热久久无毒不卡| 午夜精品久久久久久久无码| 国内精品久久久久| 国产精品久久久香蕉| 日本国产精品久久| 欧美综合天天夜夜久久| 欧美黑人激情性久久| 国产精品久久久久久久人人看 | 色综合久久久久网| 日韩人妻无码精品久久免费一|