这里写目录标题
一、上机目的
1、通过分支限界法的示例程序进一步理解分支限界法的基本思想;
2、运用分支限界法解决实际问题,进一步加深对分支限界法的理解和运用
二、上机内容与要求
1、分析并掌握“单源最短路径” 问题的分支限界法求解方法;
2、练习使用分支限界法求解“装载”问题;
三、上机步骤
1.理解分支限界法思想和算法示例;
2.上机输入和调试算法示例程序;
3.理解实验题的问题要求;
4.上机输入和调试自己所编的实验题程序;
5.验证并分析实验题的实验结果;
6.整理出实验报告;
四、上机结果
1、将课本6.2节单源最短路径算法改为程序,并进行测试和验证
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class ArcCell {
int adj; // 保存权值
int info; // 存储最短路径长度
}
public class GraphPath {
static ArcCell[][] AdjMatrix = new ArcCell[100][100];
static VerType[] vexs = new VerType[100];
static int vexnum;
static int arcnum;
static Queue<Integer> q = new LinkedList<>();
static class VerType {
int data;
int length;
}
static void CreateGraph() {
Scanner scanner = new Scanner(System.in);
int m, n, t;
System.out.print("输入顶点数和弧数:");
vexnum = scanner.nextInt();
arcnum = scanner.nextInt();
System.out.print("输入顶点:");
for (int i = 1; i <= vexnum; i++) {
vexs[i] = new VerType();
vexs[i].data = scanner.nextInt();
vexs[i].length = 10000;
}
for (int i = 1; i <= vexnum; i++)
for (int j = 1; j <= vexnum; j++) {
AdjMatrix[i][j] = new ArcCell();
AdjMatrix[i][j].adj = 0;
}
System.out.println("输入弧及权重:");
for (int i = 1; i <= arcnum; i++) {
m = scanner.nextInt();
n = scanner.nextInt();
t = scanner.nextInt();
AdjMatrix[m][n].adj = 1;
AdjMatrix[m][n].info = t;
}
}
static int NextAdj(int v, int w) {
for (int i = w + 1; i <= vexnum; i++)
if (AdjMatrix[v][i].adj != 0)
return i;
return 0; // not found;
}
static void ShortestPaths(int v) {
int k = 0; // 从首个节点开始访问
int t;
vexs[v].length = 0;
q.offer(vexs[v].data);
while (!q.isEmpty()) {
t = q.poll();
k = NextAdj(t, k);
while (k != 0) {
if (vexs[t].length + AdjMatrix[t][k].info <= vexs[k].length) {
vexs[k].length = vexs[t].length + AdjMatrix[t][k].info;
q.offer(vexs[k].data);
}
k = NextAdj(t, k);
}
}
}
static void Print() {
for (int i = 1; i <= vexnum; i++)
System.out.println(vexs[i].data + "------" + vexs[i].length);
}
public static void main(String[] args) {
CreateGraph();
ShortestPaths(1);
Print();
}
}
2、将课本6.3节装载问题改为程序,并进行测试和验证。
import java.util.PriorityQueue;
import java.util.Scanner;
class MaxHeapQNode {
MaxHeapQNode parent; // 父节点
int lchild; // 左节点: 1; 右节点: 0
int weight; // 总重量
int lev; // 层次
}
class MaxLoading {
static int n;
static int c;
static int bestw;
static int[] w;
static int[] bestx;
public static void main(String[] args) {
InPut();
MaxLoading();
OutPut();
}
static void InPut() {
Scanner scanner = new Scanner(System.in);
System.out.println("Input1:");
n = scanner.nextInt();
System.out.println("Input2:");
c = scanner.nextInt();
System.out.println("Input3:");
w = new int[n + 1];
bestx = new int[n + 1];
for (int i = 1; i <= n; ++i)
w[i] = scanner.nextInt();
}
static void AddAliveNode(PriorityQueue<MaxHeapQNode> q, MaxHeapQNode E, int wt, int i, int ch) {
MaxHeapQNode p = new MaxHeapQNode();
p.parent = E;
p.lchild = ch;
p.weight = wt;
p.lev = i + 1;
q.add(p);
}
static void MaxLoading() {
PriorityQueue<MaxHeapQNode> q = new PriorityQueue<>((a, b) -> Integer.compare(b.weight, a.weight));
// 定义剩余重量数组r
int[] r = new int[n + 1];
r[n] = 0;
for (int j = n - 1; j > 0; --j)
r[j] = r[j + 1] + w[j + 1];
int i = 1;
MaxHeapQNode E = new MaxHeapQNode();
int Ew = 0;
while (i != n + 1) {
if (Ew + w[i] <= c) {
AddAliveNode(q, E, Ew + w[i] + r[i], i, 1);
}
AddAliveNode(q, E, Ew + r[i], i, 0);
// 取下一节点
E = q.poll();
i = E.lev;
Ew = E.weight - r[i - 1];
}
bestw = Ew;
for (int j = n; j > 0; --j) {
bestx[j] = E.lchild;
E = E.parent;
}
}
static void OutPut() {
System.out.println("最优装载量为 " + bestw);
System.out.println("装载的物品为 ");
for (int i = 1; i <= n; ++i)
if (bestx[i] == 1)
System.out.print(i + " ");
}
}
标签:scanner,int,System,vexs,算法,static,new,源代码,限界
From: https://blog.csdn.net/a1234567822/article/details/136683217