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 ...
随机推荐
- Docker容器技术-在开发中引用Docker
明确一点: 容器不适合构建那种发布周期以周或月为单位的大型单一架构企业软件,容器适合采用微服务的方式,以及探索诸如持续部署这样的技术,使得我们能安全地在一天内多次更新生产环境. 一.在开发中引用Doc ...
- 主攻ASP.NET MVC4.0之重生:使用反射获取Controller的ActionResult
示例代码 public ActionResult TypeOfForName() { Type typeinfo = typeof(CustomerClassController); //typeof ...
- 跨平台移动开发 Xuijs超轻量级的框架+Emile CSS动画
Xuijs超轻量级的框架+Emile CSS动画效果图 示例代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...
- 20145231《Java程序设计》第四次实验报告
实验四 Android开发基础 实验内容 •安装Android Studio •运行安卓AVD模拟器 •使用Android运行出模拟手机并显示自己的学号 实验步骤 一.安装Android Studio ...
- NorFlash、NandFlash、eMMC比较区别【转】
本文转载自:http://www.veryarm.com/1200.html 快闪存储器(英语:Flash Memory),是一种电子式可清除程序化只读存储器的形式,允许在操作中被多次擦或写的存储器. ...
- avaweb学习总结(八)——HttpServletResponse对象(二)
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,
- poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...
- poj2442优先队列
感谢 http://hi.baidu.com/%C0%B6%C9%ABarch/blog/item/f9d343f49cd92e53d7887d73.html 的博主! 思路: 我们要找到n个smal ...
- 【bzoj1899】[Zjoi2004]Lunch 午餐(贪心+dp)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1899 显然为了节省时间,吃饭慢的人要先打饭.于是我们可以先把所有人按吃饭时间排序,于是 ...
- SVG_style_script
1. <style type="text/css"> <![CDATA[ // ZC: 禁止所有 <text/>元素的选中 text { -webki ...