码迷,mamicode.com
首页 > Web开发 > 详细

洛谷 P1546 最短网络 Agri-Net x

时间:2017-04-13 21:29:10      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:cti   cst   section   包含   std   math   amp   strong   space   

题目背景

农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场。当然,他需要你的帮助。

题目描述

约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场。为了用最小的消费,他想铺设最短的光纤去连接所有的农场。

你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并所用光纤最短的方案。每两个农场间的距离不会超过100000

输入输出格式

输入格式:

 

第一行: 农场的个数,N(3<=N<=100)。

第二行..结尾: 后来的行包含了一个N*N的矩阵,表示每个农场之间的距离。理论上,他们是N行,每行由N个用空格分隔的数组成,实际上,他们限制在80个字符,因此,某些行会紧接着另一些行。当然,对角线将会是0,因为不会有线路从第i个农场到它本身。

 

输出格式:

 

只有一个输出,其中包含连接到每个农场的光纤的最小长度。

 

输入输出样例

输入样例#1:
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
输出样例#1:
28

说明

题目翻译来自NOCOW。

USACO Training Section 3.1

60分代码:(错误原因的话是什么呢?)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define Maxn 100001
 6 
 7 using namespace std;
 8 
 9 int n,c=0,f1,f2,tot=0,k=0;
10 int f[101];
11 
12 struct point{
13     int x,y,w;
14     bool operator < (const point &qwq)const
15     {
16         return w<qwq.w;
17     }//重载运算符(多么高大上的东西!但我还是不太明白怎么使用)
18 }p[Maxn];
19 
20 int find(int x)
21 {
22     if(f[x]!=x) f[x]=find(f[x]);
23     return f[x];
24     //return x == f[x] ? x : find(f[x]); 
25 }
26 
27 int main()
28 {
29     int v;
30     scanf("%d",&n);
31     for(int i=1;i<=n;i++)
32     {
33         for(int j=1;j<=n;j++)
34         {
35             scanf("%d",&v);
36             if(v!=0) 
37             {
38                 c++;
39                 p[c].x=i;
40                 p[c].y=j;
41                 p[c].w=v;
42             }
43         }
44     }
45     for(int i=1;i<=c;i++) f[i]=i;
46     sort(p+1,p+c+1);//快排 
47     for(int i=1;i<=c;i++)
48     {
49         f1=find(p[i].x);
50         f2=find(p[i].y);
51         if(f1!=f2)
52         {
53             f[f2]=f1;
54             tot+=p[i].w;
55             k++;
56         }
57         if(k==n-1) break;
58     }
59     printf("%d",tot);
60     return 0;
61 }

AC代码:(很明显,我重新写了,不是用的(!=0)那种方法,用的最基础的)

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 
 7 using namespace std;
 8 
 9 int n,Mst;
10 int f[101];
11 
12 struct hh
13 {
14     int a,b,c;
15     bool operator < (const hh &ww)const
16     {
17         return c < ww.c;
18     }
19 }d[10001];
20 
21 int find(int l)
22 {
23     return l == f[l] ? l : find(f[l]);
24 }
25 
26 int main()
27 {
28     cin>>n;
29     for(int i=1;i<=n;i++)
30       for(int j=1;j<=n;j++)
31         {
32           cin>>d[(i-1)*n+j].c;//进行“超边处理”,使d能够“连接”起来 
33           d[(i-1)*n+j].a=i;
34           d[(i-1)*n+j].b=j;
35         } 
36     sort(d+1,d+n*n+1);//快排 
37     for(int i=1;i<=n;i++)
38       f[i]=i;
39     int xx=0,i=0;
40     while(xx<n-1)
41       {
42         i++;
43         int r1=find(d[i].a);
44         int r2=find(d[i].b);
45         if(r1!=r2)
46           {
47             f[r1]=r2;
48             xx++;
49             Mst+=d[i].c;
50           }
51       }
52     cout<<Mst;
53 }

 

洛谷 P1546 最短网络 Agri-Net x

标签:cti   cst   section   包含   std   math   amp   strong   space   

原文地址:http://www.cnblogs.com/zxqxwnngztxx/p/6705711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!