SSLOJ1612 最优布线问题

Description

学校有n台计算机,为了方便数据传输,现要将它们用数据线连接起来。两台计算机被连接是指它们之间有数据线连接。由于计算机所处的位置不同,因此不同的两台计算机的连接费用往往是不同的。

当然,如果将任意两台计算机都用数据线连接,费用将是相当庞大的。为了节省费用,我们采用数据的间接传输手段,即一台计算机可以间接的通过若干台计算机(作为中转)来实现与另一台计算机的连接。

现在由你负责连接这些计算机,你的任务是使任意两台计算机都连通(不管是直接的或间接的)。

Input

输入文件wire.in,第一行为整数n(2<=n<=100),表示计算机的数目。此后的n行,每行n个整数。第x+1行y列的整数表示直接连接第x台计算机和第y台计算机的费用。

Output

输出文件wire.out,一个整数,表示最小的连接费用。

Sample Input

3
0 1 2
1 0 1
2 1 0

Sample Output

2(注:表示连接1和2,2和3,费用为2)

思路

该题可以用prim和Kruskal做(话说读入邻接矩阵要什么Kruskal和邻接表,搞不懂老师为什么要搞3种做法
prim+邻接矩阵:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct f{
	int x,y;
};
int m[101];
bool operator <(const f &a,const f &b)
{
	return a.x>b.x;
}
priority_queue<f> p;
int a[101][101];
int main()
{
	m[1]=-0x7f7f7f7f+1;
	int n;
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
		{
			cin>>a[i][j];
		}
	}
	int n2=n-1;
	long long s=0;
	f o,ow;
	for (int i=2;i<=n;i++)
	{
		o.x=a[1][i],o.y=i;
		m[i]=o.x;
		p.push(o);
	}
	while (n2!=0)
	{
		n2--;
		while (p.size()&&p.top().x>m[p.top().y]) p.pop();
		o=p.top();
		p.pop();
		s+=o.x;
		for (int i=1;i<=n;i++)
		{
			if (a[o.y][i]<m[i])
			{
				m[i]=a[o.y][i];
				ow.x=m[i],ow.y=i;
				p.push(ow);
			}
		}
		m[o.y]=-0x7f7f7f7f+1;
	}
	cout<<s;
	return 0;
}

prim+邻接表

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct f{
	int x,y;
};
int m[101];
bool operator <(const f &a,const f &b)
{
	return a.x>b.x;
}
priority_queue<f> p;
int head[101],to[10001],net[10001],l[10001],tot=1;
void add(int x,int y)
{
	to[tot]=y,net[tot]=head[x],head[x]=tot,tot++;
	return;
}
int main()
{
	int n;
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
		{
			cin>>l[tot];
			add(i,j);
		}
	}
	int n2=n-1;
	long long s=0;
	f o,ow;
	for (int i=head[1];i;i=net[i])
	{
		o.x=l[i],o.y=to[i];
		m[o.y]=o.x;
		p.push(o);
	}
	m[1]=-0x7f7f7f7f+1;
	while (n2!=0)
	{
		n2--;
		while (p.size()&&p.top().x>m[p.top().y]) p.pop();
		o=p.top();
		p.pop();
		s+=o.x;
		for (int i=head[o.y];i;i=net[i])
		{
			if (l[i]<m[to[i]])
			{
				m[to[i]]=l[i];
				ow.x=m[to[i]],ow.y=to[i];
				p.push(ow);
			}
		}
		m[o.y]=-0x7f7f7f7f+1;
	}
	cout<<s;
	return 0;
}

Kruskal:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int m[101],f[101];
int tot=1;
struct ok{
 int to,start,l;
} a[101*101];
void add(int x,int y)
{
 a[tot].to=y,a[tot].start=x,tot++;
 return;
}
bool cmp(ok a,ok b)
{
 return a.l<b.l;
}
int find(int x)
{
 if (x==f[x]) return x;
 return f[x]=find(f[x]);
}
int main()
{
 int n;
 cin>>n;
 for (int i=1;i<=n;i++)
 {
  f[i]=i;
  for (int j=1;j<=n;j++)
  {
   cin>>a[tot].l;
   add(i,j);
  }
 }
 sort(a+1,a+n*n+1,cmp);
 long long s=0,o=1;
 for (int i=1;i<=n*n&&o<n;i++)
 {
  if (find(a[i].to)!=find(a[i].start))
  {
   f[find(a[i].to)]=find(a[i].start);
   s+=a[i].l;
   o++;
  }
 }
 cout<<s;
 return 0;
}//渣渣缩进
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值