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. 2019HDU多校 Round10

    Solved:3 Rank:214 08 Coin 题意:n组硬币 每组有两个 分别有自己的价值 每组的第一个被拿了之后才能拿第二个 问拿1,2....2n个硬币的最大价值 题解:之前贪心带反悔的做法 ...

  2. 1569: Wet Tiles

    Description Alice owns a construction company in the town of Norainia, famous for its unusually dry ...

  3. 一维二维Sparse Table

    写在前面: 记录了个人的学习过程,同时方便复习 Sparse Table 有些情况,需要反复读取某个指定范围内的值而不需要修改 逐个判断区间内的每个值显然太浪费时间 我们希望用空间换取时间 ST表就是 ...

  4. Codeforces Round #295 (Div. 2) B. Two Buttons (DP)

    题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\). 题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2 ...

  5. Codeforces #6241 div2 C. Orac and LCM (数学)

    题意:给你一个数列,求所有子序列对的\(lcm\),然后求这些所有\(lcm\)的\(gcd\). 题解:我们对所有数分解质因数,这里我们首先要知道一个定理: ​ 对于\(n\)个数,假如某个质数\( ...

  6. Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)

    题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...

  7. Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships

    Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a lin ...

  8. 包机制和javadoc

    包机制 一般用公司域名倒置作为包名: eg com.baidu.www 可以在src里自己鼠标右键建立包,包内可以存放代码, 包的路径必须在最上面, eg:package com.kuang; (自动 ...

  9. Logstash 日志收集(补)

    收集 Tomcat 日志 安装 Tomcat # 安装 jdk [root@web01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm # 下载 [root@web01 ~] ...

  10. LEETCODE - 1181【前后拼接】

    class Solution { public: string gethead(string str){//获取头单词 string ret = ""; int strlen = ...