Networking
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6432   Accepted: 3488

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
1 2 37
2 1 17
1 2 68 3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32 5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12 0

Sample Output

0
17
16
26

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的更多相关文章

随机推荐

  1. hihoCoder#1055 : 刷油漆 (树形DP+01背包)

    题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max( ...

  2. poj3660 最短路/拓扑序

    题意:有n头牛,为了给牛排顺序,给出了牛之间的胜负关系,具有传递性,问给出的胜负关系是否可以给这些牛排出唯一的顺序. 其实是个拓扑排序问题,牛的胜负关系就是有向边,然后判断是否有唯一的拓扑序就行.当然 ...

  3. spring源码学习之:xml标签扩展配置例子

    在很多情况下,我们需要为系统提供可配置化支持,简单的做法可以直接基于Spring的标准Bean来配置,但配置较为复杂或者需要更多丰富控制的 时候,会显得非常笨拙.一般的做法会用原生态的方式去解析定义好 ...

  4. mysql学习之-三种安装方式与版本介绍

    MYSQL版本介绍 mysql分alpha,beta,rc,GA四个版本. alpha  暗示这是一个以展示新特性为目的的版本,存在比较多的不稳定因素,还会向代码中添加新新特性beta 以后的beta ...

  5. C#中正则表达式的使用

    目前为止,许多编程语言和工具都包含对正则表达式的支持,C#也不例外,C#基础类库中包含有一个命名空间(System.Text.RegularExpressions)和一系列可以充分发挥规则表达式威力的 ...

  6. SSRS入门相关笔记

    1.SSRS Server 的地址的查看及设置:打开 开始->程序-> Microsoft SQL Server 2012/2014 -> Configuration Tools - ...

  7. Cookielib

    Cookielib模块主要的对象有CookieJar.FileCookieJar.MozillaCookieJar.LWPCookieJar 它们的关系:CookieJar —-派生—->Fil ...

  8. 024. asp.net中第一次使用GridView (设置鼠标经过时更换背景色)

    1. 前端HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Inde ...

  9. 让Js顺序执行且回调之

    <script src="aaa"></script> <script type="aaasdf" id="asdf&q ...

  10. noip2003复赛普及组第一题——乒乓球

    /*======================================================================= 题一.乒乓球(Table.pas) [问题背景]国际 ...