题意:n个点修路,要求总长度最小,但是有两个点p、q必须相连

思路:完全图,prim算法的效率取决于节点数,适用于稠密图。用prim求解。

p、q间距离设为0即可,最后输出时加上p、q间的距离

prim算法:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std; #define INF 15000//计算得最长值
#define MAXN 128
bool vis[MAXN];
double lowc[MAXN]; struct Point{
double x,y;
}p[MAXN]; double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double prim(double cost[][MAXN],int n){//标号从0开始
double ans=,minc;
int i,j,p;
memset(vis,false,sizeof(vis));
vis[]=true;
for(i=;i<n;++i)lowc[i]=cost[][i];
for(i=;i<n;++i){
minc=INF;
p=-;
for(j=;j<n;++j)
if(!vis[j]&&lowc[j]<minc){
minc=lowc[j];
p=j;
}
if(minc==INF)return -;//原图不连通
ans+=minc;
vis[p]=true;
for(j=;j<n;++j)
if(!vis[j]&&cost[p][j]<lowc[j])
lowc[j]=cost[p][j];
}
return ans;
} int main(){
int n,m,a,b,i,j;
double cost[MAXN][MAXN],w;
while(~scanf("%d",&n)&&n){
//m=n*(n-1)/2;//m边条数
scanf("%d%d",&a,&b);
--a;--b;
for(i=;i<n;++i)
scanf("%lf%lf",&p[i].x,&p[i].y); for(i=;i<n;++i)
for(j=i+;j<n;++j){
w=dis(p[i],p[j]);
if((i==a&&j==b)||(j==a&&i==b))w=;
cost[i][j]=cost[j][i]=w;
}
printf("%.2f\n",prim(cost,n)+dis(p[a],p[b]));
}
return ;
}

kruskal算法的效率取决于边数,适用于稀疏图。

边数为50*50,也不是很多,也可用kruskal算法:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std; #define MAXN 110//最大点数
#define MAXM 10000//最大边数
int F[MAXN];//并查集使用 struct Point{
double x,y;
}p[MAXN]; struct Edge{
int u,v;
double w;
}edge[MAXM];//存储边的信息,包括起点/终点/权值
int tol;//边数,加边前赋值为0 double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} void addedge(int u,int v,double w){
edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
} //排序函数,将边按照权值从小到大排序
bool cmp(Edge a,Edge b){
return a.w<b.w;
} int find(int x){
if(F[x]==-)return x;
return F[x]=find(F[x]);
} //传入点数,返回最小生成树的权值,如果不连通返回-1
double kruskal(int n){
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp);
int cnt=;//计算加入的边数
int i,u,v,t1,t2;
double w,ans=;
for(i=;i<tol;++i){
u=edge[i].u;
v=edge[i].v;
w=edge[i].w;
t1=find(u);
t2=find(v);
if(t1!=t2){
ans+=w;
F[t1]=t2;
++cnt;
}
if(cnt==n-)break;
}
if(cnt<n-)return -;//不连通
return ans;
} int main(){
int n,a,b,i,j;
double w;
while(~scanf("%d",&n)&&n){
scanf("%d%d",&a,&b);
for(i=;i<=n;++i)
scanf("%lf%lf",&p[i].x,&p[i].y); tol=;
for(i=;i<=n;++i)
for(j=i+;j<=n;++j){
w=dis(p[i],p[j]);
if((i==a&&j==b)||(j==a&&i==b))w=;
addedge(i,j,w);
}
printf("%.2f\n",kruskal(n)+dis(p[a],p[b]));
}
return ;
}

hdu 4463 Outlets(最小生成树)的更多相关文章

  1. HDU—4463 Outlets 最小生成树

    In China, foreign brand commodities are often much more expensive than abroad. The main reason is th ...

  2. hdu 4463 Outlets(最小生成树)

    Outlets Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submi ...

  3. 【HDU 4463 Outlets】最小生成树(prim,kruscal都可)

    以(x,y)坐标的形式给出n个点,修建若干条路使得所有点连通(其中有两个给出的特殊点必须相邻),求所有路的总长度的最小值. 因对所修的路的形状没有限制,所以可看成带权无向完全图,边权值为两点间距离.因 ...

  4. HDU 4463 Outlets(最小生成树给坐标)

    Problem Description In China, foreign brand commodities are often much more expensive than abroad. T ...

  5. HDU 4463 Outlets (最小生成树)

    题意:给定n个点坐标,并且两个点已经连接,但是其他的都没有连接,但是要找出一条最短的路走过所有的点,并且路线最短. 析:这个想仔细想想,就是应该是最小生成树,把所有两点都可以连接的当作边,然后按最小生 ...

  6. HDU 4463 Outlets 【最小生成树】

    <题目链接> 题目大意: 给你一些点的坐标,要求你将这些点全部连起来,但是必须要包含某一条特殊的边,问你连起这些点的总最短距离是多少. 解题分析: 因为一定要包含那条边,我们就记录下那条边 ...

  7. hdu 4463 Outlets

    #include<bits/stdc++.h> using namespace std; double x[100+5],y[100+5]; double e[100+5][100+5]; ...

  8. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  9. hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树

    题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 #include<cstdio& ...

随机推荐

  1. Linux主机被SSH精神病(Psychos)暴力攻破后成为肉鸡的攻防过程

    近日公司局域网突然变得非常慢,上网受到很大影响,不仅仅是访问互联网慢,就连访问公司内部服务器都感到异常缓慢.于是对本局域网网关进行测试:   $ ping 10.10.26.254   发现延时很大, ...

  2. Codeforces 667D World Tour【最短路+枚举】

    垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...

  3. vue之组件理解(一)

    组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 data.computed.watch.methods 以及生命周期钩子等.仅有的例外是像 el 这样根实例特有的选项. ...

  4. Atcoder 3857 Median Sum

    Problem Statement You are given N integers A1, A2, ..., AN. Consider the sums of all non-empty subse ...

  5. SystemTap使用技巧 1 - 4 非常重要

    http://blog.csdn.net/wangzuxi/article/details/42849053

  6. vs2015使用Git管理项目

    初级 1,在码云上去注册一个帐号(码云的私有库是免费的,安全性怎么样我不知道) 2,在码云上新建一个项目,把相关的开发人员加到这个项目里,会得到这个项目在码云上的远程仓库的地址. 3,打开vs2015 ...

  7. [LeedCode OJ]#28 Implement strStr()

    [ 声明:版权全部,转载请标明出处,请勿用于商业用途.  联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/implem ...

  8. 民大OJ 1668 追杀系列第二发

    追杀系列第二发 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte 总提交 : 57            测试通过 : 16 ...

  9. 将MySQL服务绑定到固定的IP地址上

    近期将在线服务迁移到了阿里云.     阿里云提供了云盾,安全上确实比其他的云服务有了很大的提高,遗憾的是,没有防火墙,还是需要自己设置.阿里云的虚拟机跑在XEN上,直接使用YUM安装iptables ...

  10. scrollTo(String text) and scrollToExact(String text) method of Android Driver not working

    Using the scrollTo(String text) and scrollToExact(String text) method of Android Driver. However the ...