题意:有几个村庄,要修最短的路,使得这几个村庄连通。但是现在已经有了几条路,求在已有路径上还要修至少多长的路。

分析:用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 最小生成树)的更多相关文章

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

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  2. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

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

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

  4. POJ - 2421 Constructing Roads(最小生成树&并查集

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

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

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

  6. poj 2421 Constructing Roads 解题报告

    题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...

  7. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  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. [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads

    给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...

随机推荐

  1. 跨平台移动开发_Windows 8平台使用 PhoneGap 方法

    原文地址: Using PhoneGap in Windows 8 Store Applications 下载phonegap 2.9.1 下载地址: https://codeload.github. ...

  2. JSP--常用指令

    1.JSP中的page指令: jsp中指令格式:<%@   指令名字    key=value    key=value   ......%> <%@ page language=& ...

  3. tomcat添加登录用户名密码

    tomcat版本 apache-tomcat-7.0.55.tar.gz 编辑 TOMCAT_HOME/conf/tomcat-users.xml在tomcat-users里面添加 <tomca ...

  4. Variation calling and annotation

    Resequencing 302 wild and cultivated accessions identifies genes related to domestication and improv ...

  5. jq点击切换按钮最简洁代码

    <div id="swphoto">    <img src="1.jpg">    <img src="2.jpg&q ...

  6. java基础(2)-面向对象(2)

    构造方法 构造方法特点 方法名与类名相同 方法名前没有返回值类型的声明(void也没有) 方法中不能使用return语句返回一个值 创建对象时自动调用并执行 如果类中没有自定义构造方法,则java调用 ...

  7. Flume-NG启动过程源码分析(一)(原创)

    从bin/flume 这个shell脚本可以看到Flume的起始于org.apache.flume.node.Application类,这是flume的main函数所在. main方法首先会先解析sh ...

  8. redis.h

    [对象] typedef struct redisObject {     unsigned type:4;[REDIS_STRING,REDIS_LIST, HASH, SET, ZSET]     ...

  9. python如何获取多个excel单元格的值

    一. 获取多个单元格的值报错:AttributeError: 'tuple' object has no attribute 'value' 需要读取的sample.xlsx 代码读取的是A3:B10 ...

  10. 条款42:了解typename的双重含义

    typename在很多种情况下与class是完全相同的,例如下面的使用: templame<typename T> ...... template<class T> ..... ...