题目链接

题意

给出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. Angularjs html文本显示

    <body ng-app="siteApp"> <div ng-controller="newsDetailController as vm" ...

  2. WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂

    原文:WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂 先上效果图 正常样式 拖动时样式 好下面 开始吧 ==================================== ...

  3. FTPHelper

    转载自 :https://blog.csdn.net/jiankunking/article/details/50016043 using System; using System.Collectio ...

  4. Qt 事件处理 快捷键(重写eventFilter的函数,使用Qt::ControlModifier判断)

    CTRL+Enter发送信息的实现 在现在的即时聊天程序中,一般都设置有快捷键来实现一些常用的功能,类似QQ可以用CTRL+Enter来实现信息的发送. 在QT4中,所有的事件都继承与QEvent这个 ...

  5. painter半透明的 底层窗口全透明背景

  6. FMX 动态创建 和 销毁(释放free) 对象

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  7. sklearn中LinearRegression使用及源码解读

    sklearn中的LinearRegression 函数原型:class sklearn.linear_model.LinearRegression(fit_intercept=True,normal ...

  8. ASP.NET WebForm项目--页面正确返回404及500等状态

    实现方法 项目路径 <system.webServer> <httpErrors errorMode="Custom" > <remove statu ...

  9. 开源中国的 IT 公司开源软件整理计划介绍

    直击现场 <HTML开发MacOSApp教程>  http://pan.baidu.com/s/1jG1Q58M 开源中国的 IT 公司开源软件整理计划介绍 oschina 发布于: 20 ...

  10. 插件化二(Android)

    插件化二(Android) 上一篇文章<插件化一(android)>里大概构思了下插件加载与校验的流程和一些大体设计,这次就具体展开,在<动态加载与插件化>里提到以apk形式开 ...