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. 网络发现简介 网络 ...
随机推荐
- 使用 PHP cURL 提交 JSON 数据
http://www.oschina.net/code/snippet_54100_7351 http://www.lornajane.net/posts/2011/posting-json-data ...
- scjp考试准备 - 5 - 重载和重写
如下代码,在所指示的位置插入代码能够正常编译: class Alpha{ public void bar(int... x){}; public void bar(int x){}; } public ...
- ExtJS 提示
要使ExtJS支持提示,需要在onReady的function中添加如下语句: Ext.QuickTips.init();//支持tips提示 Ext.form.Field.prototype.msg ...
- UIView 添加子视图的常用方法
1. - (void)addSubview:(UIView *)view 这是最常用的方法有两个注意点 参数view可以是nil,运行不会报错,当然,父视图的subViews也不会增加. 此方法增加 ...
- 巧用 .NET 中的「合并运算符」获得 URL 中的参数
获取 URL 中的 GET 参数,无论用什么语言开发网站,几乎是必须会用到的代码.但获取 URL 参数经常需要注意一点就是要先判断是否有这个参数存在,如果存在则取出,如果不存在则用另一个值.这个运算称 ...
- foxmail收发gmail彻底失败
周一一上班,发现gmail无法收取邮件,刚开始以为网络不稳定,后来经过百度发现原因是 gmail邮箱也被屏蔽了. 虽然可以FQ,保证gmail邮箱暂时使用,但是不可否认,在当前的形势下, ...
- WebServices中Xml的序列化
一.定义序列化实体类 [System.Xml.Serialization.XmlRoot("Custome_Xml_Root_Name")] //自定义生成的Xml根目录名称 pu ...
- 01.Apache FtpServer配置
1.解压Apache FTPServer 将下载下来的压缩包(ftpserver-1.0.6.zip)解压到本地,其目录结构如下图: 2.修改users.properties 修改 \apache-f ...
- netty 粘包问题处理
netty 粘包问题处理 key words: netty 粘包 解包 半包 TCP 一般TCP粘包/拆包解决办法 定长消息,例如每个报文长度固定,不够补空格 使用回车换行符分割,在包尾加上分割符,例 ...
- Facebook 和 Google 如何激发工程师的创造力
原文链接:http://kb.cnblogs.com/page/193450/ 今天终于“朝圣”了两个伟大的公司——Facebook和Google,对创造力和驱动力的来源有了更多的理解,尤其是对于典型 ...