HDU 6181:Two Paths(A* + SPFA)
题意
给出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)的更多相关文章
- HDU 6181:Two Paths(次短路)
Two Paths Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others) Total S ...
- HDU 2732:Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...
- HDU 4055:Number String(DP计数)
http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个仅包含‘I','D','?'的字符串,’I'表示前面的数字比后面的数字要小(Increase升 ...
- HDU 4417:Super Mario(主席树)
http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意是:给出n个数和q个询问,每个询问有一个l,r,h,问在[l,r]这个区间里面有多少个数是小于等于h的 ...
- HDU 5898:odd-even number(数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5898 题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数.(1< ...
- HDU 1520:Anniversary party(树形DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description There i ...
- HDU 2089:不要62(数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer) ...
- HDU 5787:K-wolf Number(数位DP)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5787 题意:要求相邻的K个位的数不能相同,在[L,R]区间有多少个这样的数. #inclu ...
- HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data ...
随机推荐
- windows常用cmd指令
打开命令行 1.在菜单栏中搜索命令行 2.在文件管理器的Path栏输入cmd,则在当前目录打开命令行 3.Windows+R,输入cmd,回车 ping(网络诊断工具) ping是Windows.Un ...
- WPF依赖项属性不需要包装属性也可以工作
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- socket 主机地址相关的函数
#include <arpa/inet.h> int inet_aton (const char *name, struct in_addr *addr) 将ipv4地址从数字点的形式转化 ...
- 规则“Microsoft Visual Studio 2008 的早期版本”失败。此计算机上安装了 Microsoft Visual Studio 2008 的早期版本。请在安装 SQL Server 2008 前将 Microsoft Visual Studio 2008 升级到 SP1。
今天重装了一下系统后,需要装开发工具,我用的开发工具是Visual Studio2008 和SQL Server2008R2,装完Visual Studio2008的时候在装数据库的时候却出现这样的问 ...
- Office Add-in Model 为 Outlook Mail Add-in 提供的 JavaScript API 介绍
本文所讨论的 Mailbox API是指在 Mail Add-in 中可调用的 JavaScript API.开发者可以利用这些API 实现 Add-in 和 Outlook 的交互(数据读取与写入) ...
- configure -help
http://www.360doc.com/content/14/1215/17/18578054_433158382.shtml http://blog.csdn.net/mociml/articl ...
- 深入浅出RPC——深入篇(转载)
本文转载自这里是原文 <深入篇>我们主要围绕 RPC 的功能目标和实现考量去展开,一个基本的 RPC 框架应该提供什么功能,满足什么要求以及如何去实现它? RPC 功能目标 RPC的主要功 ...
- kafka设计原理(转)
一.kafka简介 1.1 背景历史 当今社会各种应用系统,诸如商业.社交.搜索.浏览等信息工厂一样不断被生产出各种信息,在大数据时代,我们面临如下几个挑战: 如何收集这些巨大的信息 如何分析它 如何 ...
- KM算法 详解+模板
先说KM算法求二分图的最佳匹配思想,再详讲KM的实现.[KM算法求二分图的最佳匹配思想] 对于具有二部划分( V1, V2 )的加权完全二分图,其中 V1= { x1, x2, x3, ... , x ...
- JVM检测&工具
前几篇篇文章介绍了介绍了JVM的参数设置并给出了一些生产环境的JVM参数配置参考方案.正如之前文章中提到的JVM参数的设置需要根据应用的特性来进行设置,每个参数的设置都需要对JVM进行长时间的监测,并 ...