Two Paths

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)

Total Submission(s): 2184 Accepted Submission(s): 856

Problem Description

You are given a undirected graph with n nodes (numbered from 1 to n) and m edges. Alice and Bob are now trying to play a game.

Both of them will take different route from 1 to n (not necessary simple).

Alice always moves first and she is so clever that take one of the shortest path from 1 to n.

Now is the Bob’s turn. Help Bob to take possible shortest route from 1 to n.

There’s neither multiple edges nor self-loops.

Two paths S and T are considered different if and only if there is an integer i, so that the i-th edge of S is not the same as the i-th edge of T or one of them doesn’t exist.

Input

The first line of input contains an integer T(1 <= T <= 15), the number of test cases.

The first line of each test case contains 2 integers n, m (2 <= n, m <= 100000), number of nodes and number of edges. Each of the next m lines contains 3 integers a, b, w (1 <= a, b <= n, 1 <= w <= 1000000000), this means that there’s an edge between node a and node b and its length is w.

It is guaranteed that there is at least one path from 1 to n.

Sum of n over all test cases is less than 250000 and sum of m over all test cases is less than 350000.

Output

For each test case print length of valid shortest path in one line.

Sample Input

2

3 3

1 2 1

2 3 4

1 3 3

2 1

1 2 1

Sample Output

5

3

Hint

For testcase 1, Alice take path 1 - 3 and its length is 3, and then Bob will take path 1 - 2 - 3 and its length is 5.

For testcase 2, Bob will take route 1 - 2 - 1 - 2 and its length is 3

题意

给出一个无向图,求起点到终点的次短路的长度

思路

按照dijkstra模板,多开一个数组来记录次短路的长度,每次更新完最短路的数值之后,最短路和当前路径交换数值,每次求出当前路径的长度大于最短路的长度并且小于次短路的长度时,更新次短路的长度

最后注意数据范围。

AC代码

/*
* @Author: WZY
* @School: HPU
* @Date: 2018-12-05 17:04:37
* @Last Modified by: WZY
* @Last Modified time: 2018-12-10 13:40:56
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define msf(a) memset(a,INF,sizeof(a))
#define msy(a) memset(a,-1,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
inline int read()
{
int X=0,w=1;
char c=getchar();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
return X*w;
}
struct wzy
{
ll Next,to,w;
}edge[maxn];
ll cnt;
ll n,m;
ll head[maxn];
ll dis[maxn];
ll dis1[maxn];
struct node
{
ll u,d;
bool operator < (const node& dui) const {return d>dui.d;}
};
inline void add(ll u,ll v,ll w)
{
edge[cnt].Next=head[u];
edge[cnt].w=w;
edge[cnt].to=v;
head[u]=cnt++;
}
inline void dijkstra()
{
msf(dis);
msf(dis1);
priority_queue<node>q;
q.push((node){1,0});
dis[1]=0;
while(!q.empty())
{
node res=q.top();q.pop();
ll u=res.u,d=res.d;
for(ll k=head[u];~k;k=edge[k].Next)
{
ll v=edge[k].to,w=edge[k].w;
ll dist=d+w;
if(dis[v]>dist)
{
swap(dist,dis[v]);
q.push((node){v,dis[v]});
}
if(dis1[v]>dist&&dis[v]<dist)
{
dis1[v]=dist;
q.push((node){v,dis1[v]});
}
}
}
}
int main(int argc, char const *argv[])
{
int t;
scanf("%d",&t);
while(t--)
{
ll n,m;
msy(head);
scanf("%lld%lld",&n,&m);
ll x,y,z;
while(m--)
{
scanf("%lld%lld%lld",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
dijkstra();
printf("%lld\n",dis1[n]);
}
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(次短路)

    题意:求次短路. 分析:关键是情况讨论. LL tmpd = x.d + e.dist; 以下情况对应的更新结果 1.tmpd(2) < 最短路(3) < 次短路(4)-------> ...

  3. HDU 6181 Two Paths

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

  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. linux服务器用nginx做网站内页之间301的跳转方法

    例: 要将这个页面 /topic/show-228-1.html 做301跳转到 /dance/topic-show-228-1.html nginx的伪静态规则就这样写: rewrite ^/top ...

  2. bootstrap全局样式

    内联子标题: 显示如下: 主体副本: 显示如下: 对齐类: 显示如下: 强调类: 显示如下: 缩略语: 地址: 表格: 加一个class:“table-striped” js里面的奇数偶数行(odd) ...

  3. HFun.快速开发平台(四)=》自定义列表实例(请求参数的处理)

    上编自定义列表描述了自定义列表的基本实现功能,本此记录列表的请求过程. 个人比较喜欢对参数进行对象化,方便后续人维护及查看,先上代码: /******************************* ...

  4. java 随机生成6位短信验证码

    生成6位随机数字其实很简单,只需一行代码,具体如下: String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000) ...

  5. python简单爬虫 用lxml解析页面中的表格

    目标:爬取湖南大学2018年在各省的录取分数线,存储在txt文件中 部分表格如图: 部分html代码: <table cellspacing="0" cellpadding= ...

  6. oralce执行计划

    看懂Oracle执行计划   最近一直在跟Oracle打交道,从最初的一脸懵逼到现在的略有所知,也来总结一下自己最近所学,不定时更新ing… 一:什么是Oracle执行计划? 执行计划是一条查询语句在 ...

  7. vs2017 无法提交到tfs的 git存储库

    tfs 是2018版本 使用git 工具是可以提交成功. 使用vs2017的 就会一直提示 授权失败 也可以使用新安装的git https://blog.csdn.net/Meteor_s/artic ...

  8. Vue源码之----为什么Vue中Array的pop,push等方法可以reactive,而Array[0]='a'这样的方法不会reactive?

    这就要从reactive开始讲起了,粗略的说,js的操作能引起页面上显示的改变,是因为该操作引起了组件的重新渲染,渲染会生成新的虚拟节点,新节点和旧节点会对比,操作浏览器的node进行改变. vue实 ...

  9. nova98 假区域链 骗人项目(vexx.pro的前身)

    首先,我是受害者. nova98前期是vexx.pro,前期推广送比特龙, 送3个,然后推广一个新人可以再拿到1.5个. 然后呢,现在就又推出一个新网站,nova98,把之前推广的人领到币全部清零,而 ...

  10. 2019-04-19-day036-协程与进程池

    内容回顾 11:30 码云 :王老师检查作业+定期抽查 注册账号 考试的时间 threading.enumerate(),能够获取到当前正在运行的所有线程对象列表 守护线程 守护线程会等待所有的非守护 ...