• <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 閱讀(234) 評論(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
            • 評論內容較長,點擊標題查看
            • --王私江
            久久亚洲AV成人无码软件| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产精品久久久久久久久久影院| 99久久99这里只有免费费精品| 国产成人综合久久精品红| 伊人色综合九久久天天蜜桃| 日韩影院久久| 狠狠色丁香久久婷婷综合_中| 久久人人爽人人爽人人片AV不 | 无码久久精品国产亚洲Av影片| 久久中文字幕精品| 久久久精品人妻一区二区三区蜜桃 | 久久久国产精品网站| 久久亚洲国产精品一区二区| 国产真实乱对白精彩久久| 久久精品无码一区二区日韩AV| 亚洲国产成人久久笫一页 | 中文字幕乱码人妻无码久久| 无码AV中文字幕久久专区| 精品久久久久久中文字幕| 亚洲国产精品久久久久婷婷老年| 久久黄色视频| 中文精品久久久久人妻不卡| 国产精品毛片久久久久久久| 久久免费国产精品| 色婷婷综合久久久久中文| 久久精品中文字幕久久| 欧美国产精品久久高清| 午夜不卡久久精品无码免费| 成人亚洲欧美久久久久| 亚洲国产天堂久久久久久| 精品乱码久久久久久久| 日日狠狠久久偷偷色综合0| 久久无码人妻一区二区三区| 久久久久18| 91精品国产91久久久久福利| 伊人情人综合成人久久网小说 | 亚洲国产另类久久久精品黑人| 精品国产婷婷久久久| 久久精品亚洲精品国产色婷| 无码人妻少妇久久中文字幕|