题目:从节点N到节点1的求最短路径。

分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次。再有就是多重边,要取最小值。最后就是路径的长度的最大值不是100,而是100001。用Dijkstra求最短路径,感觉 Dijkstra和Prim很像,都是从结点中找到路径最小的一条,然后再做某种更新。Dijkstra是看看源节点通过当前节点能否缩短从源头到其他节 点的路径,而Prim则是通过加入当前节点到生成树,然后更新生成树中的路径数量,再从中找到最小路径。

package Map;

import java.util.Scanner;

/**
* Dijkstra最短路径
*/
public class Poj_2387_Dijkstra { static int n, m;
static int MAXV = 2010;
static int MAX = 100001; static int map[][] = new int[MAXV][MAXV];
static boolean vis[] = new boolean[MAXV]; public static int dijksrta(int V) { vis[V] = true;
for (int i = 1; i <= n; i++) {
int min = MAX;
int k = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && map[V][j] < min) {
min = map[V][j];
k = j;
}
}
vis[k] = true;
for (int j = 1; j <= n; j++) {
if (!vis[j] && min + map[k][j] < map[V][j]) {
map[V][j] = min + map[k][j];
}
}
}
return map[V][1];
} public static void main(String args[]) {
Scanner sc = new Scanner(System.in); m = sc.nextInt();
n = sc.nextInt(); for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
map[i][j] = MAX;
}
map[i][i] = 0;
}
for (int i = 1; i <= m; i++) {
int s = sc.nextInt();
int e = sc.nextInt();
int val = sc.nextInt();
if(map[s][e] > val){ //重边问题,去最小的
map[s][e] = val;
map[e][s] = val;
}
}
System.out.println(dijksrta(n));
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)的更多相关文章

  1. POJ 2387 Til the Cows Come Home Dijkstra求最短路径

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

  2. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

  3. poj 2387 Til the Cows Come Home(dijkstra算法)

    题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...

  4. POJ 2387 Til the Cows Come Home (Dijkstra)

    传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...

  5. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

  6. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  7. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  8. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  9. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted ...

随机推荐

  1. RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 是什么意思?

    <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENA ...

  2. golang注册码

    许可证服务认证 由于更新,最近注册码都不能用了,下面是能用的, http://idea.youbbs.org

  3. constexpr与指针

    一. 常量表达式:是指值不会改变并且在编译过程就能得到的计算结果的表达式. 定义常量表达式变量: constexpr 变量类型 变量名: 例如: constexpr int mf=20://///20 ...

  4. 吴恩达深度学习笔记(五) —— 优化算法:Mini-Batch GD、Momentum、RMSprop、Adam、学习率衰减

    主要内容: 一.Mini-Batch Gradient descent 二.Momentum 四.RMSprop 五.Adam 六.优化算法性能比较 七.学习率衰减 一.Mini-Batch Grad ...

  5. Linux下检测IP访问特定网站的ruby脚本

    root@ubuntu:~# vi check_ip.rbrequire 'rubygems' index = 1 max = 20 while (max-index) >= 0 puts in ...

  6. Linux 配置 SSL 证书

    完整的 SSL 证书分为四个部分: CA 根证书 (root CA) 中级证书 (Intermediate Certificate) 域名证书 证书密钥 (仅由您持有) 以 COMODO Positi ...

  7. Myeclipse中Tomcat的两种部署方式

    一.在Myeclipse软件中部署 1. 在Myeclipse中,创建好工程后,在Myeclipse菜单栏中选择 Windows -> Preferences -> Myeclipse - ...

  8. centos7 下安装eclipse

    1 在下面路径下载 eclipse-jee-neon-2-linux-gtk-x86_64.tar.gzhttp://eclipse.stu.edu.tw/technology/epp/downloa ...

  9. java基础(5)-集合类1

    集合的由来 数组是很常用的一种数据结构,但假如我们遇到以下这样的的问题: 容器长度不确定 能自动排序 存储以键值对方式的数据 如果遇到这样的情况,数组就比较难满足了,所以也就有了一种与数组类似的数据结 ...

  10. JavaScript -- 正则表达式 检验表单提交的内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...