Minimal network

The following undirected network consists of seven vertices and twelve edges with a total weight of 243.

The same network can be represented by the matrix below.

  A B C D E F G
A - 16 12 21 - - -  
B 16 - - 17 20 - -  
C 12 - - 28 - 31 -  
D 21 17 28 - 18 19 23  
E - 20 - 18 - - 11  
F - - 31 19 - - 27  
G - - - 23 11 27 -  

However, it is possible to optimise the network by removing some edges and still ensure that all points on the network remain connected. The network which achieves the maximum saving is shown below. It has a weight of 93, representing a saving of 243 ? 93 = 150 from the original network.

Using network.txt (right click and ‘Save Link/Target As…’), a 6K text file containing a network with forty vertices, and given in matrix form, find the maximum saving which can be achieved by removing redundant edges whilst ensuring that the network remains connected.


最小网络

下面这个无向网络包含有7个顶点和12条边,其总重量为243。

这个网络也可以用矩阵的形式表示如下。

  A B C D E F G
A - 16 12 21 - - -  
B 16 - - 17 20 - -  
C 12 - - 28 - 31 -  
D 21 17 28 - 18 19 23  
E - 20 - 18 - - 11  
F - - 31 19 - - 27  
G - - - 23 11 27 -  

然而,我们其实可以优化这个网络,移除其中的一些边,同时仍然保证每个顶点之间都是连通的。节省重量最多的网络如下图所示,其总重量为93,相比原来的网络节省了243 ? 93 = 150。

在这个6K的文本文件network.txt(右击并选择“目标另存为……”)中存放了一个包含有40个顶点的网络的连通矩阵。移除其中冗余的边,同时仍然保证每个顶点之间都是连通的,求最多能节省的重量。

解题

Prim算法 或者 kruskal 算法

Java

package Level4;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeMap; public class PE0107{
static int[][] network = new int[40][40];
static int len = 40;
static int AllSum=0;
static int shortestSum =0;
public static void run(){
String filename = "src/Level4/p107_network.txt";
readData(filename);
AllSum = getArrSum();
System.out.println("所有元素的和:"+AllSum);
shortestSum = Prim();
System.out.println("最短路径的和:"+shortestSum);
System.out.println("路径之差:"+ (AllSum - shortestSum));
}
public static int Prim(){
ArrayList<Integer> known = new ArrayList<Integer>();
ArrayList<Integer> goods = new ArrayList<Integer>();
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
known.add(0);
map.put(0, 0);
while(true){
int min = Integer.MAX_VALUE;
int index = -1;
int mapindex = -1;
for(int i=0;i<known.size();i++){
for(int j=0;j< len;j++){
int now = network[map.get(i)][j];
if(now < min && now!=-1 && !known.contains(j)){
min = now;
index = j;
mapindex = known.size();
}
}
}
goods.add(min);
known.add(index);
map.put(mapindex,index);
if( known.size() == len)
break;
}
int sum =0;
for(int i=0;i<goods.size() ;i++){
sum+=goods.get(i);
}
return sum;
}
public static int getArrSum(){
int sum = 0;
for(int i=0;i<network.length;i++){
for(int j=i+1;j<network[0].length;j++){
if(network[i][j]!=-1)
sum +=network[i][j];
}
}
return sum;
}
public static void StrToArr(int index,String line){
String[] strArr = line.split(","); for(int i=0;i<strArr.length;i++){
if(!strArr[i].equals("-")){
network[index][i] = Integer.parseInt(strArr[i]);
}else{
network[index][i] = -1;
}
}
}
public static void readData(String filename){
BufferedReader bufferedReader;
int index = 0;
try {
bufferedReader = new BufferedReader(new FileReader(filename));
String line ="";
try {
while((line = bufferedReader.readLine())!=null){
StrToArr(index,line);
index++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件没有数据");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("没有发现文件");
}
}
public static void main(String[] args){
long t0 = System.currentTimeMillis();
run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms");
}
}

Project Euler 107:Minimal network 最小网络的更多相关文章

  1. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  2. Python练习题 040:Project Euler 012:有超过500个因子的三角形数

    本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...

  3. Python练习题 037:Project Euler 009:毕达哥拉斯三元组之乘积

    本题来自 Project Euler 第9题:https://projecteuler.net/problem=9 # Project Euler: Problem 9: Special Pythag ...

  4. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  5. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  6. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  7. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  8. POJ3436 Command Network [最小树形图]

    POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...

  9. 自学Zabbix9.1 Network Discovery 网络发现原理

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix9.1 Network Discovery 网络发现原理 1. 网络发现简介 网络 ...

随机推荐

  1. [转]coredump简介与coredump原因总结

    [转]coredump简介与coredump原因总结 http://blog.sina.com.cn/s/blog_54f82cc201013srb.html 什么是coredump? 通常情况下co ...

  2. .net 使用validator做数据校验

    概述 在把用户输入的数据存储到数据库之前一般都要对数据做服务端校验,于是想到了.net自带的数据校验框架validator.本文对validator的使用方法进行介绍,并分析下校验的的原理. 使用va ...

  3. [SSH服务]——一些安全性配置和补充实验

    SSH 安全性和配置 转载于 http://www.ibm.com/developerworks/cn/aix/library/au-sshsecurity/ 对于一些之前列举的代码示例,许多系统管理 ...

  4. HTML5就是现在:深入了解Polyfills

    http://blog.csdn.net/wang16510/article/details/8960312 https://github.com/Modernizr/Modernizr/wiki/H ...

  5. c语言编写的日历

    输入年份如2013,显示2013年的日历. 思路: 1.查找每个月1号是星期几(这里利用了1990年1月1号是星期一) 计算年份如2013年1月1号到1990年1月1号有Days天,Day%7得到星期 ...

  6. eclipse for jee版配置tomcat

    在网上搜到的大多都是插件配置,其实默认的就可以配置tomcat的. 第一步:New -> Other -> Server ,然后选择Apache下的tomcat的版本. 注意:如果Next ...

  7. android开发,设置listview的高度无效

    一般是在item的layout中设置高度 android:layout_height="100dp" 但是发现这样后无效,因此找到解决办法,如下: android:minHeigh ...

  8. Ming Rpc

    原文地址:http://iwantmoon.com/Post/487ab43d609f49d28ff4228241e2b7c7 Rpc(Remote Procedure Call Protocal)远 ...

  9. WPS for ubuntu14

    QGtkStyle could not resolve GTK. Make sure you have installed the proper libraries. sudo apt-get ins ...

  10. 以前用过Extjs技术的开发人员在学习Extjs4时需要注意的问题

            以前学习过Extjs的同学,在学习Extjs4的时候需要注意几个关键改变: 1.Extjs4的新的类系统. 2.Extjs4中MVC思路 3.Extjs4中的新的命名规范(结合新的MV ...