Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点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 最短路径)的更多相关文章
- 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 ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- POJ 2387 Til the Cows Come Home (Dijkstra)
传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 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 ...
随机推荐
- java 跨数据库导入大数据
java 跨数据库导入大数据 /** * java程序跨服务器跨数据库批量导入导出百万级数据 * @param args * @throws Exception */ public static vo ...
- ⭐内核MKDEV(MAJOR, MINOR)宏
版本:linux-2.6.24.4宏: MKDEV(MAJOR, MINOR); 说明: 获取设备在设备表中的位置. MAJOR 主设备号 MINOR 次设 ...
- Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行
原文链接: http://www.cnblogs.com/zouzf/p/3985330.html 在wp8平台上,CCLabeTTF显示中文不会自动换行,看了下源码,原来底层的实现是根据text的空 ...
- [Python]基于CNN的MNIST手写数字识别
目录 一.背景介绍 1.1 卷积神经网络 1.2 深度学习框架 1.3 MNIST 数据集 二.方法和原理 2.1 部署网络模型 (1)权重初始化 (2)卷积和池化 (3)搭建卷积层1 (4)搭建卷积 ...
- KestrelHttpServer
source code of Kestrel of documentation https://github.com/aspnet/KestrelHttpServer https://github.c ...
- pylab.show()没有显示图形图像(python的matplotlib画图包)
no display name and no $DISPLAY environment variable ============================ @Neil's answer is ...
- 【bzoj1876】[SDOI2009]SuperGCD(高精度)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1876 一道简单的高精度+Stein算法(或者叫辗转相除法)求最大公约数板子题. md还 ...
- python爬虫-初步认识
特此声明: 以下内容来源于博主:http://blog.csdn.net/pleasecallmewhy http://cuiq ...
- R语言:导入导出数据
主要学习如何把几种常用的数据格式导入到R中进行处理,并简单介绍如何把R中的数据保存为R数据格式和csv文件. 1.保存和加载R的数据(与R.data的交互:save()函数和load()函数) a & ...
- 机器学习(六)—随机森林Random Forest
1.什么是随机采样? Bagging可以简单的理解为:放回抽样,多数表决(分类)或简单平均(回归): Bagging的弱学习器之间没有boosting那样的联系,不存在强依赖关系,基学习器之间属于并列 ...