题目链接

题意

给出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. v-charts显示标题

    使用v-charts的时候,如果要显示标题需要以下操作 1. 加入:title props <ve-pie :title="chartTitle" :data="c ...

  2. WPF 寻找控件模板中的元素

    <Window x:Class="Wpf180706.Window10"        xmlns="http://schemas.microsoft.com/wi ...

  3. MyBatis 问题 & 解决

    # 问题 Invalid bound statement (not found) # 解决 <mappers> 标签的包括的是 SQL 语句存在的地方,此外 <mapper> ...

  4. WPF滚动条嵌套,响应鼠标滑轮事件的处理

    在C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果是鼠标滚动整个S ...

  5. box-shadow 与 filter:drop-shadow 详解及技巧

    box-shadow 在前端的 CSS 编写工作想必十分常见.但是 box-shadow 除去它的常规用法,其实还存在许多不为人知的奇技淫巧. 喜欢 markdown 版本的可以戳这里. box-sh ...

  6. Qt之使用setWindowFlags方法遇到的问题(追踪进入QWidget的源码分析原因,最后用WINAPI解决问题)good

    一.简述 前段时间在使用setWindowFlags方法时遇到了一个坑,具体情况是想通过窗口界面上一个checkBox来控制窗口当前状态是否置顶,而Qt提供了Qt::WindowStaysOnTopH ...

  7. 常用json解析库比较及选择 fastjson & gson

    一.常用json解析库比较及选择 1.简介 fastjson和gson是目前比较常用的json解析库,并且现在我们项目代码中,也在使用这两个解析库. fastjson 是由阿里开发的,号称是处理jso ...

  8. 三种扩展 Office 软件功能的开发模型对比 – Office Add-In Model, VBA 和 VSTO

    当 Office 用户需要针对文档自定义新功能时,可以求助于 VBA 或者 VSTO 两种方式.Office 2013 富客户端以后,微软为 Office 平台上的开发者提供了一种新模型 --- Of ...

  9. Openssl - Static libraries (w32, mingw) 以及对Qt静态编译时的设置

    Openssl static libraries created for Windows 32bit using MinGW compiler   Compiled with:       ./Con ...

  10. hadoop 数据抽取

    #!/bin/bash if [ ! -z $2 ]; then start_time=$1 end_time=$2 else starttime=`date +%Y%m%d%H%M -d '-15 ...