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 ...
随机推荐
- linux 无密码登录
环境:Linux 脚本:Python 功能:批量IP,远程执行命令.拷贝文件 运行:./ssh_scp.py iplist.txt 脚本内容: #!/usr/bin/env python# -*- c ...
- 关于form表单中button按钮自动提交问题
坑:点击确认按钮,form表单提交2次,发送后台2次请求 //错误代码: <Button id="btnSubmit" name="btnSubmit" ...
- Java I/O 小结
主要内容: 一.输入流基类:InputStream 和 OutputStream(字节流). Reader 和 Writer(字符流) 二.文件字节流:FileInputStream和FileOutp ...
- Proxy动态代理
Proxy动态代理 package com.test.dynamicproxy; public interface Subject { public void request(); } package ...
- QT5中如何使用QFtp类
QT5中如何使用QFtp类 http://2662597.blog.51cto.com/2652597/1279806 由于QT5对QML的支持有很大的改进,所以打算将原来基于QT4的程序移植到QT5 ...
- 【转载】postgreSQL在linux中安装详解
.编译环境 Linux: CentOS 5.5 gcc: 4.1.2 1. 安装PostgreSQL 1) 解压postgresql-9.1.7.tar.bz2 #tar jxvf postgresq ...
- html5学习笔记(audio)
来源于<HTML5高级程序设计> audio api <audio controls> controls告诉浏览器显示播放控件 不指定 type 浏览器自解 oggMP3 ty ...
- 262. Trips and Users
问题描述 解决方案 -- case when 的效率比if的效率高 -- select Trips.Request_at as 'Day', -- round(sum(case Trips.Statu ...
- 如何激活Windows10系统
Win10正式企业版系统的激活方法: 按住 win+x 就会出现如下,右击桌面的左下角的“Windows”图标,从其右键菜单中选择“命令提示符(管理员)”项,以便打开 MSDOS界面. 待打开MS ...
- sql生成excel
gosp_configure 'show advanced options',1reconfiguregosp_configure 'xp_cmdshell',1reconfiguregoEXEC m ...