Til the Cows Come Home

POJ-2387

  • 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\).
  • 需要注意的是,一定要看清楚题目的输入要求,是先输入边,再输入顶点,一开始我没看清,wrong answer了一次。
package POJ;
import java.util.*;
public class POJ_2387 {
private static int n,t;
static int [][]w;
static boolean []flag;//是否被遍历过
static int sta;//开始结点
static int []dis;//distance数组
static final int INF=0x3f3f3f3f;
public static int dijikstra() {
dis=new int[n];
flag=new boolean[n];
Arrays.fill(flag, false);
for(int i=0;i<n;i++) {
if(i!=sta)
dis[i]=INF;
else dis[i]=0;
}
for(int i=0;i<n;i++) {
int index=-1,mins=INF;
for(int j=0;j<n;j++) {
if(!flag[j]&&dis[j]<=mins) {
mins=dis[index=j];
}
}
if(index==-1)
break;
flag[index]=true;
for(int j=0;j<n;j++)
dis[j]=Math.min(dis[j], dis[index]+w[index][j]);
}
return dis[0];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
//注意:这里先输入边再输入顶点!
t=cin.nextInt();
n=cin.nextInt();
w=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
w[i][j]=INF;
w[i][i]=0;
}
for(int i=0;i<t;i++) {
int from,to;
from=cin.nextInt();
to=cin.nextInt();
int len=cin.nextInt();
w[from-1][to-1]=w[to-1][from-1]=Math.min(len, w[from-1][to-1]);
}
sta=n-1;
System.out.println(dijikstra());
}
}

POJ-2387(原始dijkstra求最短路)的更多相关文章

  1. POJ 2253 - Frogger - [dijkstra求最短路]

    Time Limit: 1000MS Memory Limit: 65536K Description Freddy Frog is sitting on a stone in the middle ...

  2. 关于dijkstra求最短路(模板)

    嗯....   dijkstra是求最短路的一种算法(废话,思维含量较低,   并且时间复杂度较为稳定,为O(n^2),   但是注意:!!!!         不能处理边权为负的情况(但SPFA可以 ...

  3. Aizu-2249 Road Construction(dijkstra求最短路)

    Aizu - 2249 题意:国王本来有一个铺路计划,后来发现太贵了,决定删除计划中的某些边,但是有2个原则,1:所有的城市必须能达到. 2:城市与首都(1号城市)之间的最小距离不能变大. 并且在这2 ...

  4. ACM - 最短路 - AcWing 849 Dijkstra求最短路 I

    AcWing 849 Dijkstra求最短路 I 题解 以此题为例介绍一下图论中的最短路算法.先让我们考虑以下问题: 给定一个 \(n\) 个点 \(m\) 条边的有向图(无向图),图中可能存在重边 ...

  5. Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)

    problem Bessie is out in the field and wants to get back to the barn to get as much sleep as possibl ...

  6. poj 3463/hdu 1688 求次短路和最短路个数

    http://poj.org/problem?id=3463 http://acm.hdu.edu.cn/showproblem.php?pid=1688 求出最短路的条数比最短路大1的次短路的条数和 ...

  7. 850. Dijkstra求最短路 II

    给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行包 ...

  8. 849. Dijkstra求最短路 I

    给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行包 ...

  9. Dijkstra求次短路

    #10076.「一本通 3.2 练习 2」Roadblocks:https://loj.ac/problem/10076 解法: 次短路具有一种性质:次短路一定是由起点到点x的最短路 + x到y的距离 ...

随机推荐

  1. hdu1011 Starship Troopers

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  2. k8s二进制部署 - coredns安装

    coredns的资源清单文件rabc.yaml apiVersion: v1 kind: ServiceAccount metadata: name: coredns namespace: kube- ...

  3. Docker的OverlayFS存储驱动

    OverlayFS存储驱动 OverlayFS是一个现代的Union Filesystem,类似于AUFS,但速度更快,实现更简单.Docker为OverlayFS提供了两个存储驱动程序:overla ...

  4. 【非原创】codeforces 1029F Multicolored Markers 【贪心+构造】

    题目:戳这里 题意:给a个红色小方块和b个蓝色小方块,求其能组成的周长最小的矩形,要求红色或蓝色方块至少有一个也是矩形. 思路来源:戳这里 解题思路:遍历大矩形可能满足的所有周长,维护最小值即可.需要 ...

  5. (转载)RTMP协议中的AMF数据 http://blog.csdn.net/yeyumin89/article/details/7932585

    为梦飞翔   (转载)RTMP协议中的AMF数据 http://blog.csdn.net/yeyumin89/article/details/7932585 这里有一个连接,amf0和amf3的库, ...

  6. 洛谷p1725 露琪诺 单调队列优化的DP

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; int ...

  7. Inkscape 太慢

    问题: 在画板上图像较多时, 会在粘贴 / 删除 时会陷入长时间的等待 解决: 最佳: Ubuntu上面的比windows上的快上几十倍, 测试比较, 感觉并不是Ubuntu上多用了GPU, 总之, ...

  8. js sort tricks All In One

    js sort tricks All In One js 排序技巧 const arr = [ { label: 'False 1 ', disabled: false, }, { label: 'F ...

  9. v-for & for...in vs for...of

    v-for & for...in vs for...of for..in vs for...of for (const key in object) { if (Object.hasOwnPr ...

  10. HTML5 + JS 网站追踪技术:帆布指纹识别 Canvas FingerPrinting Universally Unique Identifier,简称UUID

    1 1 1 HTML5 + JS  网站追踪技术:帆布指纹识别 Canvas FingerPrinting 1 一般情况下,网站或者广告联盟都会非常想要一种技术方式可以在网络上精确定位到每一个个体,这 ...