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. webservice soapclient报错Error fetching http headers

    设置: ini_set('default_socket_timeout', 600); // or whatever new value you want 参考:http://stackoverflo ...

  2. 011--VS2013 C++ 斜角地图贴图

    准备好的图片 //全局变量HDC mdc;HBITMAP fullmap;//声明位图对象,在初始化函数中完成的斜角地图会保存在这个位图中const int rows = 10, cols = 10; ...

  3. Mono for Android (2)-- Android应用程序初认识

    一:日志记录 先添加using Android.Util; 在该命名控件下有log类 Log.Info("HA", "End onCreate"); //记录消 ...

  4. 【上传AppStore】iOS项目上传到AppStore步骤流程(第三章) - 基本信息总汇

    一.App ID(bundle identifier) App ID即Product ID,用于标识一个或者一组App. App ID应该和Xcode中的Bundle Identifier是一致(Ex ...

  5. .net sql 防注入 httpmodule

    1 新建一个类,实现IHttpModule接口 using System; using System.Collections.Generic; using System.Linq; using Sys ...

  6. 使用getElementById获取xml中的指定元素

    Document有一个getElementById的方法,在文档中的解释是:     返回具有带给定值的 ID 属性的 Element.如果不存在此类元素,则此方法返回 null.如果多个元素具有带该 ...

  7. Python2.x与3​​.x版本区别

    Python的3​​.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级. 为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下相容 ...

  8. yii的常用配置文件

    <?php return array( 'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..', //当前应用根目录路径 'nam ...

  9. vmware workstation 10.0

    2013.9.3 vmware workstation 10.0 build 1295980新增功能– 可以将windows 8.1物理pc转变为虚拟机:unity模式增强,与windows 8.1 ...

  10. JAVA Hashmap不能用基本的数据类型

    今天开始学习Java... 转载:http://moto0421.iteye.com/blog/1143777 今天试了一下HahsMap, 采用如下形似定义 (这个下面是用了csdn的一位同仁的文章 ...