题目链接

题意

给出n个点m条边的无向图,求次短路。

思路

POJ 2449 类似,只不过大小要开成long long。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100011;
const int INF = 0x3f;
struct Edge {
int v, nxt; LL w;
} edge[N*2];
struct Node {
int u; LL g, h;
friend bool operator < (const Node &a, const Node &b) {
return a.g + a.h > b.h + b.g;
}
};
int n, m, vis[N], head[N], tot;
LL h[N]; void Add(int u, int v, LL w) {
edge[tot] = (Edge) { v, head[u], w }; head[u] = tot++;
edge[tot] = (Edge) { u, head[v], w }; head[v] = tot++;
} void Spfa(int s, int t) {
memset(h, INF, sizeof(h));
memset(vis, 0, sizeof(vis));
queue<int> que; que.push(t);
vis[t] = 1; h[t] = 0;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = 0;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(h[v] > h[u] + w) {
h[v] = h[u] + w;
if(!vis[v]) vis[v] = 1, que.push(v);
}
}
}
} LL Astar(int s, int t, int k) {
Node now = (Node) { s, 0LL, h[s] };
priority_queue<Node> que; que.push(now);
int cnt = 0;
while(!que.empty()) {
now = que.top(); que.pop();
int u = now.u; LL gg = now.g, hh = now.h;
// printf("%d : %d - %d\n", u, gg, hh);
if(u == t) cnt++;
if(cnt == k) return gg + hh;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v; LL w = edge[i].w;
now = (Node) { v, gg + w, h[v] };
que.push(now);
}
}
return 0;
} int main() {
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head)); tot = 0;
for(int i = 1; i <= m; i++) {
int u, v; LL w;
scanf("%d%d%lld", &u, &v, &w);
Add(u, v, w);
}
Spfa(1, n);
printf("%lld\n", Astar(1, n, 2));
}
return 0;
}

HDU 6181:Two Paths(A* + SPFA)的更多相关文章

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

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

  2. HDU 2732:Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...

  3. HDU 4055:Number String(DP计数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个仅包含‘I','D','?'的字符串,’I'表示前面的数字比后面的数字要小(Increase升 ...

  4. HDU 4417:Super Mario(主席树)

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意是:给出n个数和q个询问,每个询问有一个l,r,h,问在[l,r]这个区间里面有多少个数是小于等于h的 ...

  5. HDU 5898:odd-even number(数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5898 题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数.(1< ...

  6. HDU 1520:Anniversary party(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description   There i ...

  7. HDU 2089:不要62(数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Problem Description   杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer) ...

  8. HDU 5787:K-wolf Number(数位DP)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5787 题意:要求相邻的K个位的数不能相同,在[L,R]区间有多少个这样的数. #inclu ...

  9. HDU 5818:Joint Stacks(stack + deque)

    http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description   A stack is a data ...

随机推荐

  1. WPF中Binding使用StringFormat格式化字符串方法

    原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...

  2. WPF特效-鱼游动动画2

    原文:WPF特效-鱼游动动画2           纯代码撸动画实践2:           原图:(png格式)                                            ...

  3. WPF学习目录

    基本概念 数据源Source-目标Target WPF生命周期 App.xaml 依赖属性 WPF路由 线程操纵UI问题 利用属性中设置.查看DataContext/Command等 分析布局 写数据 ...

  4. DataGridTemplateColumn

    DataGridTemplateColumn自定义单元格样式 <DataGrid Grid.Row="0" Name="BasicRuleDataGrid" ...

  5. Qt设置窗口的初始大小(使用sizeHint这个虚函数,或者在构造函数里使用resize函数)

    我们用qt创建一个窗口,先后显示它,代码如下: class Mywindow : public QMainWindow{ ..... } int main( int argc, char** argv ...

  6. 图像滤镜艺术---Oilpaint油画滤镜

    原文:图像滤镜艺术---Oilpaint油画滤镜  Oilpaint油画滤镜     图像油画效果实际上是将图像边缘产生一种朦胧,雾化的效果,同时,将一定的边缘模糊化,这样图像整体上看去像素与像素之间 ...

  7. 用C#修改系统区域和语言设置

    原文:用C#修改系统区域和语言设置 这几天做项目,因为客户机的系统不同,发现客户机的区域和语言设置也不尽相同,导致程序运行时根据时间判断的很多属性和方法都出现各种各样的千奇百怪的问题. 修改程序太费时 ...

  8. UWP-磁贴初识

    原文:UWP-磁贴初识 简单的磁贴内容实现,来自 Bob 的视频. 为一个按钮添加点击事件,来更新磁贴. private void ChangeTileContentButton_Click(obje ...

  9. Linux简单文本处理

    tr命令:tr [option] set1 [set2] 删除或者替换set1中的字符在文本表示这个问题中,windows系统下,\r\n为换行:而linux系统下,\n为换行.win->lin ...

  10. RIO的性能

    看了一下微软官网RIO没有达到四五倍的宣称(而且必须在windows 2012r2才可以)最多一倍github.com/aspnet/benchmarks测试代码可以从github.com/zelia ...