• <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
            • 評論內容較長,點擊標題查看
            • --王私江
            久久WWW免费人成一看片| 久久婷婷久久一区二区三区| 国产成人精品久久亚洲高清不卡 | 久久久久久a亚洲欧洲aⅴ| 999久久久免费国产精品播放| 欧美性大战久久久久久| 午夜欧美精品久久久久久久| 精品久久久久久国产潘金莲| 久久免费99精品国产自在现线| 日韩人妻无码一区二区三区久久99| 久久这里只有精品18| 亚洲精品国精品久久99热| 精品久久久久久亚洲精品| 亚洲国产视频久久| 国产高潮国产高潮久久久91| 亚洲国产精品无码久久久秋霞2 | 久久国产色AV免费看| 久久se精品一区二区影院| 久久99国产综合精品| 久久青青色综合| 久久国产高清一区二区三区| 久久精品国产99国产精品导航| 国产视频久久| 久久这里只有精品首页| 久久精品中文字幕无码绿巨人| 伊人久久大香线蕉综合热线| 狠狠精品久久久无码中文字幕 | 久久乐国产精品亚洲综合| 久久人人妻人人爽人人爽| 久久精品极品盛宴观看| 久久久久亚洲av毛片大| 国产成人久久精品二区三区| 国产精品福利一区二区久久| 久久精品国产亚洲av水果派| 久久亚洲精品中文字幕| 国产精品无码久久久久久| 亚洲欧美成人综合久久久| 亚洲中文字幕无码久久2020| 久久综合给久久狠狠97色| 久久精品国产亚洲77777| 久久91综合国产91久久精品|