- 浏览: 24203 次
- 性别:
-
最新评论
-
夜CT:
英语不好......这是什么意思啊?
Ruby on Rails Routing - Simple Examples -
may_cauc:
楼主怎么解决这个问题的?我也出现了
ROR新手请教连mysql数据库的问题. -
xiaoqiu369:
清空了缓存还是不行.
请教一个问题,为什么我修改了样式表文件public/stylesheets/scaffold.css,在页面上没有变化. -
ddandyy:
清空浏览器缓存
请教一个问题,为什么我修改了样式表文件public/stylesheets/scaffold.css,在页面上没有变化.
1 树与哈夫曼树
Java代码
package tree;
public class TreeNode {
TreeNode llink;
TreeNode rlink;
int info;
}
package tree;
public class Tree {
TreeNode root;
public Tree() {
}
public boolean isEmpty() {
return root == null;
}
// 插入
public void insertBinaryTree(int info) {
TreeNode node = new TreeNode();
node.info = info;
if (root == null) {
root = node;
} else {
TreeNode currentNode = root;
TreeNode parent;
while (currentNode != null) {
parent = currentNode;
if (info > currentNode.info) {
currentNode = currentNode.rlink;
if (currentNode == null) {
parent.rlink = node;
}
} else if (info < currentNode.info) {
currentNode = currentNode.llink;
if (currentNode == null) {
parent.llink = node;
}
}
}
}
}
// 删除
public void treeDelete(int info) {
TreeNode tNode = serach(info);
TreeNode deleteNode = new TreeNode();
TreeNode dNodeChild = new TreeNode();
if (tNode == null) {
System.out.println("node is not exists");
} else if (tNode.llink == null || tNode.rlink == null) {
deleteNode = tNode;
} else {
deleteNode = treeSuccessor(tNode);
}
if (deleteNode.llink != null) {
dNodeChild = deleteNode.llink;
} else {
dNodeChild = deleteNode.rlink;
}
if (getFatherNode(deleteNode) == null) {
root = dNodeChild;
} else {
if (deleteNode == getFatherNode(deleteNode).llink) {
getFatherNode(deleteNode).llink = dNodeChild;
} else {
getFatherNode(deleteNode).rlink = dNodeChild;
}
}
if (deleteNode != tNode) {
tNode.info = deleteNode.info;
}
}
// 搜索
public TreeNode serach(int info) {
return treeSerach(root, info);
}
// 搜索
public TreeNode treeSerach(TreeNode tNode, int info) {
if (tNode == null || tNode.info == info) {
return tNode;
}
if (info < tNode.info) {
return treeSerach(tNode.llink, info);
} else {
return treeSerach(tNode.rlink, info);
}
}
// 最大节点
public TreeNode treeMaxiMum(TreeNode tNode) {
while (tNode.rlink != null) {
tNode = tNode.rlink;
}
return tNode;
}
// 最小节点
public TreeNode treeMiniMun(TreeNode tNode) {
while (tNode.llink != null) {
tNode = tNode.llink;
}
return tNode;
}
// 找后继
public TreeNode treeSuccessor(TreeNode tNode) {
if (tNode.rlink != null) {
return treeMiniMun(tNode.rlink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.rlink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找前驱
public TreeNode treePrecursor(TreeNode tNode) {
if (tNode.llink != null) {
return treeMaxiMum(tNode.llink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.llink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找节点的父节点
public TreeNode getFatherNode(TreeNode tNode) {
TreeNode current = root;
TreeNode trailCurrent = null;
while (current != null) {
if (current.info == tNode.info) {
break;
}
trailCurrent = current;
if (tNode.info < current.info) {
current = current.llink;
} else {
current = current.rlink;
}
}
return trailCurrent;
}
// 中序遍历
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
// 打印树
public void printTree() {
System.out.println("inOrder");
inOrder(root);
System.out.println();
System.out.println("preOrder");
preOrder(root);
System.out.println();
System.out.println("postOrder");
postOrder(root);
System.out.println();
}
// 前序遍历
public void preOrder(TreeNode tNode) {
if (tNode != null) {
System.out.print(tNode.info + " ");
preOrder(tNode.llink);
preOrder(tNode.rlink);
}
}
// 后序遍历
public void postOrder(TreeNode tNode) {
if (tNode != null) {
postOrder(tNode.llink);
postOrder(tNode.rlink);
System.out.print(tNode.info + " ");
}
}
public static void main(String[] args) {
Tree tree = new Tree();
System.out.println("insert tree start");
tree.insertBinaryTree(15);
tree.insertBinaryTree(5);
tree.insertBinaryTree(16);
tree.insertBinaryTree(3);
tree.insertBinaryTree(12);
tree.insertBinaryTree(20);
tree.insertBinaryTree(10);
tree.insertBinaryTree(13);
tree.insertBinaryTree(18);
tree.insertBinaryTree(23);
tree.insertBinaryTree(6);
tree.insertBinaryTree(7);
System.out.println("insert tree end");
System.out.println("print tree start");
tree.treeDelete(15);
tree.printTree();
System.out.println("print tree end");
}
}
package tree;
public class TreeNode {
TreeNode llink;
TreeNode rlink;
int info;
}
package tree;
public class Tree {
TreeNode root;
public Tree() {
}
public boolean isEmpty() {
return root == null;
}
// 插入
public void insertBinaryTree(int info) {
TreeNode node = new TreeNode();
node.info = info;
if (root == null) {
root = node;
} else {
TreeNode currentNode = root;
TreeNode parent;
while (currentNode != null) {
parent = currentNode;
if (info > currentNode.info) {
currentNode = currentNode.rlink;
if (currentNode == null) {
parent.rlink = node;
}
} else if (info < currentNode.info) {
currentNode = currentNode.llink;
if (currentNode == null) {
parent.llink = node;
}
}
}
}
}
// 删除
public void treeDelete(int info) {
TreeNode tNode = serach(info);
TreeNode deleteNode = new TreeNode();
TreeNode dNodeChild = new TreeNode();
if (tNode == null) {
System.out.println("node is not exists");
} else if (tNode.llink == null || tNode.rlink == null) {
deleteNode = tNode;
} else {
deleteNode = treeSuccessor(tNode);
}
if (deleteNode.llink != null) {
dNodeChild = deleteNode.llink;
} else {
dNodeChild = deleteNode.rlink;
}
if (getFatherNode(deleteNode) == null) {
root = dNodeChild;
} else {
if (deleteNode == getFatherNode(deleteNode).llink) {
getFatherNode(deleteNode).llink = dNodeChild;
} else {
getFatherNode(deleteNode).rlink = dNodeChild;
}
}
if (deleteNode != tNode) {
tNode.info = deleteNode.info;
}
}
// 搜索
public TreeNode serach(int info) {
return treeSerach(root, info);
}
// 搜索
public TreeNode treeSerach(TreeNode tNode, int info) {
if (tNode == null || tNode.info == info) {
return tNode;
}
if (info < tNode.info) {
return treeSerach(tNode.llink, info);
} else {
return treeSerach(tNode.rlink, info);
}
}
// 最大节点
public TreeNode treeMaxiMum(TreeNode tNode) {
while (tNode.rlink != null) {
tNode = tNode.rlink;
}
return tNode;
}
// 最小节点
public TreeNode treeMiniMun(TreeNode tNode) {
while (tNode.llink != null) {
tNode = tNode.llink;
}
return tNode;
}
// 找后继
public TreeNode treeSuccessor(TreeNode tNode) {
if (tNode.rlink != null) {
return treeMiniMun(tNode.rlink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.rlink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找前驱
public TreeNode treePrecursor(TreeNode tNode) {
if (tNode.llink != null) {
return treeMaxiMum(tNode.llink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.llink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找节点的父节点
public TreeNode getFatherNode(TreeNode tNode) {
TreeNode current = root;
TreeNode trailCurrent = null;
while (current != null) {
if (current.info == tNode.info) {
break;
}
trailCurrent = current;
if (tNode.info < current.info) {
current = current.llink;
} else {
current = current.rlink;
}
}
return trailCurrent;
}
// 中序遍历
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
// 打印树
public void printTree() {
System.out.println("inOrder");
inOrder(root);
System.out.println();
System.out.println("preOrder");
preOrder(root);
System.out.println();
System.out.println("postOrder");
postOrder(root);
System.out.println();
}
// 前序遍历
public void preOrder(TreeNode tNode) {
if (tNode != null) {
System.out.print(tNode.info + " ");
preOrder(tNode.llink);
preOrder(tNode.rlink);
}
}
// 后序遍历
public void postOrder(TreeNode tNode) {
if (tNode != null) {
postOrder(tNode.llink);
postOrder(tNode.rlink);
System.out.print(tNode.info + " ");
}
}
public static void main(String[] args) {
Tree tree = new Tree();
System.out.println("insert tree start");
tree.insertBinaryTree(15);
tree.insertBinaryTree(5);
tree.insertBinaryTree(16);
tree.insertBinaryTree(3);
tree.insertBinaryTree(12);
tree.insertBinaryTree(20);
tree.insertBinaryTree(10);
tree.insertBinaryTree(13);
tree.insertBinaryTree(18);
tree.insertBinaryTree(23);
tree.insertBinaryTree(6);
tree.insertBinaryTree(7);
System.out.println("insert tree end");
System.out.println("print tree start");
tree.treeDelete(15);
tree.printTree();
System.out.println("print tree end");
}
}
Java代码
package tree;
/**
* @author B.Chen
*/
public class HuffmanTree {
/**
* 根节点
*/
public TreeNode root;
/**
* 数组下表
*/
public int nodeSize;
/**
* 长度
*/
public int length;
/**
* 哈夫曼节点数组
*/
public TreeNode[] nodeList;
/**
* 访问控制
*/
public boolean[] visited;
/**
* @param length
*/
public HuffmanTree(int length) {
this.length = length;
nodeList = new TreeNode[2 * length-1];
visited = new boolean[2*length-1];
}
/**
* @param info
*/
public void addNode(int info) {
TreeNode tNode = new TreeNode();
tNode.info = info;
if (nodeSize < length) {
nodeList[nodeSize++] = tNode;
} else {
System.out.println("out of size");
}
}
/**
* 构造哈夫曼树
*/
public void createHuffmanTree() {
int j = length;
while (nodeList[2*length -2] == null) {
TreeNode lNode = getMiniMumNode();
TreeNode rNode = getMiniMumNode();
TreeNode tNode = new TreeNode();
System.out.println(lNode.info + " " + rNode.info);
tNode.llink = lNode;
tNode.rlink = rNode;
tNode.info = lNode.info + rNode.info;
nodeList[j++] = tNode;
}
root = nodeList[2 * length - 2];
}
/**
* @return TreeNode
*/
public TreeNode getMiniMumNode() {
TreeNode tNode = null;
int size = 0;
for(int i=0;i<2*length-1;i++) {
if(!visited[i] && nodeList[i] != null) {
tNode = nodeList[i];
size = i;
for(int j=0;j<2*length-1;j++) {
if(!visited[j] && nodeList[j] != null) {
if(tNode.info > nodeList[j].info) {
tNode = nodeList[j];
size = j;
}
}
}
}
}
visited[size] = true;
return tNode;
}
/**
* 中序遍历
*
* @param tNode
*/
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
/**
* 打印
*/
public void printHuffmanTree() {
System.out.println("inOrder");
inOrder(root);
}
/**
* @param args
*/
public static void main(String[] args) {
HuffmanTree hTree = new HuffmanTree(6);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(5);
hTree.addNode(6);
hTree.createHuffmanTree();
hTree.printHuffmanTree();
}
}
package tree;
/**
* @author B.Chen
*/
public class HuffmanTree {
/**
* 根节点
*/
public TreeNode root;
/**
* 数组下表
*/
public int nodeSize;
/**
* 长度
*/
public int length;
/**
* 哈夫曼节点数组
*/
public TreeNode[] nodeList;
/**
* 访问控制
*/
public boolean[] visited;
/**
* @param length
*/
public HuffmanTree(int length) {
this.length = length;
nodeList = new TreeNode[2 * length-1];
visited = new boolean[2*length-1];
}
/**
* @param info
*/
public void addNode(int info) {
TreeNode tNode = new TreeNode();
tNode.info = info;
if (nodeSize < length) {
nodeList[nodeSize++] = tNode;
} else {
System.out.println("out of size");
}
}
/**
* 构造哈夫曼树
*/
public void createHuffmanTree() {
int j = length;
while (nodeList[2*length -2] == null) {
TreeNode lNode = getMiniMumNode();
TreeNode rNode = getMiniMumNode();
TreeNode tNode = new TreeNode();
System.out.println(lNode.info + " " + rNode.info);
tNode.llink = lNode;
tNode.rlink = rNode;
tNode.info = lNode.info + rNode.info;
nodeList[j++] = tNode;
}
root = nodeList[2 * length - 2];
}
/**
* @return TreeNode
*/
public TreeNode getMiniMumNode() {
TreeNode tNode = null;
int size = 0;
for(int i=0;i<2*length-1;i++) {
if(!visited[i] && nodeList[i] != null) {
tNode = nodeList[i];
size = i;
for(int j=0;j<2*length-1;j++) {
if(!visited[j] && nodeList[j] != null) {
if(tNode.info > nodeList[j].info) {
tNode = nodeList[j];
size = j;
}
}
}
}
}
visited[size] = true;
return tNode;
}
/**
* 中序遍历
*
* @param tNode
*/
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
/**
* 打印
*/
public void printHuffmanTree() {
System.out.println("inOrder");
inOrder(root);
}
/**
* @param args
*/
public static void main(String[] args) {
HuffmanTree hTree = new HuffmanTree(6);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(5);
hTree.addNode(6);
hTree.createHuffmanTree();
hTree.printHuffmanTree();
}
}
在8×8的棋盘上分布着n个骑士,他们想约在某一个格中聚会。骑士每天可以像国际象棋中的马那样移动一次,可以从中间向8个方向移动,请你计算n个骑士的最早聚会地点和要走多少天,要求尽早聚会,且n个人走的总步数最少,先到聚会地点的骑士可以不再移动等待其他的骑士。
从键盘输入n(0<n<=64),然后一次输入n个其实的初始位置xi,yi(0<=xi,y<=7)。屏幕输出以空格分割的三个数,分别为聚会的点(x,y) 以及要走的天数。="" ○ ○ ="" ○ ○="" ◎="" ○ ○="" ○ ○ ="" 骑士走法(中间为起始位置,空为走到位置)="" java代码="" package="" convex;="" public="" class="" point="" {="" public="" int="" x,="" y;="" public="" point(int="" x,="" int="" y)="" {="" if="" (x=""> 7 || y > 7) {
throw new RuntimeException("out of matrix");
}
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + x + ",y=" + y;
}
}
package convex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import convex.Point;
public class Algo {
private boolean[][] flg = new boolean[8][8];
private int[][] shortPath = new int[8][8];
//最短距离矩阵
public int[][] distanceSq(Point p1) {
djkst(p1);
return shortPath;
}
//BFS
private void djkst(Point p1) {
Point[] queue = new Point[64];
flg[p1.x][p1.y] = true;
queue[0] = p1;
int j=0;
int queueSize = 1;
while (j < queue.length) {
Point temp = queue[j];
Point[] list = getList(temp);
for(int i=0;i < list.length;i++) {
if(list[i] != null) {
Point w = list[i];
if (!flg[w.x][w.y]) {
shortPath[w.x][w.y] = shortPath[temp.x][temp.y] + 1;
queue[queueSize++] = w;
flg[w.x][w.y] = true;
}
}
}
j++;
}
}
//可行步数集
private static Point[] getList(Point point) {
Point[] list = new Point[8];
int length = 0;
if (point.x + 2 <= 7 && point.y + 1 <= 7) {
list[length++] = new Point(point.x + 2, point.y + 1);
}
if (point.x - 2 >= 0 && point.y - 1 >= 0) {
list[length++] = new Point(point.x - 2, point.y - 1);
}
if (point.x + 1 <= 7 && point.y + 2 <= 7) {
list[length++] = new Point(point.x + 1, point.y + 2);
}
if (point.x - 1 >= 0 && point.y - 2 >= 0) {
list[length++] = new Point(point.x - 1, point.y - 2);
}
if (point.x + 2 <= 7 && point.y - 1 >= 0) {
list[length++] = new Point(point.x + 2, point.y - 1);
}
if (point.x - 2 >= 0 && point.y + 1 <= 7) {
list[length++] = new Point(point.x - 2, point.y + 1);
}
if (point.x + 1 <= 7 && point.y - 2 >= 0) {
list[length++] = new Point(point.x + 1, point.y - 2);
}
if (point.x - 1 >= 0 && point.y + 2 <= 7) {
list[length++] = new Point(point.x - 1, point.y + 2);
}
return list;
}
public static int[] method(Point[] points, int i,int j,Object[] pointList) {
int maxDay = 0;
int distance = 0;
for(int k=0;k<pointlist.length;k++) {="" int="" day="((int[][])pointList[k])[i][j];" distance="" +="day;" if(maxday<day)="" {="" maxday="day;" }="" }="" return="" new="" int[]{maxday,distance};="" }="" public="" static="" void="" main(string[]="" args)="" throws="" ioexception="" {="" 数据输入="" 数据输入格式:第一个数字是骑士n,第2,3个数字是第一个骑士的坐标,依次类推。="" 每个数字之间以空格区分="" bufferedreader="" stdin="new" bufferedreader(="" new="" inputstreamreader(system.in));="" string="" line="stdin.readLine();" stringtokenizer="" st="new" stringtokenizer(line);="" int="" pointlength="Integer.parseInt(st.nextToken());" point[]="" points="new" point[pointlength];="" for(int="" i="0;i<points.length;i++)" {="" int="" x="Integer.parseInt(st.nextToken());" int="" y="Integer.parseInt(st.nextToken());" points[i]="new" point(x,y);="" }="" object[]="" pointlist="new" object[points.length];="" for="" (int="" j="0;" j="" <="" points.length;="" j++)="" {="" pointlist[j]="new" algo().distancesq(points[j]);="" }="" int="" minday="999999999;" int="" mindistance="999999999;" for(int="" i="0;i<7;i++)" {="" for(int="" j="0;j<7;j++)" {="" int[]="" result="Algo.method(points," i,j,pointlist);="" 找最短天数,最短天数相同,找最短距离="" if="" (minday=""> result[0]) {
minDay = result[0];
minDistance = result[1];
} else if(minDay == result[0]) {
if(minDistance > result[1]) {
minDistance = result[1];
}
}
}
}
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
int[] result = Algo.method(points, i,j,pointList);
if(minDay == result[0] && minDistance == result[1]) {
System.out.println(i+" " + j +" "+ minDay);
}
}
}
}
}
package convex;
public class Point {
public int x, y;
public Point(int x, int y) {
if (x > 7 || y > 7) {
throw new RuntimeException("out of matrix");
}
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + x + ",y=" + y;
}
}
package convex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import convex.Point;
public class Algo {
private boolean[][] flg = new boolean[8][8];
private int[][] shortPath = new int[8][8];
//最短距离矩阵
public int[][] distanceSq(Point p1) {
djkst(p1);
return shortPath;
}
//BFS
private void djkst(Point p1) {
Point[] queue = new Point[64];
flg[p1.x][p1.y] = true;
queue[0] = p1;
int j=0;
int queueSize = 1;
while (j < queue.length) {
Point temp = queue[j];
Point[] list = getList(temp);
for(int i=0;i < list.length;i++) {
if(list[i] != null) {
Point w = list[i];
if (!flg[w.x][w.y]) {
shortPath[w.x][w.y] = shortPath[temp.x][temp.y] + 1;
queue[queueSize++] = w;
flg[w.x][w.y] = true;
}
}
}
j++;
}
}
//可行步数集
private static Point[] getList(Point point) {
Point[] list = new Point[8];
int length = 0;
if (point.x + 2 <= 7 && point.y + 1 <= 7) {
list[length++] = new Point(point.x + 2, point.y + 1);
}
if (point.x - 2 >= 0 && point.y - 1 >= 0) {
list[length++] = new Point(point.x - 2, point.y - 1);
}
if (point.x + 1 <= 7 && point.y + 2 <= 7) {
list[length++] = new Point(point.x + 1, point.y + 2);
}
if (point.x - 1 >= 0 && point.y - 2 >= 0) {
list[length++] = new Point(point.x - 1, point.y - 2);
}
if (point.x + 2 <= 7 && point.y - 1 >= 0) {
list[length++] = new Point(point.x + 2, point.y - 1);
}
if (point.x - 2 >= 0 && point.y + 1 <= 7) {
list[length++] = new Point(point.x - 2, point.y + 1);
}
if (point.x + 1 <= 7 && point.y - 2 >= 0) {
list[length++] = new Point(point.x + 1, point.y - 2);
}
if (point.x - 1 >= 0 && point.y + 2 <= 7) {
list[length++] = new Point(point.x - 1, point.y + 2);
}
return list;
}
public static int[] method(Point[] points, int i,int j,Object[] pointList) {
int maxDay = 0;
int distance = 0;
for(int k=0;k<pointlist.length;k++) {="" int="" day="((int[][])pointList[k])[i][j];" distance="" +="day;" if(maxday<day)="" {="" maxday="day;" }="" }="" return="" new="" int[]{maxday,distance};="" }="" public="" static="" void="" main(string[]="" args)="" throws="" ioexception="" {="" 数据输入="" 数据输入格式:第一个数字是骑士n,第2,3个数字是第一个骑士的坐标,依次类推。="" 每个数字之间以空格区分="" bufferedreader="" stdin="new" bufferedreader(="" new="" inputstreamreader(system.in));="" string="" line="stdin.readLine();" stringtokenizer="" st="new" stringtokenizer(line);="" int="" pointlength="Integer.parseInt(st.nextToken());" point[]="" points="new" point[pointlength];="" for(int="" i="0;i<points.length;i++)" {="" int="" x="Integer.parseInt(st.nextToken());" int="" y="Integer.parseInt(st.nextToken());" points[i]="new" point(x,y);="" }="" object[]="" pointlist="new" object[points.length];="" for="" (int="" j="0;" j="" <="" points.length;="" j++)="" {="" pointlist[j]="new" algo().distancesq(points[j]);="" }="" int="" minday="999999999;" int="" mindistance="999999999;" for(int="" i="0;i<7;i++)" {="" for(int="" j="0;j<7;j++)" {="" int[]="" result="Algo.method(points," i,j,pointlist);="" 找最短天数,最短天数相同,找最短距离="" if="" (minday=""> result[0]) {
minDay = result[0];
minDistance = result[1];
} else if(minDay == result[0]) {
if(minDistance > result[1]) {
minDistance = result[1];
}
}
}
}
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
int[] result = Algo.method(points, i,j,pointList);
if(minDay == result[0] && minDistance == result[1]) {
System.out.println(i+" " + j +" "+ minDay);
}
}
}
}
}
下面开始写数据结构的基本代码:
1 节点类
Java代码
package graph;
public class GraphNode {
public GraphNode link;
public int info;
}
package graph;
public class GraphNode {
public GraphNode link;
public int info;
}
2 邻接表表示图的链表类
Java代码
package graph;
public class GraphList {
public GraphNode first;
public GraphNode last;
public boolean visitable;
public int getAjd(int[] ajd) {
GraphNode current = first;
int length = 0;
while(current != null) {
ajd[length++] = current.info;
current = current.link;
}
return length;
}
public void addNode(int v) {
GraphNode node = new GraphNode();
node.info = v;
if(first == null) {
first = node;
last = node;
} else {
last.link = node;
last = node;
}
}
}
package graph;
public class GraphList {
public GraphNode first;
public GraphNode last;
public boolean visitable;
public int getAjd(int[] ajd) {
GraphNode current = first;
int length = 0;
while(current != null) {
ajd[length++] = current.info;
current = current.link;
}
return length;
}
public void addNode(int v) {
GraphNode node = new GraphNode();
node.info = v;
if(first == null) {
first = node;
last = node;
} else {
last.link = node;
last = node;
}
}
}
3 图类
Java代码
package graph;
public class Graph {
private int length;
private GraphList[] list;
public Graph(int length) {
this.length = length;
list = new GraphList[length];
}
public void dfs(int v) {
int[] ajd = new int[length];
int ajdlength = list[v].getAjd(ajd);
list[v].visitable = true;
System.out.print(v+" ");
for(int i=0;i<ajdlength;i++) {="" int="" w="ajd[i];" if(!list[w].visitable)="" {="" dfs(w);="" }="" }="" }="" 深度优先遍历="" public="" void="" dfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" for(int="" i="0;i<length;i++)" {="" if(!list[i].visitable)="" {="" dfs(i);="" }="" }="" }="" 广度优先遍历="" public="" void="" bfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" bfs();="" }="" private="" void="" bfs()="" {="" queue="" queue="new" queue();="" for(int="" index="0;index<length;index++)" {="" if(!list[index].visitable)="" {="" queue.addqueue(index);="" list[index].visitable="true;" system.out.print(index+"="" ");="" while(!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" adj="new" int[length];="" int="" ajdlength="list[temp].getAjd(adj);" for(int="" i="0;i<ajdlength;i++)" {="" int="" w="adj[i];" if(!list[w].visitable)="" {="" system.out.print(w+"="" ");="" queue.addqueue(w);="" list[w].visitable="true;" }="" }="" }="" }="" }="" }="" 长度="" public="" void="" length()="" {="" system.out.println(length);="" }="" public="" boolean="" isempty()="" {="" return="" length="=" 0;="" }="" 添加节点="" public="" void="" addgraph(int="" info)="" {="" for(int="" i="0;i<length;i++)" {="" if(list[i]="=" null)="" {="" graphlist="" g="new" graphlist();="" g.addnode(info);="" list[i]="g;" break;="" }="" }="" }="" 添加边="" public="" void="" addside(int="" vfrom,int="" vto)="" {="" list[vfrom].addnode(vto);="" }="" 打印图="" public="" void="" print()="" {="" for(int="" i="0;i<length;i++)" {="" graphnode="" current="list[i].first;" while(current="" !="null)" {="" system.out.print(current.info+"="" ");="" current="current.link;" }="" system.out.println();="" }="" }="" public="" static="" void="" main(string[]="" args)="" {="" graph="" graph="new" graph(11);="" system.out.println("create="" graph="" start");="" for(int="" i="0;i<11;i++)" {="" graph.addgraph(i);="" }="" graph.addside(0,="" 1);="" graph.addside(0,="" 5);="" graph.addside(1,="" 2);="" graph.addside(1,="" 3);="" graph.addside(1,="" 5);="" graph.addside(2,="" 4);="" graph.addside(4,="" 3);="" graph.addside(5,="" 6);="" graph.addside(6,="" 8="" );="" graph.addside(7,="" 3);="" graph.addside(7,="" 8="" );="" graph.addside(8,="" 10);="" graph.addside(9,="" 4);="" graph.addside(9,="" 7);="" graph.addside(9,="" 10);="" graph.print();="" system.out.println("create="" graph="" end");="" graph.bfstravel();="" }="" }="" package="" graph;="" public="" class="" graph="" {="" private="" int="" length;="" private="" graphlist[]="" list;="" public="" graph(int="" length)="" {="" this.length="length;" list="new" graphlist[length];="" }="" public="" void="" dfs(int="" v)="" {="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[v].getAjd(ajd);" list[v].visitable="true;" system.out.print(v+"="" ");="" for(int="" i="0;i<ajdlength;i++)" {="" int="" w="ajd[i];" if(!list[w].visitable)="" {="" dfs(w);="" }="" }="" }="" 深度优先遍历="" public="" void="" dfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" for(int="" i="0;i<length;i++)" {="" if(!list[i].visitable)="" {="" dfs(i);="" }="" }="" }="" 广度优先遍历="" public="" void="" bfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" bfs();="" }="" private="" void="" bfs()="" {="" queue="" queue="new" queue();="" for(int="" index="0;index<length;index++)" {="" if(!list[index].visitable)="" {="" queue.addqueue(index);="" list[index].visitable="true;" system.out.print(index+"="" ");="" while(!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" adj="new" int[length];="" int="" ajdlength="list[temp].getAjd(adj);" for(int="" i="0;i<ajdlength;i++)" {="" int="" w="adj[i];" if(!list[w].visitable)="" {="" system.out.print(w+"="" ");="" queue.addqueue(w);="" list[w].visitable="true;" }="" }="" }="" }="" }="" }="" 长度="" public="" void="" length()="" {="" system.out.println(length);="" }="" public="" boolean="" isempty()="" {="" return="" length="=" 0;="" }="" 添加节点="" public="" void="" addgraph(int="" info)="" {="" for(int="" i="0;i<length;i++)" {="" if(list[i]="=" null)="" {="" graphlist="" g="new" graphlist();="" g.addnode(info);="" list[i]="g;" break;="" }="" }="" }="" 添加边="" public="" void="" addside(int="" vfrom,int="" vto)="" {="" list[vfrom].addnode(vto);="" }="" 打印图="" public="" void="" print()="" {="" for(int="" i="0;i<length;i++)" {="" graphnode="" current="list[i].first;" while(current="" !="null)" {="" system.out.print(current.info+"="" ");="" current="current.link;" }="" system.out.println();="" }="" }="" public="" static="" void="" main(string[]="" args)="" {="" graph="" graph="new" graph(11);="" system.out.println("create="" graph="" start");="" for(int="" i="0;i<11;i++)" {="" graph.addgraph(i);="" }="" graph.addside(0,="" 1);="" graph.addside(0,="" 5);="" graph.addside(1,="" 2);="" graph.addside(1,="" 3);="" graph.addside(1,="" 5);="" graph.addside(2,="" 4);="" graph.addside(4,="" 3);="" graph.addside(5,="" 6);="" graph.addside(6,="" 8="" );="" graph.addside(7,="" 3);="" graph.addside(7,="" 8="" );="" graph.addside(8,="" 10);="" graph.addside(9,="" 4);="" graph.addside(9,="" 7);="" graph.addside(9,="" 10);="" graph.print();="" system.out.println("create="" graph="" end");="" graph.bfstravel();="" }="" }="" 4="" 队列="" java代码="" package="" graph;="" public="" class="" queue="" {="" public="" graphnode="" first;="" public="" graphnode="" last;="" public="" int="" count;="" public="" void="" addqueue(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" if(first="=" null)="" {="" first="node;" last="node;" }="" else="" {="" last.link="node;" last="last.link;" }="" count++;="" }="" public="" void="" deletequeue()="" {="" if(first="=" null)="" {="" system.out.println("null="" queue");="" }="" else="" {="" first="first.link;" count--;="" }="" }="" public="" boolean="" isempty()="" {="" return="" count="=" 0;="" }="" public="" int="" front()="" {="" if(first="=" null)="" {="" return="" -1;="" }="" return="" first.info;="" }="" public="" int="" back()="" {="" if(last="=" null)="" {="" return="" -1;="" }="" return="" last.info;="" }="" }="" package="" graph;="" public="" class="" queue="" {="" public="" graphnode="" first;="" public="" graphnode="" last;="" public="" int="" count;="" public="" void="" addqueue(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" if(first="=" null)="" {="" first="node;" last="node;" }="" else="" {="" last.link="node;" last="last.link;" }="" count++;="" }="" public="" void="" deletequeue()="" {="" if(first="=" null)="" {="" system.out.println("null="" queue");="" }="" else="" {="" first="first.link;" count--;="" }="" }="" public="" boolean="" isempty()="" {="" return="" count="=" 0;="" }="" public="" int="" front()="" {="" if(first="=" null)="" {="" return="" -1;="" }="" return="" first.info;="" }="" public="" int="" back()="" {="" if(last="=" null)="" {="" return="" -1;="" }="" return="" last.info;="" }="" }="" 5="" 堆栈="" java代码="" package="" graph;="" public="" class="" stack="" {="" public="" graphnode="" stacktop;="" public="" int="" count;="" public="" void="" push(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" node.link="stackTop;" stacktop="node;" count++;="" }="" public="" void="" pop()="" {="" if(stacktop="=" null)="" {="" system.out.println("null="" stack");="" }="" else="" {="" stacktop="stackTop.link;" count--;="" }="" }="" public="" int="" top()="" {="" if(stacktop="=" null)="" {="" return="" -1;="" }="" return="" stacktop.info;="" }="" }="" package="" graph;="" public="" class="" stack="" {="" public="" graphnode="" stacktop;="" public="" int="" count;="" public="" void="" push(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" node.link="stackTop;" stacktop="node;" count++;="" }="" public="" void="" pop()="" {="" if(stacktop="=" null)="" {="" system.out.println("null="" stack");="" }="" else="" {="" stacktop="stackTop.link;" count--;="" }="" }="" public="" int="" top()="" {="" if(stacktop="=" null)="" {="" return="" -1;="" }="" return="" stacktop.info;="" }="" }="" 6="" 图的最短路径算法="" java代码="" package="" graph;="" public="" class="" graph="" {="" private="" int="" length;="" private="" graphlist[]="" list;="" private="" int[][]="" weight;="" public="" graph(int="" length)="" {="" this.length="length;" list="new" graphlist[length];="" weight="new" int[length][length];="" }="" public="" void="" dfs(int="" v)="" {="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[v].getAjd(ajd);" list[v].visitable="true;" system.out.print(v="" +="" "="" ");="" for="" (int="" i="0;" i="" <="" ajdlength;="" i++)="" {="" int="" w="ajd[i];" if="" (!list[w].visitable)="" {="" dfs(w);="" }="" }="" }="" 深度优先遍历="" public="" void="" dfstravel()="" {="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" list[i].visitable="false;" }="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" if="" (!list[i].visitable)="" {="" dfs(i);="" }="" }="" }="" 广度优先遍历="" public="" void="" bfstravel()="" {="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" list[i].visitable="false;" }="" bfs();="" }="" private="" void="" bfs()="" {="" queue="" queue="new" queue();="" for="" (int="" index="0;" index="" <="" length;="" index++)="" {="" if="" (!list[index].visitable)="" {="" queue.addqueue(index);="" list[index].visitable="true;" system.out.print(index="" +="" "="" ");="" while="" (!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[temp].getAjd(ajd);" for="" (int="" i="0;" i="" <="" ajdlength;="" i++)="" {="" int="" w="ajd[i];" if="" (!list[w].visitable)="" {="" system.out.print(w="" +="" "="" ");="" queue.addqueue(w);="" list[w].visitable="true;" }="" }="" }="" }="" }="" }="" 最短路径="" private="" int[]="" shortpath(int="" v)="" {="" int[]="" shortpath="new" int[length];="" boolean[]="" weightfound="new" boolean[length];="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" 趋近无穷="" shortpath[i]="9999;" weightfound[i]="false;" }="" shortpath[v]="0;" weightfound[v]="true;" queue="" queue="new" queue();="" queue.addqueue(v);="" while="" (!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[temp].getAjd(ajd);" for="" (int="" i="0;" i="" <="" ajdlength;="" i++)="" {="" int="" w="ajd[i];" if="" (!weightfound[w])="" {="" if="" (shortpath[w]=""> shortPath[temp] + weight[temp][w]) {
shortPath[w] = shortPath[temp] + weight[temp][w];
}
}
}
int minWeightNode = 0;
for (int i = 0; i < length; i++) {
if (!weightFound[i]) {
minWeightNode = i;
for (int j = 0; j < length; j++) {
if (!weightFound[j]) {
if (shortPath[j] < shortPath[minWeightNode]) {
minWeightNode = j;
}
}
}
break;
}
}
if (!weightFound[minWeightNode]) {
weightFound[minWeightNode] = true;
queue.addQueue(minWeightNode);
}
}
return shortPath;
}
// 长度
public void length() {
System.out.println(length);
}
public boolean isEmpty() {
return length == 0;
}
// 添加节点
public void addGraph(int info) {
for (int i = 0; i < length; i++) {
if (list[i] == null) {
GraphList g = new GraphList();
g.addNode(info);
list[i] = g;
break;
}
}
}
// 添加边
public void addSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
weight[vfrom][vto] = value;
}
// 打印图
public void print() {
for (int i = 0; i < length; i++) {
GraphNode current = list[i].first;
while (current != null) {
System.out.print(current.info + " ");
current = current.link;
}
System.out.println();
}
}
public static void main(String[] args) {
Graph graph = new Graph(5);
System.out.println("create graph start");
for (int i = 0; i < 5; i++) {
graph.addGraph(i);
}
graph.addSide(0, 1, 16);
graph.addSide(0, 3, 2);
graph.addSide(0, 4, 3);
graph.addSide(3, 4, 7);
graph.addSide(3, 1, 12);
graph.addSide(4, 1, 10);
graph.addSide(4, 3, 5);
graph.addSide(4, 2, 4);
graph.addSide(2, 1, 3);
graph.addSide(1, 2, 5);
graph.print();
System.out.println("create graph end");
int[] shortPath = graph.shortPath(0);
for (int i = 0; i < shortPath.length; i++) {
System.out.print(shortPath[i] + " ");
}
}
}
package graph;
public class Graph {
private int length;
private GraphList[] list;
private int[][] weight;
public Graph(int length) {
this.length = length;
list = new GraphList[length];
weight = new int[length][length];
}
public void dfs(int v) {
int[] ajd = new int[length];
int ajdlength = list[v].getAjd(ajd);
list[v].visitable = true;
System.out.print(v + " ");
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!list[w].visitable) {
dfs(w);
}
}
}
// 深度优先遍历
public void dfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
for (int i = 0; i < length; i++) {
if (!list[i].visitable) {
dfs(i);
}
}
}
// 广度优先遍历
public void bfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
bfs();
}
private void bfs() {
Queue queue = new Queue();
for (int index = 0; index < length; index++) {
if (!list[index].visitable) {
queue.addQueue(index);
list[index].visitable = true;
System.out.print(index + " ");
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!list[w].visitable) {
System.out.print(w + " ");
queue.addQueue(w);
list[w].visitable = true;
}
}
}
}
}
}
// 最短路径
private int[] shortPath(int v) {
int[] shortPath = new int[length];
boolean[] weightFound = new boolean[length];
for (int i = 0; i < length; i++) {
// 趋近无穷
shortPath[i] = 9999;
weightFound[i] = false;
}
shortPath[v] = 0;
weightFound[v] = true;
Queue queue = new Queue();
queue.addQueue(v);
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!weightFound[w]) {
if (shortPath[w] > shortPath[temp] + weight[temp][w]) {
shortPath[w] = shortPath[temp] + weight[temp][w];
}
}
}
int minWeightNode = 0;
for (int i = 0; i < length; i++) {
if (!weightFound[i]) {
minWeightNode = i;
for (int j = 0; j < length; j++) {
if (!weightFound[j]) {
if (shortPath[j] < shortPath[minWeightNode]) {
minWeightNode = j;
}
}
}
break;
}
}
if (!weightFound[minWeightNode]) {
weightFound[minWeightNode] = true;
queue.addQueue(minWeightNode);
}
}
return shortPath;
}
// 长度
public void length() {
System.out.println(length);
}
public boolean isEmpty() {
return length == 0;
}
// 添加节点
public void addGraph(int info) {
for (int i = 0; i < length; i++) {
if (list[i] == null) {
GraphList g = new GraphList();
g.addNode(info);
list[i] = g;
break;
}
}
}
// 添加边
public void addSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
weight[vfrom][vto] = value;
}
// 打印图
public void print() {
for (int i = 0; i < length; i++) {
GraphNode current = list[i].first;
while (current != null) {
System.out.print(current.info + " ");
current = current.link;
}
System.out.println();
}
}
public static void main(String[] args) {
Graph graph = new Graph(5);
System.out.println("create graph start");
for (int i = 0; i < 5; i++) {
graph.addGraph(i);
}
graph.addSide(0, 1, 16);
graph.addSide(0, 3, 2);
graph.addSide(0, 4, 3);
graph.addSide(3, 4, 7);
graph.addSide(3, 1, 12);
graph.addSide(4, 1, 10);
graph.addSide(4, 3, 5);
graph.addSide(4, 2, 4);
graph.addSide(2, 1, 3);
graph.addSide(1, 2, 5);
graph.print();
System.out.println("create graph end");
int[] shortPath = graph.shortPath(0);
for (int i = 0; i < shortPath.length; i++) {
System.out.print(shortPath[i] + " ");
}
}
}
8 普里姆最小生成树
Java代码
package graph;
/**
* @author B.Chen
*
*/
public class Graph {
/**
* 节点数
*/
private int length;
/**
* 链表
*/
private GraphList[] list;
/**
* 权集
*/
private int[][] weight;
/**
* 轻边集
*/
private int[][] lightSide;
/**
* @param length
*/
public Graph(int length) {
this.length = length;
list = new GraphList[length];
weight = new int[length][length];
lightSide = new int[length][length];
}
/**
* @param v
*/
public void dfs(int v) {
int[] ajd = new int[length];
int ajdlength = list[v].getAjd(ajd);
list[v].visitable = true;
System.out.print(v + " ");
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!list[w].visitable) {
dfs(w);
}
}
}
/**
* 深度优先遍历
*/
public void dfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
for (int i = 0; i < length; i++) {
if (!list[i].visitable) {
dfs(i);
}
}
}
/**
* 广度优先遍历
*/
public void bfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
bfs();
}
/**
* bfs
*/
private void bfs() {
Queue queue = new Queue();
for (int index = 0; index < length; index++) {
if (!list[index].visitable) {
queue.addQueue(index);
list[index].visitable = true;
System.out.print(index + " ");
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] adj = new int[length];
int ajdlength = list[temp].getAjd(adj);
for (int i = 0; i < ajdlength; i++) {
int w = adj[i];
if (!list[w].visitable) {
System.out.print(w + " ");
queue.addQueue(w);
list[w].visitable = true;
}
}
}
}
}
}
/**
* 长度
*/
public void length() {
System.out.println(length);
}
/**
* @return boolean
*/
public boolean isEmpty() {
return length == 0;
}
/**
* @param info
*/
public void addGraph(int info) {
for (int i = 0; i < length; i++) {
if (list[i] == null) {
GraphList g = new GraphList();
g.addNode(info);
list[i] = g;
break;
}
}
}
/**
* 添加有向图的一条边
* @param vfrom
* @param vto
* @param value 权
*/
public void addSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
weight[vfrom][vto] = value;
}
/**
* 添加无向图的一条边
* @param vfrom
* @param vto
* @param value
*/
public void addDoubleSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
list[vto].addNode(vfrom);
weight[vfrom][vto] = value;
weight[vto][vfrom] = value;
}
/**
* 打印图
*/
public void print() {
for (int i = 0; i < length; i++) {
GraphNode current = list[i].first;
while (current != null) {
System.out.print(current.info + " ");
current = current.link;
}
System.out.println();
}
}
/**
* Dijkstra
*
* @param v
* @return int[]
*/
public int[] shortPath(int v) {
int[] shortPath = new int[length];
boolean[] weightFound = new boolean[length];
for (int i = 0; i < length; i++) {
// 趋近无穷
shortPath[i] = 9999;
weightFound[i] = false;
}
shortPath[v] = 0;
weightFound[v] = true;
Queue queue = new Queue();
queue.addQueue(v);
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!weightFound[w]) {
if (shortPath[w] > shortPath[temp] + weight[temp][w]) {
shortPath[w] = shortPath[temp] + weight[temp][w];
}
}
}
int minWeightNode = 0;
for (int i = 0; i < length; i++) {
if (!weightFound[i]) {
minWeightNode = i;
for (int j = 0; j < length; j++) {
if (!weightFound[j]) {
if (shortPath[j] < shortPath[minWeightNode]) {
minWeightNode = j;
}
}
}
break;
}
}
if (!weightFound[minWeightNode]) {
weightFound[minWeightNode] = true;
queue.addQueue(minWeightNode);
}
}
return shortPath;
}
/**
* 普里姆最小生成树
*
* @param v
*/
public void primMST(int v) {
boolean[] visited = new boolean[length];
for (int i = 0; i < length; i++) {
visited[i] = false;
for (int j = 0; j < length; j++) {
lightSide[i][j] = 9999;
}
}
visited[v] = true;
Queue queue = new Queue();
queue.addQueue(v);
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
lightSide[temp][w] = weight[temp][w];
}
// 找到最小边
int minSide = 0;
int vfrom =0;
int vto = 0;
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (visited[i] && visited[j]) {
continue;
}
minSide = lightSide[i][j];
vfrom = i;
vto = j;
for (int k = 0; k < length; k++) {
for (int l = 0; l < length; l++) {
if (visited[k] && visited[l]) {
continue;
}
if (lightSide[k][l] < minSide) {
minSide = lightSide[k][l];
vfrom = k;
vto = l;
}
}
}
break;
}
}
//将最小边的节点vto设为true,并输出vto
if (!visited[vto]) {
visited[vto] = true;
System.out.print(vfrom+"==>" + vto+", ");
queue.addQueue(vto);
}
}
}
/**
* @param args
*/
相关推荐
在日常的开发和使用中,我们经常需要借助各种小工具来提高工作效率,例如快速启动常用的应用程序、管理文件等。一个简单但功能强大的集成工具箱可以帮助用户快速访问、启动并管理程序。今天,我们将以Python为基础,结合Tkinter和Win32API,开发一个类似Windows快捷方式的工具箱应用,能够让你轻松集成各种常用程序并一键启动
django自建博客app
《基于YOLOv8的智慧校园实验室高压灭菌锅安全联锁系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计
用于hifi测序数据的基因组组装程序
Microsoft Access 2010 数据库引擎可再发行程序包AccessDatabaseEngine-X64解压后的文件AceRedist
从大模型、智能体到复杂AI应用系统的构建——以产业大脑为例
自然语言处理之TF-IDF算法与TextRank算法的缠绵_textrank,tf-idf和两者的组合-CSDN博客.html
内容概要:2023版《科学智能 (AI4S)全球发展观察与展望》阐述了AI for Science(AI4S)在全球范围内的最新进展及其对科学和工业的深远影响。文章首先回顾了AI4S在过去一年中的快速发展,特别是在药物研发、材料科学、地质学、污染治理等多个领域的应用实例。AI4S通过结合深度学习、机器学习和其他AI技术,加速了从基础研究到实际应用的转化过程。例如,在药物研发中,AI4S帮助科学家克服了“反摩尔定律”的挑战,提高了新药研发的成功率;在材料科学中,AI4S实现了复杂材料的高效模拟,如人造钻石、石墨烯、碳纳米管等;在地质学中,AI4S通过模拟地球内部结构和物理过程,为地震学研究提供了新视角。此外,文章还探讨了大语言模型(LLMs)与科学方法的结合,指出LLMs不仅能辅助科学研究,还能生成新的科学假设并进行逻辑推理。 适合人群:具备一定科研背景或对AI技术感兴趣的科研人员、工程师、政策制定者及高校师生。
这个数据集包含了日常步数统计、睡眠时长、活跃分钟数以及消耗的卡路里,是个人健康与健身追踪的一部分。 该数据集非常适合用于以下实践: 数据清洗:现实世界中的数据往往包含缺失值、异常值或不一致之处。例如,某些天的步数可能缺失,或者存在不切实际的数值(如10,000小时的睡眠或负数的卡路里消耗)。通过处理这些问题,可以学习如何清理和准备数据进行分析。 探索性分析(发现日常习惯中的模式):可以通过分析找出日常生活中的模式和趋势,比如一周中哪一天人们通常走得最多,或是睡眠时间与活跃程度之间的关系等。 构建可视化图表(步数趋势、睡眠与活动对比图):将数据转换成易于理解的图形形式,有助于更直观地看出数据的趋势和关联。例如,绘制步数随时间变化的趋势图,或是比较睡眠时间和活动量之间的关系图。 数据叙事(将个人风格的追踪转化为可操作的见解):通过讲述故事的方式,把从数据中得到的洞察变成具体的行动建议。例如,根据某人特定时间段内的活动水平和睡眠质量,提供改善健康状况的具体建议。
框架结构天城商业办公楼5200平米(建筑图 结构图 计算书 开题报告 任务书 文献翻.zip
柴油机连杆加工工艺及夹具设计.zip
读书网首页的HTML信息
文字渐变颜色代码生成器:让文字绽放多彩魅力,演示:在信息交流日益丰富的今天,个性化的文字展示成为吸引目光的关键。这款文字渐变颜色代码生成器,便是为满足这一需求而生的绿色软件,无需安装,便捷实用。 它的操作极为简便。用户只需在软件界面中输入想要转换的文字内容,接着从丰富的色彩选项里挑选心仪的起始颜色与结束颜色,随后轻轻按下 “转换按钮”,神奇的事情就此发生 —— 适用于论坛、网页、QQ 空间等多种平台,以及自定义格式的渐变颜色代码便会即刻生成。不仅如此,生成的代码还能自动复制到剪切板,极大地节省了用户手动复制的时间。当你在论坛回帖、更新网页内容或是装扮 QQ 空间时,只需轻松粘贴代码,原本单调的文字瞬间就能拥有绚丽的渐变色彩,瞬间脱颖而出,为你的表达增添独特魅力,让文字不再平凡,轻松成为视觉焦点。 一款可以轻松把一段文字生成渐变颜色代码的绿色软件,当你在软件中输入完要转换的文字后,只需要挑选自己喜欢的起始颜色、结束颜色后,按一下―转换按钮即可生成相应的论坛/网页/QQ空间以及自定义格式代码,并且代码可以自动复制到剪切板中,回帖时直接粘贴代码即可不错得文字代码生成器,让你得文字更加漂亮.
1.【锂电池剩余寿命预测】Transformer锂电池剩余寿命预测(Matlab完整源码和数据) 2.数据集:NASA数据集,已经处理好,B0005电池训练、B0006测试; 3.环境准备:Matlab2023b,可读性强; 4.模型描述:Transformer在各种各样的问题上表现非常出色,现在被广泛使用。 5.领域描述:近年来,随着锂离子电池的能量密度、功率密度逐渐提升,其安全性能与剩余使用寿命预测变得愈发重要。本代码实现了Transformer在该领域的应用。 6.作者介绍:机器学习之心,博客专家认证,机器学习领域创作者,2023博客之星TOP50,主做机器学习和深度学习时序、回归、分类、聚类和降维等程序设计和案例分析,文章底部有博主联系方式。从事Matlab、Python算法仿真工作8年,更多仿真源码、数据集定制私信。
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
Android项目原生java语言课程设计,包含LW+ppt
配套文章:https://blog.csdn.net/gust2013/article/details/146909670?spm=1001.2014.3001.5502
《基于YOLOv8的智慧社区儿童游乐设施安全监测系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计