Optimal wiring and wiring

Source: Internet
Author: User

Optimal wiring and wiring
[Problem description] the school has n computers. To facilitate data transmission, we need to connect them with data lines. Two computers are connected by data lines. Because the computer is located in different locations, the connection fees for different two computers are often different. Of course, if you connect any two computers with data lines, the cost will be quite large. To save costs, we use indirect data transmission means, that is, a computer can indirectly connect to another computer through several computers (as transit. Now you are responsible for connecting these computers. The task is to connect any two computers (either directly or indirectly ). [Input format] input file wire. in, the first behavior integer n (2 <= n <= 100) indicates the number of computers. Next n rows, n integers in each line. The integer in column y of row x + 1 indicates the cost of directly connecting the x and y computers. Output Format: the output file wire. out, an integer that indicates the minimum connection fee. [Input sample] 3 0 1 2 1 0 1 2 1 0 [output sample] 2 (Note: Connection 1, 2, and 3, cost 2)

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int maxn=0x7fffffff; 6 int map[101][101]; 7 int minn[101]; 8 int vis[101]; 9 int vis2[101][101];10 int main()11 {12     int n,m;13     scanf("%d%d",&n,&m);14     for(int i=0;i<=n;i++)minn[i]=maxn;15     for(int i=0;i<=n;i++)16     for(int j=0;j<=n;j++)17     {18         if(i==j)19         map[i][j]=0;20         else21         map[i][j]=maxn;22     }23     for(int i=1;i<=m;i++)24     {25         int x,y,z;26         scanf("%d%d%d",&x,&y,&z);27         map[x][y]=z;28         map[y][x]=z;29     }30     for(int i=1;i<=n;i++)31     {32         if(map[1][i])33         minn[i]=map[1][i];34     }35     minn[1]=0;36     vis[1]=1;37     int now=1;38     for(int i=2;i<=n;i++)39     {40         int k=0;41         for(int j=2;j<=n;j++)42         {43             if(vis[j]==0&&minn[j]<minn[k])44             {45                 k=j;46             }47         }48         vis[k]=1;49         //printf("%d %d\n",now,k);50         now=k;51         for(int j=2;j<=n;j++)52         {53             if(vis[j]==0&&map[k][j]<minn[j])54             {55                 minn[j]=map[k][j];56             }57         }58     }59     int tot=0;60     for(int i=1;i<=n;i++)61     tot=tot+minn[i];62     printf("%d\n",tot);63     for(int i=1;i<=n;i++)64     {65         for(int j=1;j<=n;j++)66         {67             if(map[i][j]==minn[i]&&vis2[i][j]==0&&i!=j)68             {69                 vis2[i][j]=1;70                 vis2[j][i]=1;71                 printf("%d %d\n",j,i);72             }73         }74     }75     return 0;76 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.