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. MIT一牛人对数学在机器学习中的作用给的评述

    MIT一牛人对数学在机器学习中的作用给的评述 转载自http://my.oschina.net/feedao/blog/52252,不过这个链接也是转载的,出处已经无从考证了.   感觉数学似乎总是不 ...

  2. JavaScript组成—— DOM、BOM、ECMAScript

    ECMAScript是JS的核心:提供核心语言功能 DOM(文档对象模型):提供访问和操作网页内容的方法和接口 BOM(浏览器对象模型):提供与浏览器交互的方法和接口 1. DOM(文档对象模型) 1 ...

  3. SSIS 连接数据

    通常情况下,ETL方案需要同时访问两个或多个数据源,并把结果合并为单个数据流,输出到目标表中.为了向目标表中提供统一的数据结构,需要把多个数据源连接在一起.数据连接的另外一种用法,就是根据现有的数据, ...

  4. PowerBI开发 第八篇:查询参数

    在PowerBI Desktop中,用户可以定义一个或多个查询参数(Query Parameter),参数的功能是为了实现PowerBI的参数化编程,使得Data Source的属性.替换值和过滤数据 ...

  5. 关于Map迭代循环,key和value的顺序问题

    使用Hashtable,keySet()返回的顺序为降序(key降顺序) ---->6, 5, 4, 3, 2, 1使用TreeMap,keySet()返回的顺序为升序(key升顺序) ---- ...

  6. 「功能笔记」Spacemacs+Evil备忘录

    设置代理 (setq url-gateway-method 'socks) (setq socks-server '("Default server" "127.0.0. ...

  7. unity ray和line射线检测

    RaycastHit 光线投射碰撞 Struct Structure used to get information back from a raycast. 用来获取从raycast函数中得到的信息 ...

  8. PHP Laravel 连接并访问数据库

    第一次连接数据库 数据库配置位于config/database.php数据库用户名及密码等敏感信息位于.env文件创建一个测试表laravel_course <?php namespace Ap ...

  9. 查询过的问题关于HTML的问题

    一.Bootstrap字体图标(Glyphicons) http://www.runoob.com/bootstrap/bootstrap-glyphicons.html 二.Jquery 实现 “下 ...

  10. 团队项目:安卓端用百度地图api定位显示跑道

    因为安卓调用api对我来说是一个完全陌生的领域,我在经过很长时间终于弄出来了,这段时间还是很有成效的,我得到了历练. 第一步:注册成为百度开发者 在百度地图开放平台创建应用.地址http://lbsy ...