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. 7.DropDownList的绑定

    ListView中是无法像TextBox等控件那样将DropDownList的选中值绑定到数据字段的,必须编程处理.如例子:人员的性别(男,女,保密),三个值固定写在DropDownList中. 在显 ...

  2. 【Linux】鸟哥的Linux私房菜基础学习篇整理(四)

    1. VI按键说明.(1)移动光标的方法h或向左箭头:光标向左移动一个字符:j或向下箭头:光标向下移动一个字符:k或向上箭头:光标向上移动一个字符:l或向右箭头:光标向右移动一个字符:进行多次移动,如 ...

  3. -_-#【jQuery插件】Colorpicker 颜色选择器

    Spectrum - The No Hassle jQuery Colorpicker (jQuery 插件) Color Picker jQuery plugins (jQuery 插件) colo ...

  4. 【数学】Codeforces 707C Pythagorean Triples

    题目链接: http://codeforces.com/problemset/problem/707/C 题目大意: 给你一个数,构造其余两个勾股数.任意一组答案即可,没法构造输出-1. 答案long ...

  5. Reverse Words in a String——LeetCode

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  6. 高效算法——C 分饼

    My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N ...

  7. iOS 用CocoaPods做iOS程序的依赖管理

    文档更新说明 2012-12-02 v1.0 初稿 2014-01-08 v1.1 增加设置 ruby 淘宝源相关内容 2014-05-25 v2.0 增加国内 spec 镜像.使用私有 pod.po ...

  8. 关于python保留几位小数,不进行四舍五入的方法

    def cut(num,c): c=10**(-c) return (num//c)*c print cut(2.999,2) 不过有一部分数会出现问题,还请大神评论 例如: >>> ...

  9. iOS开发之监测网络状态

    一.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户 ...

  10. Android 使用加速度传感器实现摇一摇功能及优化

    如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 目前很多应用已经实现了摇一摇功能,这里通过讲解该功能的原理及实现回顾一下加速度传感器的使用: 1.首先获得 ...