POJ_1287_mst
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 0 2 3 Sample Output 0 Source |
这道题其实就是求mst,贴AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAXN=55;
const int MAXM=1e6;
int father[MAXN];
struct Edge
{
int u,v,w;
}edge[MAXM];
int tot;
void addedge(int u,int v,int w)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot++].w=w;
}
int find_set(int x)
{
if(father[x]==-1)
return x;
else
return father[x]=find_set(father[x]);
}
bool cmp(Edge x,Edge y)
{
return x.w<y.w;
}
int Kruskal(int n)
{
memset(father,-1,sizeof(father));
sort(edge,edge+tot,cmp);
int ans=0;
int num=0;
for(int i=0;i<tot;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int fa1=find_set(u);
int fa2=find_set(v);
if(fa1!=fa2)
{
ans+=w;
num++;
father[fa1]=fa2;
}
if(num==n-1)
break;
}
if(num<n-1)
return -1;
else
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m))
{
if(n==0)
break;
tot=0;
int u,v,w;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
u--;
v--;
addedge(u,v,w);
}
int ans=Kruskal(n);
printf("%d\n",ans);
}
return 0;
}
POJ_1287_mst的更多相关文章
随机推荐
- 作业:用HTML制作邮箱登陆界面
<body leftmargin="200" rightmargin="200"> <font size="45" > ...
- poj2492 带权并查集
题意:研究一种生物,有n个生物个体,发现有一些之间进行了交配,给出了这些关系,问是否有同性恋的bug出现. 用0\1表示某元素和其祖先元素的性别关系,0 为相同,1 为不同,用 mod2 实现累计处理 ...
- 黑马程序员——JAVA基础之多线程的线程间通讯等
------- android培训.java培训.期待与您交流! ---------- 线程间通讯: 其实就是多个线程在操作同一个资源,但是动作不同. wait(); 在其他线程调用此对象的notif ...
- Attention and Augmented Recurrent Neural Networks
Attention and Augmented Recurrent Neural Networks CHRIS OLAHGoogle Brain SHAN CARTERGoogle Brain Sep ...
- matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。
最近跑深度学习,提出的feature是4096维的,放到我们的程序里,跑得很慢,很慢.... 于是,一怒之下,就给他降维处理了,但是matlab 自带的什么pca( ), princomp( )函数, ...
- Lucene 对文档打分的规则整理记录
摘引自:http://www.cnblogs.com/forfuture1978/archive/2010/02/08/1666137.html Lucene的搜索结果默认按相关度排序,这个相关度排序 ...
- datagrid中load,reload,loadData方法的区别
它有其中有load,reload,loadData这三个方法,它们都有相同的功能,都是加载数据的,但又有区别. load方法,比如我已经定义一个datagrid的id为grid,那这个方法的使用方式为 ...
- c++中的dictionary对象:map的使用备忘
#include <map> #include <iostream> using namespace std; void main(){ map <string, int ...
- zookeeper源码学习一——zookeeper启动
最近正在研究zookeeper,一些心得记录一下,如有错误,还请大神指正. zookeeper下载地址:http://zookeeper.apache.org/releases.html,百度一下就能 ...
- js列表分页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...