Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some
roads between some villages and your job is the build some roads such that all
the villages are connect and the length of all the roads built is
minimum.

 
Input
The first line is an integer N (3 <= N <= 100),
which is the number of villages. Then come N lines, the i-th of which contains N
integers, and the j-th of these N integers is the distance (the distance should
be an integer within [1, 1000]) between village i and village j.

Then
there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each
line contains two integers a and b (1 <= a < b <= N), which means the
road between village a and village b has been built.

 
Output
You should output a line contains an integer, which is
the length of all the roads to be built such that all the villages are
connected, and this value is minimum.
 
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
 
Sample Output
179
 
 
很简单的一个最小生成树 题意也很简单 看完直接AC了  好开心O(∩_∩)O~~

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; #define inf 99999999 int map[][],path,node[],vis[],n; void Prim()
{
int ans=;
vis[]=;
node[]=;
for(int k=;k<=n;k++)
{
int minn=inf;
for(int i=;i<=n;i++)
if(!vis[i]&&minn>node[i])
{
minn=node[i];
ans=i;
}
vis[ans]=;
path+=node[ans]; for(int i=;i<=n;i++)
{
if(!vis[i]&&node[i]>map[ans][i])
node[i]=map[ans][i];
}
}
} int main()
{
while(cin>>n)
{
for(int i=;i<=n;i++)
{
node[i]=inf;vis[i]=;
for(int j=;j<=n;j++)
cin>>map[i][j];
}
int m;
cin>>m;
while(m--)
{
int a,b;
cin>>a>>b;
map[a][b]=map[b][a]=;
}
path=;
Prim();
cout<<path<<endl;
}
return ;
}

hdu1102 Constructing Roads (简单最小生成树Prim算法)的更多相关文章

  1. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  2. hdu1102 Constructing Roads 基础最小生成树

    //克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...

  3. 图论算法(五)最小生成树Prim算法

    最小生成树\(Prim\)算法 我们通常求最小生成树有两种常见的算法--\(Prim\)和\(Kruskal\)算法,今天先总结最小生成树概念和比较简单的\(Prim\)算法 Part 1:最小生成树 ...

  4. 最小生成树,Prim算法与Kruskal算法,408方向,思路与实现分析

    最小生成树,Prim算法与Kruskal算法,408方向,思路与实现分析 最小生成树,老生常谈了,生活中也总会有各种各样的问题,在这里,我来带你一起分析一下这个算法的思路与实现的方式吧~~ 在考研中呢 ...

  5. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  6. 最小生成树Prim算法(邻接矩阵和邻接表)

    最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...

  7. 最小生成树—prim算法

    最小生成树prim算法实现 所谓生成树,就是n个点之间连成n-1条边的图形.而最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int map[ ...

  8. Highways POJ-1751 最小生成树 Prim算法

    Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...

  9. SWUST OJ 1075 求最小生成树(Prim算法)

    求最小生成树(Prim算法) 我对提示代码做了简要分析,提示代码大致写了以下几个内容 给了几个基础的工具,邻接表记录图的一个的结构体,记录Prim算法中最近的边的结构体,记录目标边的结构体(始末点,值 ...

随机推荐

  1. c++子类继承父类的覆盖问题

    废话少说,先看一段代码! #include <iostream> #include <string> using namespace std; class A { public ...

  2. 动态规划(决策单调优化):BZOJ 4518 [Sdoi2016]征途

    4518: [Sdoi2016]征途 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 532  Solved: 337[Submit][Status][ ...

  3. Android实现OCR扫描识别数字图片之图片扫描识别

    [Android实例] Android实现OCR扫描识别数字图片之图片扫描识别 Android可以识别和扫描二维码,但是识别字符串呢? google提供了以下解决方案用的是原来HP的相关资料. 可以吧 ...

  4. Java 单元测试Junit

    @Test @Before @After 测试方法运行前执行Before动作(比如创建资源),运行后执行After动作(比如销毁资源) @BeforeClass @AfterClass 测试类运行前执 ...

  5. log4j配置祥解

    第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 1 log4j.rootCategory=INFO, stdou ...

  6. SunTlsRsaPremasterSecret KeyGenerator not available问题解决

    本地Idea测试可以,部署到开发环境遇到下面的问题: javax.net.ssl.SSLKeyException: RSA premaster secret error at com.sun.net. ...

  7. SVM原理(1)

    SVM即支持向量机,是一种机器学习内的二类分类方法,是有监督学习方法. 首先我们需要建立一个分类任务: 首先考虑线性可分的情况:(所谓线性可分就是在N维空间上的两类点,可以用N-1个未知数的函数(超平 ...

  8. 软件设计模式 B卷

            软件设计模式 试 卷(作业考核 线上)  B  卷   学习中心:            院校学号:             姓名                (共        页 ...

  9. 【bzoj3514】Codechef MARCH14 GERALD07加强版

    hzwer上少有的几道需要建一下模的 要不是有这么几道题 我都觉得lct只会考裸题了呢 题解看hzwer吧 http://hzwer.com/4358.html 唯一蛋疼的就是为了处理0这个呵呵的位置 ...

  10. Lucene的多域查询、结果中查询、查询结果分页、高亮查询结果和结果评分

    1.针对多个域的一次性查询 1.1.三种方案     使用lucene构造搜索引擎的时候,如果要针对多个域进行一次性查询,一般来说有三种方法:     第一种实现方法是创建多值的全包含域的文本进行索引 ...