2016huasacm暑假集训训练三 C - Til the Cows Come Home
题目链接:https://vjudge.net/contest/123674#problem/C
N题目大意是有n个点,然后给出从a点到b点的距离,a和b是互相可以抵达的,则是无向图,问从1到n的最短距离
解题思路:只是一道简单的最短路问题,按照最短路模板就能AC,但这题还有有个主意点,就是重边问题,但我用的是spfa算法,就不需要考虑这个问题,如果是dijkstra算法就要考虑这个问题;
ac代码:
- import java.util.Comparator;
- import java.util.PriorityQueue;
- import java.util.Queue;
- import java.util.Scanner;
- public class Main {
- static int n, m;
- static int[][] map = new int[][];
- static int[] dis = new int[];
- static boolean[] vis = new boolean[];
- static final int Inf = 0x3f3f3f3f;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int u, v, w;
- while (sc.hasNext()) {
- m = sc.nextInt();n = sc.nextInt();
- for (int i = ; i <= n; i++) {
- for (int j = i; j <= n; j++) {
- map[i][j] = Inf;
- map[j][i] = Inf;
- }
- }
- for (int i = ; i < m; i++) { //建图
- u = sc.nextInt();
- v = sc.nextInt();
- w = sc.nextInt();
- if (map[u][v] > w) { //spfa算法,已经考虑了重边问题,所以不需要再考虑
- map[v][u] = w;
- map[u][v] = w;
- }
- }
- spfa();
- System.out.println(dis[n]); //输出1~n的最短距离
- }
- sc.close();
- }
- private static void spfa(int s) {
- for (int i = ; i <= n; i++) {
- vis[i] = false;
- dis[i] = Inf;
- }
- dis[s] = ;
- vis[s] = true;
- Comparator<Integer> cmp = new Comparator<Integer>() {
- public int compare(Integer o1, Integer o2) {
- int i = (int) o1;
- int j = (int) ;
- if (dis[i] > dis[j]) {
- return ;
- } else if (dis[i] == dis[j]) {
- return ;
- } else {
- return -;
- }
- }
- };
- Queue<Integer> q = new PriorityQueue<Integer>(, cmp);
- q.clear();
- q.offer(s);
- while (!q.isEmpty()) {
- int head = q.poll();
- vis[head] = false;
- for (int i = ; i <= n; i++) {
- int temp = dis[head] + map[head][i];
- if (temp < dis[i]) {
- dis[i] = temp;
- if (!vis[i]) {
- q.offer(i);
- vis[i] = true;
2016huasacm暑假集训训练三 C - Til the Cows Come Home的更多相关文章
- 2016huasacm暑假集训训练三 G - 还是畅通工程
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...
- 2016huasacm暑假集训训练三 F - Jungle Roads
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/F 题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使 ...
- 2016huasacm暑假集训训练三 D - Invitation Cards
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/D 题意:一张个向图,求从点1开始到其他各点的最短路权值和加上从其他各点到点1的最短 ...
- 2016huasacm暑假集训训练三 B-Frogger
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/B 题意:一只青蛙在湖中一颗石头上, 它想去有另一只青蛙的石头上,但是 湖里的水很脏 ...
- 2016huasacm暑假集训训练五 H - Coins
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/H 题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士:的士司机可以不找零,但 ...
- 2016huasacm暑假集训训练五 J - Max Sum
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...
- 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...
- 2016huasacm暑假集训训练五 F - Monkey Banana Problem
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...
- 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 ...
随机推荐
- C# Stream 和 byte[] 之间的转换(文件流的应用)
一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...
- 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 ...
- John the Ripper
John the RipperJohn the Ripper(简称John)是一款著名的密码破解工具.它主要针对各种Hash加密的密文.它不同于Rainbow Table方式.它采用实时运算的方式和密 ...
- jQuery跨域
其实jQuery跨域很简单很简单,你记住格式就好,跨域的原理请参考 <jsonp跨域> jQuery跨域代码: $.ajax({ url:'https://suggest.taobao.c ...
- ural 1145. Rope in the Labyrinth
1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...
- 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 ...
- 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 ...
- iOS学习32之UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- 经典收藏 50个jQuery Mobile开发技巧集萃
http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...
- lua 获取文件名和扩展名
local str = "aaa.bbb.bbb.txt" --获取文件名 function getFileName(str) local idx = str:match(&quo ...