题意:求次短路。

分析:关键是情况讨论。

LL tmpd = x.d + e.dist;

以下情况对应的更新结果

1、tmpd(2) < 最短路(3) < 次短路(4)-------> 最短路 = 2,次短路 = 3

2、tmpd(2) = 最短路(2) < 次短路(3)-------> 最短路 = 2,次短路 = 2

3、最短路(2) < tmpd(3) < 次短路(4)-------> 最短路 = 2,次短路 = 3

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
struct Edge{
int from, to;
LL dist;
Edge(int f, int t, LL d):from(f), to(t), dist(d){}
};
struct HeapNode{
LL d;
int u;
HeapNode(LL dd, int uu):d(dd), u(uu){}
bool operator < (const HeapNode& rhs)const{
return d > rhs.d;
}
};
struct Dijkstra{
int n, m;
vector<Edge> edges;
vector<int> G[MAXN];
LL dist1[MAXN];
LL dist2[MAXN];
void init(int n){
this -> n = n;
for(int i = 0; i < n; ++i) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, LL dist){
edges.push_back(Edge(from, to, dist));
m = edges.size();
G[from].push_back(m - 1);
}
void dijkstra(int s){
priority_queue<HeapNode> Q;
for(int i = 0; i < n; ++i){
dist1[i] = LL_INF;
dist2[i] = LL_INF;
}
dist1[s] = 0;
Q.push(HeapNode(0, s));
while(!Q.empty()){
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if(x.d > dist2[u]) continue;
for(int i = 0; i < G[u].size(); ++i) {
Edge &e = edges[G[u][i]];
LL tmpd = x.d + e.dist;
if(tmpd < dist1[e.to]){
swap(tmpd, dist1[e.to]);
Q.push(HeapNode(dist1[e.to], e.to));
}
else if(tmpd == dist1[e.to]){
dist2[e.to] = tmpd;
Q.push(HeapNode(dist2[e.to], e.to));
}
if(tmpd > dist1[e.to] && tmpd < dist2[e.to]){
dist2[e.to] = tmpd;
Q.push(HeapNode(dist2[e.to], e.to));
}
}
}
}
}dij;
int main(){
int T;
scanf("%d", &T);
while(T--){
int n, m;
scanf("%d%d", &n, &m);
dij.init(n);
int x, y;
LL d;
for(int i = 0; i < m; ++i){
scanf("%d%d%lld", &x, &y, &d);
dij.AddEdge(x - 1, y - 1, d);
dij.AddEdge(y - 1, x - 1, d);
}
dij.dijkstra(0);
printf("%lld\n", dij.dist2[n - 1]);
}
return 0;
}

HDU - 6181 Two Paths(次短路)的更多相关文章

  1. 2017多校第10场 HDU 6181 Two Paths 次短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181 题意:给一个图,求出次短路. 解法:我之前的模板不能解决这种图,就是最短路和次短路相等的情况,证 ...

  2. HDU 6181 Two Paths

    这是一道次短路的题 但是本题有两个坑 注意边权的范围,一定要在所有与距离有关的地方开 long long 本题所求的并不是次短路,而是与最短路不同的最短的路径,如果最短路不止一条,那么就输出最短路的长 ...

  3. HDU 6181:Two Paths(次短路)

    Two Paths Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others) Total S ...

  4. 【hdu 6181】Two Paths

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6181 [题意] 让你求从1到n的次短路 [题解] 模板题; 因为点可以重复走; 则一定会有次短路. di ...

  5. HDU 6181 第k短路

    Two Paths Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)Total ...

  6. HDU 6181:Two Paths(A* + SPFA)

    题目链接 题意 给出n个点m条边的无向图,求次短路. 思路 和 POJ 2449 类似,只不过大小要开成long long. #include <bits/stdc++.h> using ...

  7. HDU 5294 Tricks Device 最短路+最大流

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...

  8. HDU 5889 Barricade(最短路+最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意: 给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点.现在帝国将军要在路径上放置障 ...

  9. hdu 5521 Meeting(最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两 ...

随机推荐

  1. web组件化开发第一天

    技术选型 html5 css3 jq 应用的插件 FullPage.js 一.建一个测试页面,测试静态的功能 <!DOCTYPE html> <html> <head&g ...

  2. HTML中元素 标签 属性

    HTNL中元素是以开始标签开始 结束标签结尾的 如:<p>this is a paragraph </p> <p>是开始标签   </p>是结束标签  ...

  3. Linux之系统优化配置

    Linux系统优化配置 更新国内镜像源 国内速度快的常用更新源如下: ​ http://mirrors.sohu.com ​ http://mirrors.163.com [root@greymous ...

  4. C# Show()与ShowDialog()的区别-----转载

    A.WinForm中窗体显示  显示窗体可以有以下2种方法:  Form.ShowDialog方法 (窗体显示为模式窗体)  Form.Show方法 (窗体显示为无模式窗体) 两者具体区别如下:  1 ...

  5. 【剑指Offer面试编程题】题目1372:最大子向量和--九度OJ

    题目描述: HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天JOBDU测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但 ...

  6. 如何更改linux(centos)下的Apache http端口号

    # vi  /etc/httpd/conf/httpd.conf  文件 修改两个地方     #Listen 12.34.56.78:80     Listen 80     #把80改为你设置的端 ...

  7. python deepcopy的替代方案

    from copy import deepcopy import marshal import timeit from multidict import CIMultiDict def test_de ...

  8. PAT B1019/A1069 数字黑洞

    给定任一个各位数字不完全相同的四位正整数,如果先把四个数字按照非递增排序,再按照非递减排序,然后用第一个数字减第二个数字,将得到一个新的数字,一直重复这样做,很快就会停在有“数字黑洞”之称的6147, ...

  9. myeclipse汉化

    MyEclipse默认安装在计算机用户目录下面,安装完成后对MyEclipse快捷方式使用鼠标右键属性---打开文件位置--进入安装的目录下面即可看到 zh_CN.7z解压缩将zh_CN目录文件放到 ...

  10. docker学习笔记-05:Docker安装mysql和redis

    一.安装mysql 1.docker hub 上查找mysql镜像 docker search mysql 2.从docker hub (使用阿里云加速器)拉取mysql镜像到本地标签为5.6 doc ...