COGS 457. 最优布线问题 解题报告

457. 最优布线问题

[问题描述]

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

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

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

[输入格式] 

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


使所有节点都联通,且路径总权值最小,裸的最小生成树。Kruscal水过。


其实正解是Prim……Kruscal会T一个点。然后开O2就A啦~\(≧▽≦)/~

#include<cstdio>
#include<iostream>
#include<algorithm>
#define COGS
using namespace std;
const int maxn=1500*1500+1;//数组,要开足!!!
int m,n,cnt=-1;
int p[1501],w[maxn],r[maxn],u[maxn],v[maxn];
void Init(){
	int x;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>x;
			if(i<j && x>0){
				cnt++;
				u[cnt]=i;v[cnt]=j;w[cnt]=x;//记录每条边
			}
		}
	}
}
int cmp(const int i,const int j){return w[i]<w[j];}//以权重为关键字排序,便于Kruskal贪心
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}//借助并查集实现的Kruskal
void Kruskal(){
	int ans=0;
	for(int i=0;i<n;i++)p[i]=i;
	for(int i=0;i<n*n;i++)r[i]=i;//初始化
	sort(r,r+(n*n),cmp);
	for(int i=0;i<n*n;i++){
		int e=r[i];int x=find(u[e]);int y=find(v[e]);
		if(x!=y){ans+=w[e];p[x]=y;}//不在同个连通分量,加入MST
	}
	cout<<ans;
}
int main()
{
	ios::sync_with_stdio(false);
	#ifdef COGS
		string FileName="wire";
		freopen((FileName+".in").c_str(),"r",stdin);
		freopen((FileName+".out").c_str(),"w",stdout);
	#endif
	Init();
	Kruskal();
}


以下是将代码修改为cot平滑的方法: 1. 首先,需要使用边界角的cot权重计算每个顶点的权重。 2. 然后,使用cot权重对每个顶点的邻域点进行加权计算,得到平滑后的坐标。 3. 最后,根据平滑后的坐标更新每个顶点的位置。 修改后的代码如下: float smooth() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); //cot平滑 for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = weight_sum = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); weight_sum += cot_weight; } cogs.push_back(cog / weight_sum); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } 其中cot函数的定义如下: double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } 注意,这里使用的是边界角的cot权重,因此在计算cot权重时需要判断当前边是否为边界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值