来源:dlut oj

1105: Zhuo’s Dream

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 40 Solved: 14
[Submit][Status][Web Board]

Description

Zhuo is a lovely boy and always make day dream. This afternoon he has a dream that he becomes the king of a kingdom called TwoBee.

In the dream Zhuo faced a hard situation: he should make a traffic construction plan for the kingdom. We have known that the kingdom consists of N cities and M dirt roads, it’s very uncomfortable when travelling in the road. Now he would choose some roads to rebuild to concrete road. To avoid called TwoBee by the common people, Zhuo wants to show some excellent thing to this world while it needs your help. He wants his plan achieve two goals:

1. We should choose just N-1 roads to rebuild. After rebuild we can also travel between any two cities by the concrete roads.

2. We want to minimize the longest length among the chosen roads.

So you should write a program to calculate the total length of the chosen roads.

Input

There are multiple test cases.

The first line contains two integers N and M. (1 <= N <= 100, 1 <= M <= 10000)

In the next M lines, each line contains three integers a, b, w, indicating that there is a dirt road between city a and city b with length w. (1 <= a, b <= N, 1 <= w <= 1000)

Output

For each test case, output an integer indicating the total length of the chosen roads. If there is no solution, please output -1.

Sample Input

3 3
1 2 1 1 3 1 2 3 3

Sample Output

2

HINT

Source

Rainy

prim算法

#include <cstdio>
#include <iostream>
#include <memory.h>
using namespace std;
const int maxn=;
const int inf=<<;
int map[maxn][maxn];
int dis[maxn];
bool flag[maxn];
int a,b,c,n,m,ans; void init()
{
memset(map,-,sizeof(map));
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]==- || c< map[a][b])
{
map[a][b]=map[b][a]=c;
}
}
} void prim()
{
memset(flag,false,sizeof(flag));
for(int i=;i<=n;i++)dis[i]=inf;
dis[]=;
ans=;
for(int j=;j<=n;j++)
{
int now,value=inf;
for(int i=;i<=n;i++)
{
if(flag[i]==false && dis[i]<value)
{
value=dis[i];
now=i;
}
}
if(value==inf)
{
cout << "-1" <<endl;
return;
}
flag[now]=true;
ans+=dis[now];
for(int i=;i<=n;i++)
{
if(!flag[i] && map[now][i]!=- && dis[i]>map[now][i])
dis[i]=map[now][i];
}
}
cout << ans <<endl;
} int main()
{
//'freopen("in.txt","r",stdin);
while(cin >> n >> m)
{
init();
prim();
}
return ;
}
 下面是最小生成树的Kruskal算法,这个算法原理看起来很复杂,但实现起来很简单:开始的时候是每个顶点一棵树,并将边按权重升序排列。然后从前到后按循序选边,如果当前选择的边的两个顶点分在两棵不同的树中,则将该边加入到最小生成树中,并合当前边连接的两棵树,如果边得两个顶点在相同的树中,则不做任何处理,需要注意的是这个算法是针对无向连通图的,如果是有限图,则需要在算法中做些处理,但算法原理是一样的。
kruskal算法:
 
#include <cstdio>
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
const int maxn=;
int m,n;
int cnt,length;
int parent[maxn]; struct node
{
int from;
int to;
int val;
}s[maxn]; bool cmp(const node &l,const node &r)
{
return l.val<r.val;
} void init()
{
for(int i=;i<maxn;i++)
{
parent[i]=i;
}
} int find(int x)
{
while(x!=parent[x])
{
x=parent[x];
}
return x;
} void merge(int a,int b,int val)
{
int roota=find(a);
int rootb=find(b);
if(roota!=rootb)
{
cnt++;
parent[rootb]=roota;
length+=val;
}
} void kruskal()
{
sort(s,s+n,cmp);
for(int i=;i<n;i++)
{
merge(s[i].from,s[i].to,s[i].val);
if(cnt==m-)break;
}
if(cnt<m-)
{
length=-;
}
} int main()
{
// freopen("in.txt","r",stdin);
while(cin >> m >> n)
{
init();
cnt=,length=;
memset(s,,sizeof(s));
for(int i=;i<n;i++)
{
cin >> s[i].from >> s[i].to >> s[i].val;
}
kruskal();
cout << length <<endl;
}
return ;
}
 

【prim + kruscal 】 最小生成树模板的更多相关文章

  1. HDU 1301-Jungle Roads【Kruscal】模板题

    题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...

  2. poj 1258 最小生成树 模板

    POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...

  3. POJ-图论-最小生成树模板

    POJ-图论-最小生成树模板 Kruskal算法 1.初始时所有结点属于孤立的集合. 2.按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条 ...

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

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

  5. 最小生成树模板【kruskal & prim】

    CDOJ 1966 Kruskal 解法 时间复杂度O(mlogm) m为边数,这里主要是边排序占时间,后面并查集还好 #include <cstdio> #include <cst ...

  6. POJ 1258:Agri-Net Prim最小生成树模板题

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45050   Accepted: 18479 Descri ...

  7. 最小生成树--prim+优先队列优化模板

    prim+优先队列模板: #include<stdio.h> //大概要这些头文件 #include<string.h> #include<queue> #incl ...

  8. HDU 1879 继续畅通工程(Prim||Kruscal模板题)

    原题链接 Prim(点归并) //异或运算:相同为假,不同为真 #include<cstdio> #include<algorithm> #define maxn 105 us ...

  9. HDOJ-1301(最小生成树模板+Prim算法)

    Jungle Roads HDOJ-1301 这是最小生成树的水题,唯一要注意的就是那个n,其实输入只有n-1行. #include<iostream> #include<cstdi ...

随机推荐

  1. POJ 2313 Sequence#贪心

    (- ̄▽ ̄)-* 找规律 //初始化为B[i]=A[i] //然后由V=|A[1]-B[1]|+|A[2]-B[2|+|A[3]-B[3]| // +|B[1]-B[2]|+|B[2]-B[3]| / ...

  2. 3、Hibernate三态间的转换

    学过hibernate的人都可能都知道hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),大家伙也许也知道这三者之间的区别 ...

  3. 深入浅出聊Unity3D项目优化:从Draw Calls到GC

    前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章,而且陈述的都是既有的事实,因而给自己“文(dou)学(bi)”加工留下的 ...

  4. web页面开发相关基础

    CSS是一种用于web的标准布局语言,可以控制版面.颜色以及元素和图像的大小和位置.HTML文档应该利用外部样式表来定义文档中使用的样式.JavaScript也应该放在外部文档中,这个文档应该只包含J ...

  5. osg + cuda

    #include <osg/Notify> #include <osgViewer/Viewer> #include <osgCompute/Memory> #in ...

  6. HDU 1509 Windows Message Queue(队列)

    题目链接 Problem Description Message queue is the basic fundamental of windows system. For each process, ...

  7. wuzhi 五指 数据库仿 thinkphp

    在/coreframe/app/模块/libs/class/mydb.class.php 中编辑 调用 : $mydb = load_class("mydb","模块&q ...

  8. OC类的本质及分类

    (一)类的本质 类对象(class object)与实例对象(instance object) 类本身也是一个对象,是class类型的对象,简称“类对象”. 在/usr/include/objc/ob ...

  9. lucene索引的创建与搜索

    package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...

  10. linux双线ip设置(不需额外增加路由表)

    linux 双线ip设置(不需额外增加路由表,只需修改下面就ok了)修改   vi /etc/iproute2/rt_tables              (增加电信和网通两个路由表) 增加252  ...