POJ 1287 Networking (最小生成树模板题)

Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 02 3
1 2 37
2 1 17
1 2 683 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 325 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 120

Sample Output

0
17
16
26
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
/*int fat[200],p,r;
struct node
{
int x,y,v;
};
node edge[2500];
bool cmp(node a,node b)
{
return a.v<b.v;
}
int find(int x)
{
if(x!=fat[x])
fat[x]=find(fat[x]);
return fat[x];
}
void unionn(int x,int y)
{
int fa=find(x);
int fb=find(y);
if(fa!=fb) fat[fa]=fb;
}
int main()
{
int p,r,i,tot,k,a,b,c;
while(cin>>p)
{
if(p==0) break;
cin>>r;
if(r==0) cout<<0<<endl;
else
{
for(i=1;i<=r;i++)
{
cin>>a>>b>>c;
edge[i].x=a;
edge[i].y=b;
edge[i].v=c;
}
for(i=1;i<=p;i++)
fat[i]=i;
sort(edge+1,edge+1+r,cmp);
tot=0;k=0;
for(i=1;i<=r;i++)
{
if(find(edge[i].x)!=find(edge[i].y))
{
unionn(edge[i].x,edge[i].y);
tot+=edge[i].v;
k++;
}
if(k==p-1) break;
}
cout<<tot<<endl;
}
}
}kruskal*/
#define inf 0x3f3f3f
int edge[2500][2500],lowcost[2500],v[2500],p,r;
void prim()
{
int minn,next,sum;
memset(v,0,sizeof(v));
v[1]=1;
sum=0;
for(int i=1;i<=p;i++)
lowcost[i]=edge[1][i];
for(int i=2;i<=p;i++)
{
int minn=inf;
for(int j=1;j<=p;j++)
{
if(!v[j]&&minn>lowcost[j])
{
minn=lowcost[j];
next=j;
}
}
sum+=minn;
v[next]=1;
for(int j=1;j<=p;j++)
{
if(!v[j]&&lowcost[j]>edge[next][j])
lowcost[j]=edge[next][j];
}
}
cout<<sum<<endl;
}
int main()
{
int a,b,c,i,j;
while(cin>>p)
{
if(!p) break;
cin>>r;
for(i=1;i<=p;i++)
{
for(j=1;j<=p;j++)
edge[i][j]=inf;
}
for(i=1;i<=r;i++)
{
cin>>a>>b>>c;
if(edge[a][b]>c)
edge[a][b]=edge[b][a]=c;
}
prim();
}
}

nedtworking相关推荐

最新文章

  1. HDFS集群安装部署
  2. 五种计算机编程语言注定要衰落,牛逼的你怎么看呢???
  3. 高并发,分布式系统要点
  4. sqlite3 编译问题
  5. 老罗直播——只要给你一个机会,你就伸双手去接!
  6. hdu 2188巴什博弈
  7. Java求出1000内的完数
  8. 百度人脸识别文档冲突,facetype应是face_type
  9. DB2 SQLCODE 异常大全编辑(二)
  10. 基于JAVA EE的临床科室管理系统
  11. 怎么让excel中一部分表格显示但是打印时不打印?
  12. Latex-加通讯作者的小信封标记
  13. hadoop2.8配置_Hadoop 2.8集群安装及配置记录
  14. Adobe CEP 常用宿主事件名
  15. mysql中的locate,MySQL LOCATE()用法及代码示例
  16. 华为eNSP配置防火墙进入web界面
  17. 数据结构 (3)栈与队列之粗心的人如何写oj血的教训
  18. opencv-python学习笔记【更新中】
  19. JavaScriptAPI
  20. win10搜索框突然不能搜索本地应用

热门文章

  1. react ssr php,一文吃透 React SSR 服务端渲染和同构原理
  2. Mysql 脚本兼容性测试_MySQL 测试小脚本〔案例〕
  3. python语言的主网址-python官方网站
  4. python工资一般多少西安-python西安薪资
  5. python发音模块-python声音模块
  6. 学python能干什么工作-什么是Python?学完之后能做哪些工作?
  7. python编程入门第3版pdf-Python编程入门第三版
  8. Nuance语音识别技术及解决方案
  9. 广义表取表头表尾_数据结构广义表的递归算法
  10. 能识别nvme的pe启动_【腾讯WeTest干货分享】机器学习在启动耗时测试中的应用及模型调优...