题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102

/************************************************************************/
/*
hdu Constructing Roads
最小生成树
题目大意:在N个村子中已经存在部分存在连通,建最少长度的路使得所有的村子连通。
解题思路:已经连通的村子其中间的路径作为0,即修建的时候修建为0耗费,求这些节点的最小生成树。
*/
/************************************************************************/ #include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int map[N][N];
int mark[N];
int n,q,i,j; int Prim()
{
int sum = ;
int t = n;
int min,k;
memset(mark,,sizeof(mark));
while(--t)
{
min = ;
for (i = ; i <= n; i++)
{
if(mark[i]!= && map[][i] < min)
{
min = map[][i];
k = i;
}
}
mark[k] = ;
sum += min;
for (j = ; j <= n; j++)
{
if(mark[j]!= && map[k][j] < map[][j])
{
map[][j] = map[k][j];
}
}
}
return sum;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
scanf("%d",&map[i][j]);
}
}
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&i,&j);
map[i][j] = ;
map[j][i] = ;
} printf("%d\n",Prim());
} return ;
}

hdu Constructing Roads (最小生成树)的更多相关文章

  1. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  2. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  4. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

    题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...

  5. HDU 1102 Constructing Roads(最小生成树,基础题)

    注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...

  6. hdu Jungle Roads(最小生成树)

    Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...

  7. HDU 1301Jungle Roads(最小生成树 prim,输入比较特殊)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1301 Jungle Roads Time Limit: 2000/1000 MS (Java/Oth ...

  8. POJ - 2421 Constructing Roads (最小生成树)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  9. HDU1102 Constructing Roads —— 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...

随机推荐

  1. JAVA内存模型及垃圾回收自我总结

    本文为原创,根据<深入理解java虚拟机>和自己的一些理解进行整理,单纯和看其他人的博客感觉不如自己一点点的画和记录来的印象深刻. JAVA内存模型: 上图中:局部变量表所需的内存在编译期 ...

  2. 10、java5线程池之返回结果的任务之Callable与Future

    JDK文档描述Callable: public interface Callable<V>返回结果并且可能抛出异常的任务.实现者定义了一个不带任何参数的叫做 call 的方法. Calla ...

  3. quartz.net 的配置文件资料

    java版本的文档比较全 http://www.quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigPlugins.ht ...

  4. Ubuntu18.04下使用Blender进行视频格式转换

    Ubuntu下可以使用Blender的Video Editing功能进行视频格式转换, 具体步骤: 打开Blender后, 在顶层菜单栏中, 将Choose Screen Layout修改为Video ...

  5. 从html加载json文件想起

    原文来自:https://www.cnblogs.com/dibaosong/p/4572274.html#top 文中给出了data.json文件内容 还给出了html文件内容 ok. 1.新建工程 ...

  6. ajax请求格式

    ajax请求格式........... var rowsData = $('#receiptPrintList').datagrid('getSelections'); $.ajax({ type: ...

  7. 【LeetCode】Binary Tree Upside Down

    Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...

  8. apache配置中ProxyPassReverse指令的含义

    apache中的mod_proxy模块主要作用就是进行url的转发,即具有代理的功能.应用此功能,可以很方便的实现同tomcat等应用服务器的整合,甚者可以很方便的实现web集群的功能. 例如使用ap ...

  9. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  10. linux文件系统 - 初始化(一)

    术语表: struct task:进程 struct mnt_namespace:命名空间 struct mount:挂载点 struct vfsmount:挂载项 struct file:文件 st ...