Two Paths

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 525    Accepted Submission(s): 265

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

 
Source
 
题解:无向图求第k短路(非次短路) 模板
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
const int maxn=2e5+,maxm=2e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e17+;
struct edge
{
int from,to;
ll w;
} pre[maxn];
vector<edge>G[maxn],T[maxn];
priority_queue<P,vector<P>,greater<P> >q;
ll dist[maxn]; void addedge(int u,int v,ll w)
{
G[u].push_back((edge)
{
u,v,w
});
T[v].push_back((edge)
{
v,u,w
});
}
void dij(int s)
{
dist[s]=0LL;
q.push(P(dist[s],s));
while(!q.empty())
{
P p=q.top();
q.pop();
int u=p.second;
for(int i=; i<T[u].size(); i++)
{
edge e=T[u][i];
if(dist[e.to]>dist[u]+e.w)
{
dist[e.to]=dist[u]+e.w;
q.push(P(dist[e.to],e.to));
}
}
}
}
struct node
{
int to;
///g(p)为当前从s到p所走的路径的长度;dist[p]为点p到t的最短路的长度;
ll g,f;///f=g+dist,f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;
bool operator<(const node &x ) const
{
if(x.f==f) return x.g<g;
return x.f<f;
}
}; ll A_star(int s,int t,int k)
{
priority_queue<node>Q;
if(dist[s]==INF) return -;
int cnt=;
if(s==t) k++;
ll g=0LL;
ll f=g+dist[s];
Q.push((node)
{
s, g, f
});
while(!Q.empty())
{
node x=Q.top();
Q.pop();
int u=x.to;
if(u==t) cnt++;
if(cnt==k) return x.g;
for(int i=; i<G[u].size(); i++)
{
edge e=G[u][i];
ll g=x.g+e.w;
ll f=g+dist[e.to];
Q.push((node)
{
e.to, g, f
});
}
}
return -;
}
void init(int n)
{
for(int i=; i<=n+; i++) G[i].clear(),T[i].clear();
}
int main()
{
int n,m;
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=; i<=m; i++)
{
int u,v;
ll w;
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=; i<=n; i++) dist[i]=INF;
dij(n);
printf("%lld\n",A_star(,n,));
init(n);
}
return ;
}

HDU 6181 第k短路的更多相关文章

  1. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】

    在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...

  3. POJ 2449 Remmarguts' Date --K短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

  4. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  5. BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...

  6. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  7. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  8. 第k短路

    poj 2449 模板题  A*+spfa #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  9. poj 2449(A*求第K短路)

    题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...

随机推荐

  1. 执行力:Just Do It

    执行力,最最关键的一点就是,立即去做,不要想太多. 当有一件事需要去做的时候,你的大脑肯定是接受到了"某种信号",比如来了一个灵感.受到一点启发.做某件事突然来了兴趣.或者想去探讨 ...

  2. ubuntu下安装搜狗输入法

    1.如果系统中未安装依赖fcitx,libssh2-1,或者依赖fcitx,libssh2-1的版本低的话,则需提前安装或者升级,否则安装输入法时会出错 安装命令 sudo apt-get insta ...

  3. Mybatis使用generator自动生成的Example类使用OR条件查询

    参考:https://blog.csdn.net/qq_36614559/article/details/80354511 public List<AssetsDevicetypeRefacto ...

  4. [BZOJ1135][POI2009]Lyz[霍尔定理+线段树]

    题意 题目链接 分析 这个二分图匹配模型直接建图的复杂度太高,考虑霍尔定理. 对于某些人组成的区间,我们只需要考虑他们的并是一段连续的区间的集合.更进一步地,我们考虑的人一定是连续的. 假设我们考虑的 ...

  5. SSIS 遍历目录,把文件内容导入数据库

    最近接手一个项目,程序的基本框架是:程序A导出数据,以.tsv格式存储,数据列之间以tab间隔:程序B吃文档,把数据导入到数据库中,并把处理过的文档备份/移动到指定的目录中.为了快速开发,程序B设计成 ...

  6. Git 使用简记

    目录 git 标签 添加标签 git tag <tagname> ,例:git tag v1.0 添加带有说明的标签 git tag -a v0.1 -m "第一次提交" ...

  7. CentOS7安装OpenStack(Rocky版)-01.控制节点的系统环境准备

    分享一下Rocky版本的OpenStack安装管理经验: OpenStack每半年左右更新一版,目前是版本是201808月发布的版本-R版(Rocky),目前版本安装方法优化较好,不过依然是比较复杂 ...

  8. 分分钟让你理解HTTPS

    一.HTTP存在的问题 1.1 可能被窃听 HTTP 本身不具备加密的功能,HTTP 报文使用明文方式发送 由于互联网是由联通世界各个地方的网络设施组成,所有发送和接收经过某些设备的数据都可能被截获或 ...

  9. Linux内核分析 笔记二 操作系统是如何工作的 ——by王玥

    一.知识要点 1.计算机是如何工作的?(总结)——三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算 ...

  10. 《Linux内核设计与实现》Chapter 2 读书笔记

    <Linux内核设计与实现>Chapter 2 读书笔记 一.获取内核源码 1.使用Git 我们曾经在以前的学习中使用过Git方法 $ git clone git://git.kernel ...