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 ...
随机推荐
- new Date(dateString)
xxxx-xx-xx xx:xx:xx chrome firefox opera xxxx/xx/xx xx:xx:xx chrome firefox opera safari ios(苹果手机只认此 ...
- 20145230《java学习笔记》第七周学习总结
20145230 <Java程序设计>第7周学习总结 教材学习内容 Lambda语法概览 我们在许多地方都会有按字符串长度排序的需求,如果在同一个方法内,我们可以使用一个byName局部变 ...
- JavaWeb Request和Response
1. Request与Response 1.1. Web应用运行机制 到目前为止,我们已经掌握了Web应用程序的运行机制,现在学习的就是Web应用程序运行机制中很重要的内容 —— Request与Re ...
- poj 1265 Area【计算几何:叉积计算多边形面积+pick定理计算多边形内点数+计算多边形边上点数】
题目:http://poj.org/problem?id=1265 Sample Input 2 4 1 0 0 1 -1 0 0 -1 7 5 0 1 3 -2 2 -1 0 0 -3 -3 1 0 ...
- Windows batch: call more than one command in a FOR loop?
https://stackoverflow.com/questions/2252979/windows-batch-call-more-than-one-command-in-a-for-loop U ...
- js运算符、关键字、保留字、转义字符
- alibaba的JSON.toString会把值为null的字段去掉,谨记
alibaba的JSON.toString会把值为null的字段去掉,谨记 Map<String,Object> map = new HashMap<>(); map.put( ...
- FreeTDS-SQL Server在linux和unix下的免费驱动
微软为MS SQL Server的连接和使用提供了很好的 驱动和 文档. 不幸的是,那只能在windows操作系统上使用. 所以对于Linux或者Unix,您需要寻找不同的方法来连接MS SQL Se ...
- 《Advanced Bash-scripting Guide》学习(八):从一个目录移动整个目录树到另一个目录
本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 ABS书上的例子: 从一个目录移动整个目录树到另一个目录 #!/bin/bash ...
- Maven下载 || 配置本地仓库 || IntelliJ IDEA配置Maven教程
本文章主要介绍1.Maven下载 2.配置本地仓库Repository 3.IDEA配置Maven 三点. 相关博客: Eclipse配置Maven https://www.cnblogs.c ...