题目链接:https://vjudge.net/contest/123674#problem/C

N题目大意是有n个点,然后给出从a点到b点的距离,a和b是互相可以抵达的,则是无向图,问从1到n的最短距离

解题思路:只是一道简单的最短路问题,按照最短路模板就能AC,但这题还有有个主意点,就是重边问题,但我用的是spfa算法,就不需要考虑这个问题,如果是dijkstra算法就要考虑这个问题;

ac代码:

  1. import java.util.Comparator;
  2. import java.util.PriorityQueue;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5. public class Main {
  6. static int n, m;
  7. static int[][] map = new int[][];
  8. static int[] dis = new int[];
  9. static boolean[] vis = new boolean[];
  10. static final int Inf = 0x3f3f3f3f;
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. int u, v, w;
  14. while (sc.hasNext()) {
  15. m = sc.nextInt();n = sc.nextInt();
  16. for (int i = ; i <= n; i++) {
  17. for (int j = i; j <= n; j++) {
  18. map[i][j] = Inf;
  19. map[j][i] = Inf;
  20. }
  21. }
  22. for (int i = ; i < m; i++) { //建图
  23. u = sc.nextInt();
  24. v = sc.nextInt();
  25. w = sc.nextInt();
  26. if (map[u][v] > w) { //spfa算法,已经考虑了重边问题,所以不需要再考虑
  27. map[v][u] = w;
  28. map[u][v] = w;
  29. }
  30. }
  31. spfa();
  32. System.out.println(dis[n]); //输出1~n的最短距离
  33. }
  34. sc.close();
  35. }
  36. private static void spfa(int s) {
  37.  
  38. for (int i = ; i <= n; i++) {
  39. vis[i] = false;
  40. dis[i] = Inf;
  41. }
  42. dis[s] = ;
  43. vis[s] = true;
  44. Comparator<Integer> cmp = new Comparator<Integer>() {
  45.  
  46. public int compare(Integer o1, Integer o2) {
  47. int i = (int) o1;
  48. int j = (int) ;
  49. if (dis[i] > dis[j]) {
  50. return ;
  51. } else if (dis[i] == dis[j]) {
  52. return ;
  53. } else {
  54. return -;
  55. }
  56. }
  57. };
  58. Queue<Integer> q = new PriorityQueue<Integer>(, cmp);
  59. q.clear();
  60. q.offer(s);
  61. while (!q.isEmpty()) {
  62. int head = q.poll();
  63. vis[head] = false;
  64. for (int i = ; i <= n; i++) {
  65. int temp = dis[head] + map[head][i];
  66. if (temp < dis[i]) {
  67. dis[i] = temp;
  68. if (!vis[i]) {
  69. q.offer(i);
  70. vis[i] = true;

2016huasacm暑假集训训练三 C - Til the Cows Come Home的更多相关文章

  1. 2016huasacm暑假集训训练三 G - 还是畅通工程

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...

  2. 2016huasacm暑假集训训练三 F - Jungle Roads

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/F 题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使 ...

  3. 2016huasacm暑假集训训练三 D - Invitation Cards

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/D 题意:一张个向图,求从点1开始到其他各点的最短路权值和加上从其他各点到点1的最短 ...

  4. 2016huasacm暑假集训训练三 B-Frogger

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/B 题意:一只青蛙在湖中一颗石头上, 它想去有另一只青蛙的石头上,但是 湖里的水很脏 ...

  5. 2016huasacm暑假集训训练五 H - Coins

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/H 题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士:的士司机可以不找零,但 ...

  6. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  7. 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...

  8. 2016huasacm暑假集训训练五 F - Monkey Banana Problem

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...

  9. 2016huasacm暑假集训训练五 E - What Is Your Grade?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/E 题意:给做出的题目个数,5个的100分,4个的前n/2的同学95,后n/2的90 ...

随机推荐

  1. C# Stream 和 byte[] 之间的转换(文件流的应用)

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  2. Apache Kafka for Item Setup

    At Walmart.com in the U.S. and at Walmart's 11 other websites around the world, we provide seamless ...

  3. John the Ripper

    John the RipperJohn the Ripper(简称John)是一款著名的密码破解工具.它主要针对各种Hash加密的密文.它不同于Rainbow Table方式.它采用实时运算的方式和密 ...

  4. jQuery跨域

    其实jQuery跨域很简单很简单,你记住格式就好,跨域的原理请参考 <jsonp跨域> jQuery跨域代码: $.ajax({ url:'https://suggest.taobao.c ...

  5. ural 1145. Rope in the Labyrinth

    1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...

  6. ural 1071. Nikifor 2

    1071. Nikifor 2 Time limit: 1.0 secondMemory limit: 64 MB Nikifor has a number x. He doesn't need it ...

  7. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest D. Do it Right!

    D. Do it Right! time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. iOS学习32之UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  9. 经典收藏 50个jQuery Mobile开发技巧集萃

    http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...

  10. lua 获取文件名和扩展名

    local str = "aaa.bbb.bbb.txt" --获取文件名 function getFileName(str) local idx = str:match(&quo ...