Project Euler 107:Minimal network 最小网络
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 最小网络的更多相关文章
- 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 ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- Python练习题 037:Project Euler 009:毕达哥拉斯三元组之乘积
本题来自 Project Euler 第9题:https://projecteuler.net/problem=9 # Project Euler: Problem 9: Special Pythag ...
- Python练习题 033:Project Euler 005:最小公倍数
本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- POJ3436 Command Network [最小树形图]
POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...
- 自学Zabbix9.1 Network Discovery 网络发现原理
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix9.1 Network Discovery 网络发现原理 1. 网络发现简介 网络 ...
随机推荐
- extjs 表格可复制
在GridPanel的配置项中,加入这个配置就可以了: viewConfig:{ enableTextSelection:true }
- CoreLocation简单应用
1.获取locationManager let locationManager: CLLocationManager = CLLocationManager() 2.设置locationManager ...
- 与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。
今天同学请教我数据库为什么打不开了,打开SQL Server 2008 的 SQL Server Management Studio,输入sa的密码发现,无法登陆数据库?提示以下错误: "在 ...
- java数据结构和算法------快速排序
package iYou.neugle.sort; public class Quick_sort { public static void QuickSort(double[] array, int ...
- PID
http://blog.gkong.com/liaochangchu_117560.ashx
- Python实现DBScan
Python实现DBScan 运行环境 Pyhton3 numpy(科学计算包) matplotlib(画图所需,不画图可不必) 计算过程 st=>start: 开始 e=>end: 结束 ...
- JAVA数据结构-----枚举
枚举(Enumeration)接口虽然它本身不属于数据结构,但它在其他数据结构的范畴里应用很广. 枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式. 例如,枚举定义了 ...
- [LAMP]【转载】——PHP7.0的安装
***原文链接:http://my.oschina.net/sallency/blog/541287 php编译过程报错解决可参考:http://www.cnblogs.com/z-ping/arch ...
- C# 微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- java获取常见文本文件的编码 解决乱码问题
乱码问题的产生一般是,由字节流转字符流的时候,读文件的编码与文件的系统编码不一致造成的. 解决方式:先自动判断文件系统编码类型,然后读的时候用这个类型去读就ok了. 自动判断文件系统编码类型代码如下, ...