Poj 2421 Constructing Roads(Prim 最小生成树)
题意:有几个村庄,要修最短的路,使得这几个村庄连通。但是现在已经有了几条路,求在已有路径上还要修至少多长的路。
分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被Prim选中加入最小生成树。
package Map; import java.util.Scanner; /**
* Prime
*/
public class Poj_2421_Prim { static int MAXVEX = 200;
static int n, m;
static int[][] arc = new int[MAXVEX][MAXVEX];
static int visited[] = new int[MAXVEX];//判断是否加入生成树 public static int prime() { int min, i, j, k, sum = 0;
visited[1] = 1; for (i = 2; i <= n; i++) {
min = 1000000;
k = 0;
for (j = 1; j <= n; j++) {
if (visited[j] == 0 && arc[1][j] < min) {
min = arc[1][j];
k = j;
}
} sum += min;
visited[k] = 1;
for (j = 1; j <= n; j++) {
if (visited[j] == 0 && arc[1][j] > arc[k][j]) {
arc[1][j] = arc[k][j];
}
}
}
return sum;
} public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
arc[i][j] = sc.nextInt();
}
arc[i][i] = 1000000;
} m = sc.nextInt(); //如果路径存在,则置为0.这样
for (int i = 1; i <= m; i++) {
int s = sc.nextInt();
int e = sc.nextInt();
arc[s][e] = 0;
arc[e][s] = 0;
} System.out.println(prime());
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Poj 2421 Constructing Roads(Prim 最小生成树)的更多相关文章
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- POJ - 2421 Constructing Roads(最小生成树&并查集
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj 2421 Constructing Roads 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
随机推荐
- 正则表达式----grep
正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. grep [参数] 匹配条件 路径 参数 : -n :显示行号 ...
- Android中设置自己软件的铃声+震动
有时候一些通讯软件需要这些个功能,比如说收到短信,通知等,要求手机发出铃声,或震动,或发光以提示用户知晓. 往往手机都是有默认设置的,比如说用户开启了铃声+震动:只铃声不震动:完全静音等等... 这个 ...
- mongodb 的安装(Centor OS )
1.下载地址 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.2.tgz 2.解压.配置 tar zxvf mongodb ...
- [转载]ORA-00313:无法打开日志组1(线程 1)的成员_ORA-00312:
原文地址:1)的成员_ORA-00312:">ORA-00313:无法打开日志组1(线程 1)的成员_ORA-00312:作者:Sweet_薇薇毅 今天用系统清理工具把系统垃圾清理了一 ...
- [转载]Spring配置文件详解一:
原文地址:与base-package="com.xx">Spring配置文件详解一:<context:annotation-config/>与<contex ...
- Spring Boot入门——tomcat配置
1.通过配置文件配置 server.port = 8080 2.通过程序配置 import org.springframework.boot.SpringApplication; import org ...
- JAVA设计模式初探之组合模式
先看看组合模式的定义吧:“将对象组合成树形结构以表示‘部分-整体’的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性.” 就拿剪发办卡的事情来分析一下吧. 首先,一张卡可以在总部,分店, ...
- Secret Code
Secret Code 一.题目 [NOIP模拟赛A10]Secret Code 时间限制: 1 Sec 内存限制: 128 MB 提交: 10 解决: 6 [提交][状态][讨论版] 题目描述 ...
- 【转】Pearson,Spearman,Kendall相关系数的具体分析
测量相关程度的相关系数很多,各种参数的计算方法及特点各异. 连续变量的相关指标: 此时一般用积差相关系数,又称pearson相关系数来表示其相关性的大小,积差相关系数只适用于两变量呈线性相关时.其数值 ...
- 2016-02-20WebForm登陆验证,判断用户是否登陆 PageBase类
http://blog.csdn.net/fanbin168/article/details/49404233 很多时候,WebFrom页面,我们需要判断用户是否已经登陆了.假如有很多页面,难道我们要 ...