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. 【noi 2.6_9275】&【bzoj 3398】Bullcow(DP){Usaco2009 Feb}

    题意:一共有N只牡牛(公牛)和牝牛(母牛),每2只牡牛间至少要有K只牝牛才不会斗殴.问无斗殴发生的方案数. 解法:f[i][j]表示一共i只牛,最后一只是j(0为牝牛,1为牡牛)的方案数.f[i][0 ...

  2. hdu3652B-number (数位dp)

    Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal for ...

  3. poj1821——Fence

    题意: 一个栅栏一共有n(从1--n)个木板,我们找k个工人去粉刷它,li表示每个人有限制粉刷木板数量,pi表示粉刷一个木板得到的钱,si表示他开始在那个木板前面 如果一个工人要粉刷,那么他必须粉刷s ...

  4. Codeforces ECR86 C. Yet Another Counting Problem(规律,区间)

    题意:给你两个正整数a和b,询问q次,每次给你一个区间[l,r],问[l,r]中有多少数字满足:x%a%b!=a%b%a. 题解:看公式无从下手的题,一般都是要找规律的.首先,我们知道,假如x%a%b ...

  5. Codeforces Global Round 8 D. AND, OR and square sum (贪心,位运算)

    题意:有\(n\)个数,选择某一对数使二者分别\(or\)和\(and\)得到两个新值,求操作后所有数平方和的最大值. 题解:不难发现每次操作后,两个数的二进制表示下的\(1\)的个数总是不变的,所以 ...

  6. CF1462-F. The Treasure of The Segments

    题意: 给出n个线段组成的集合,第i个线段用 \(\{l_i, r_i\}\) 表示线段从坐标轴的点\(l_i\)横跨到点\(r_i\).现在你可以删除其中的一些线段,使得剩下的线段组成的集合中至少存 ...

  7. ArcGIS制作MobileCache

    在使用ArcGIS Mobile进行二次开发时,矢量图层需要制作成MobileCache,才能在手持设备中加载. 下面介绍如何通过ArcMap制作MobileCache: 一.安装ArcGIS Mob ...

  8. kubernetes跑jenkins动态slave

    使用jenkins动态slave的优势: 服务高可用,当 Jenkins Master 出现故障时,Kubernetes 会自动创建一个新的 Jenkins Master 容器,并且将 Volume ...

  9. Web Performance API

    Web Performance API 性能监测/性能优化 https://developer.mozilla.org/en-US/docs/Web/API/Performance https://d ...

  10. js Memory Management

    js Memory Management 垃圾回收是一个术语,在计算机编程中用于描述查找和删除那些不再被其他对象引用的对象的处理过程. 换句话说,垃圾回收是删除任何其他对象未使用的对象的过程. 垃圾收 ...