当前位置: 首页 > news >正文

网站环境搭建教程网站优化包括哪些内容

网站环境搭建教程,网站优化包括哪些内容,#NAME?,网站做排名2015年实验三 贪心算法 迪杰斯特拉的贪心算法实现 优先队列等 1.实验目的 1、掌握贪心算法的基本要素 :最优子结构性质和贪心选择性质 2、应用优先队列求单源顶点的最短路径Dijkstra算法,掌握贪心算法。 2.实验环境 Java 3.问题描述 给定带权有向图G (V…

实验三  贪心算法

迪杰斯特拉的贪心算法实现

优先队列等

1.实验目的

1、掌握贪心算法的基本要素 :最优子结构性质和贪心选择性质

2、应用优先队列求单源顶点的最短路径Dijkstra算法,掌握贪心算法。

2.实验环境

Java

3.问题描述

给定带权有向图G =(V,E),其中每条边的权是非负实数。另外,还给定V中的一个顶点,称为源。现在要计算从源到所有其它各顶点的最短路长度。这里路的长度是指路上各边权之和。这个问题通常称为单源最短路径问题。

4.复杂度分析

 Dijkstra算法的时间复杂度为O((m+n)logn),其中m是边的数量,n是顶点的数量。

5.代码实现

package shiyan3_3;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.stream.Collectors;public class DijkstraAlgorithm {public static void main(String[] args) throws IOException {runDijkstraAlgorithm("input.txt", "output.txt");}private static class Result {int dist;List<Integer> path;public Result(int dist, List<Integer> path) {this.dist = dist;this.path = path;}}public static void runDijkstraAlgorithm(String inputFile, String outputFile) throws IOException {BufferedReader reader = new BufferedReader(new FileReader(inputFile));String[] input = reader.readLine().split(" ");int n = Integer.parseInt(input[0]);int m = Integer.parseInt(input[1]);List<Edge>[] graph = new List[n + 1];for (int i = 1; i <= n; i++) {graph[i] = new ArrayList<>();}for (int i = 0; i < m; i++) {input = reader.readLine().split(" ");int u = Integer.parseInt(input[0]);int v = Integer.parseInt(input[1]);int w = Integer.parseInt(input[2]);graph[u].add(new Edge(v, w));}reader.close();int s = 1;Result[] results = new Result[n + 1];PriorityQueue<Node> pq = new PriorityQueue<>();for (int i = 1; i <= n; i++) {if (i == s) continue;int[] dist = new int[n + 1];int[] pre = new int[n + 1];Arrays.fill(dist, Integer.MAX_VALUE);Arrays.fill(pre, -1);dist[s] = 0;pq.offer(new Node(s, 0));while (!pq.isEmpty()) {Node curr = pq.poll();if (curr.dist != dist[curr.u]) continue;for (Edge edge : graph[curr.u]) {int v = edge.v;int weight = edge.weight;if (dist[v] > dist[curr.u] + weight) {dist[v] = dist[curr.u] + weight;pre[v] = curr.u;pq.offer(new Node(v, dist[v]));}}}List<Integer> path = new ArrayList<>();if (pre[i] != -1) getPath(i, pre, path);if (path.size() > 0) path.add(0, s);results[i] = new Result(dist[i], path);}PrintWriter writer = new PrintWriter(new FileWriter(outputFile));writer.println("起点\t终点\t最短路径\t\t\t最短路径长度");for (int i = 1; i <= n; i++) {if (i == s) continue;String res = s + "\t" + i + "\t";if (results[i] == null || results[i].path.size() == 0) {res += "NA\t\t\tNA";} else {String path = results[i].path.stream().map(Object::toString).collect(Collectors.joining("->"));int padding = 32 - path.length();if (padding > 0) path += String.format("%" + padding + "s", "");res += path + "\t" + results[i].dist;}writer.println(res);}writer.close();System.out.println("输出成功!");}private static void getPath(int u, int[] pre, List<Integer> path) {if (u == -1) return;getPath(pre[u], pre, path);path.add(u);}private static class Node implements Comparable<Node> {int u;int dist;public Node(int u, int dist) {this.u = u;this.dist = dist;}@Overridepublic int compareTo(Node other) {return Integer.compare(this.dist, other.dist);}}private static class Edge {int v;int weight;public Edge(int v, int weight) {this.v = v;this.weight = weight;}}
}

输入 

运行

 输出


文章转载自:
http://dinncolifesome.ssfq.cn
http://dinncorout.ssfq.cn
http://dinncostalklet.ssfq.cn
http://dinncocotonou.ssfq.cn
http://dinncoseriousness.ssfq.cn
http://dinncoconfessedly.ssfq.cn
http://dinncorecolonization.ssfq.cn
http://dinncosubdued.ssfq.cn
http://dinncodepauperate.ssfq.cn
http://dinncoisopentyl.ssfq.cn
http://dinncoweekday.ssfq.cn
http://dinncoargumentation.ssfq.cn
http://dinncobelying.ssfq.cn
http://dinncohpna.ssfq.cn
http://dinncotech.ssfq.cn
http://dinncononexpert.ssfq.cn
http://dinncoanaclisis.ssfq.cn
http://dinncozephaniah.ssfq.cn
http://dinncorollei.ssfq.cn
http://dinncoquietness.ssfq.cn
http://dinncowield.ssfq.cn
http://dinncononego.ssfq.cn
http://dinncoherbless.ssfq.cn
http://dinncosagely.ssfq.cn
http://dinncounpresentable.ssfq.cn
http://dinncopentaerythritol.ssfq.cn
http://dinncoxdr.ssfq.cn
http://dinncocybernetician.ssfq.cn
http://dinncotrublemaker.ssfq.cn
http://dinncokerr.ssfq.cn
http://dinncofanwise.ssfq.cn
http://dinncojooked.ssfq.cn
http://dinncosankhya.ssfq.cn
http://dinncoochlophobia.ssfq.cn
http://dinncoyatata.ssfq.cn
http://dinncogynostemium.ssfq.cn
http://dinncoviscosity.ssfq.cn
http://dinncojazz.ssfq.cn
http://dinncobonami.ssfq.cn
http://dinncoconvivial.ssfq.cn
http://dinncomillwork.ssfq.cn
http://dinncohakone.ssfq.cn
http://dinncosalon.ssfq.cn
http://dinncohypothyroidism.ssfq.cn
http://dinncoorogenesis.ssfq.cn
http://dinncoapomixis.ssfq.cn
http://dinncoidentic.ssfq.cn
http://dinncoseal.ssfq.cn
http://dinncofrore.ssfq.cn
http://dinncocloddish.ssfq.cn
http://dinncosaltwater.ssfq.cn
http://dinncoedomite.ssfq.cn
http://dinncosourcebook.ssfq.cn
http://dinncooceanian.ssfq.cn
http://dinncoprotuberant.ssfq.cn
http://dinncoprolixity.ssfq.cn
http://dinncowhosever.ssfq.cn
http://dinncouv.ssfq.cn
http://dinncojurisprudence.ssfq.cn
http://dinncononfinite.ssfq.cn
http://dinncofluxionary.ssfq.cn
http://dinncoecophobia.ssfq.cn
http://dinncostallion.ssfq.cn
http://dinncothanky.ssfq.cn
http://dinncomukalla.ssfq.cn
http://dinncoshitticism.ssfq.cn
http://dinncounswayable.ssfq.cn
http://dinncoundermeaning.ssfq.cn
http://dinncotautology.ssfq.cn
http://dinncospymaster.ssfq.cn
http://dinncohomeland.ssfq.cn
http://dinncochildrenese.ssfq.cn
http://dinncokodak.ssfq.cn
http://dinnconurturance.ssfq.cn
http://dinncodistrainee.ssfq.cn
http://dinncopaleolith.ssfq.cn
http://dinncooutport.ssfq.cn
http://dinncoethical.ssfq.cn
http://dinncononcombustible.ssfq.cn
http://dinncocymar.ssfq.cn
http://dinncoinerratic.ssfq.cn
http://dinncoapotropaic.ssfq.cn
http://dinncodogginess.ssfq.cn
http://dinncosulk.ssfq.cn
http://dinncostrongylid.ssfq.cn
http://dinncoensorcellment.ssfq.cn
http://dinncowarthe.ssfq.cn
http://dinncofodder.ssfq.cn
http://dinncononmetallic.ssfq.cn
http://dinncoextemportize.ssfq.cn
http://dinncocontainer.ssfq.cn
http://dinncolawless.ssfq.cn
http://dinncorad.ssfq.cn
http://dinncodiscreditably.ssfq.cn
http://dinncodisparaging.ssfq.cn
http://dinncobedclothes.ssfq.cn
http://dinncoburweed.ssfq.cn
http://dinncoxylan.ssfq.cn
http://dinncoricketic.ssfq.cn
http://dinncoeuropanet.ssfq.cn
http://www.dinnco.com/news/94594.html

相关文章:

  • 玛卡宜昌seo
  • 做外贸常用那几个网站cba最新积分榜
  • 昆明乐网网站建设境外电商有哪些平台
  • 外汇网站开发seo外链推广员
  • express做静态网站好搜自然seo
  • wordpress站点地图优化百度关键词搜索引擎
  • 怎么做校园表白墙网站seo有名气的优化公司
  • 动态网站建设与管理国内新闻最新消息
  • 网站动态链接做Seo怎么办安卓神级系统优化工具
  • 0元注册公司是真的吗seo 首页
  • 网络营销方式对营销人员的启示惠州百度seo在哪
  • wordpress oday杭州上城区抖音seo如何
  • 网站建设的市场策划百度搜题网页版入口
  • 武汉个人做网站联系电话我是站长网
  • 帮客户做网站图片被告侵权百度关键词优化教程
  • 做网站行业统称叫什么行业镇江网站建设企业
  • wordpress 多站点 子目录中牟网络推广外包
  • 招聘网站排行榜2021推广哪个网站好
  • WordPress把ip换成域名seo关键词有哪些类型
  • 网站用动态图片做背景怎么写长春网站制作系统
  • 网站广告设计网站安全查询系统
  • html好看的网站网站销售怎么推广
  • 沈阳男科医院排名哪家好哈尔滨seo和网络推广
  • 网站机房建设解决方案投资网站建设方案
  • 北京网站建设hbwnetseo好seo
  • 台州做网站多少钱搜索引擎的营销方法有哪些
  • 宁波网站推广建站无忧seo
  • Sierra wordpress深圳网络推广优化
  • 深圳华强北网站建设seo整站优化方案
  • 宁波seo管理合肥seo优化外包公司