HDU 6181 第k短路
Two Paths
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 525 Accepted Submission(s): 265
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.
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.
3 3
1 2 1
2 3 4
1 3 3
2 1
1 2 1
3
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
#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短路的更多相关文章
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】
在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...
- POJ 2449 Remmarguts' Date --K短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- 第k短路
poj 2449 模板题 A*+spfa #include<iostream> #include<cstdio> #include<cstring> #inclu ...
- poj 2449(A*求第K短路)
题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...
随机推荐
- MySQL主从报错1594
一.主从报错 Relay log read failure 问题原因,MySQL主从使用的是kvm虚拟机,物理机超分严重,在负载高的情况下会kill掉占用资源最多的虚拟机,再启动后导致主从失败 mys ...
- ECMAScript6——Set数据结构
/** * 数据结构 Set */ // ----------------------------------------------------- /** * 集合的基本概念:集合是由一组无序且唯一 ...
- 真机调试傻瓜图文教程(Xcode6.4)
先准备好99刀,真机调试才带你玩. PS:万能宝十来块钱可以买个资格... Developer Apple上的设置 1.打开https://developer.apple.com/,点击Member ...
- 基于Shader实现的UGUI描边解决方案
基于Shader实现的UGUI描边解决方案 前言 大扎好,我系狗猥.当大家都以为我鸽了的时候,我又出现了,这也是一种鸽.创业两年失败后归来,今天想给大家分享一个我最近研究出来的好康的,比游戏还刺激,还 ...
- cocos2d-x学习之路(三)——精灵与动作
这里我们来看看所有游戏引擎中都会出现的一个重要的概念——精灵
- 前端常见算法面试题之 - 二维数组中的查找[JavaScript解法]
--------------------- 作者:吴潇雄 来源:CSDN 原文:https://blog.csdn.net/weixin_43439741/article/details/835118 ...
- Ajax引擎:ajax请求步骤详细代码
说起AJAX,可能是很多同学在很多地方都看到过,各大招聘网站上对于WEB前端和PHP程序员的技能要求清单中也是必不可少的一项.但是,ajax请求步骤详细代码以及说明却比较少见到 什么是AJAX引擎? ...
- CentOS Docker环境搭建教程
安装与配置 Docker 安装 Docker Docker 软件包已经包括在默认的 CentOS-Extras 软件源里.因此想要安装 docker,只需要运行下面的 yum 命令: yum inst ...
- B1041. 考试座位号(15)
这题比较简单,没有调试,一次通过,虽然简单,不过也有借鉴意义. #include<bits/stdc++.h> using namespace std; const int N=1005; ...
- 作业6小学生四则运算测试APP的NABCD模型
小学生四则运算测试NABCD模型 组员:李新,朱浩龙,叶煜稳,陈俊金,林德麟 (1) N (Need 需求) 需求分析: 四则运算是小学生学习数学的核心和基础,必须经过大量的练习才能熟练掌握,但是一 ...