【BZOJ 1598】 牛跑步
【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=1598
【算法】
A*求k短路
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1010
#define MAXM 10010
const int INF = 2e9; int i,n,m,k,tot,rtot,u,v,w;
int head[MAXN],rhead[MAXN],dist[MAXN]; struct Edge
{
int to,w,nxt;
} e[MAXM<<];
struct info
{
int s,d;
friend bool operator < (info a,info b)
{
return a.d + dist[a.s] > b.d + dist[b.s];
}
}; inline void add(int u,int v,int w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
tot++;
e[tot] = (Edge){u,w,rhead[v]};
rhead[v] = tot;
}
inline void dijkstra(int s)
{
int i,u,v,w;
static bool visited[MAXN];
priority_queue< pair<int,int> > q;
while (!q.empty()) q.pop();
memset(visited,false,sizeof(visited));
for (i = ; i <= n; i++) dist[i] = INF;
dist[s] = ;
q.push(make_pair(,s));
while (!q.empty())
{
u = q.top().second;
q.pop();
if (visited[u]) continue;
visited[u] = true;
for (i = rhead[u]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (dist[u] + w < dist[v])
{
dist[v] = dist[u] + w;
q.push(make_pair(-dist[v],v));
}
}
}
}
inline void Astar(int s,int t)
{
int i,v,w,cnt = ;
priority_queue< info > q;
info cur;
while (!q.empty()) q.pop();
q.push((info){s,});
while (!q.empty())
{
cur = q.top();
q.pop();
if (cur.s == t)
{
cnt++;
if (cnt <= k) printf("%d\n",cur.d);
else break;
}
for (i = head[cur.s]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
q.push((info){v,cur.d+w});
}
}
for (i = ; i <= k - cnt; i++) printf("-1\n");
} int main()
{ scanf("%d%d%d",&n,&m,&k);
for (i = ; i <= m; i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
dijkstra();
Astar(n,); return ; }
【BZOJ 1598】 牛跑步的更多相关文章
- BZOJ 1598 牛跑步
牛跑步 [问题描述] BESSIE准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘, 然后走回牛棚. BESSIE也不想跑得太远,所以她想走最短的路经. 农场上一共有M ...
- 【BZOJ】1598: [Usaco2008 Mar]牛跑步
[题意]给定有向图,边严格从大编号指向小编号,求前k短路.n<=1000,m<=10000,k<=100. [算法]归并+拓扑排序||A*求第k短路 [题解]因为此题自带拓扑序的特殊 ...
- Bzoj 1598: [Usaco2008 Mar]牛跑步 dijkstra,堆,K短路,A*
1598: [Usaco2008 Mar]牛跑步 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 427 Solved: 246[Submit][St ...
- bzoj 1598: [Usaco2008 Mar]牛跑步 [k短路 A*] [学习笔记]
1598: [Usaco2008 Mar]牛跑步 题意:k短路 ~~貌似A*的题目除了x数码就是k短路~~ \[ f(x) = g(x) + h(x) \] \(g(x)\)为到达当前状态实际代价,\ ...
- bzoj 1598: [Usaco2008 Mar]牛跑步 -- 第k短路,A*
1598: [Usaco2008 Mar]牛跑步 Time Limit: 10 Sec Memory Limit: 162 MB Description BESSIE准备用从牛棚跑到池塘的方法来锻炼 ...
- K短路 (A*算法) [Usaco2008 Mar]牛跑步&[Sdoi2010]魔法猪学院
A*属于搜索的一种,启发式搜索,即:每次搜索时加一个估价函数 这个算法可以用来解决K短路问题,常用的估价函数是:已经走过的距离+期望上最短的距离 通常和Dijkstra一起解决K短路 BZOJ1598 ...
- BZOJ1598: [Usaco2008 Mar]牛跑步
传送门 K短路,普遍的算法是采用AStar求解,先建立反向边跑一遍dij,或者spfa什么的.跑出反向边的距离就可以看为估价函数中的$h()$.设$dist$为当前已经走过的距离,那么$f(node) ...
- BZOJ_1598_[Usaco2008 Mar]牛跑步_A*
BZOJ_1598_[Usaco2008 Mar]牛跑步_A* Description BESSIE准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘, 然后走回牛棚. B ...
- 【BZOJ1598】牛跑步 [A*搜索]
牛跑步 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description BESSIE准备用从牛棚跑到池塘的方 ...
- [bzoj1598][Usaco08Mar]牛跑步_A*_Dijkstra
牛跑步 bzoj-1598 题目大意:给你n个点,m条边的有向图.求从1到n的严格的第k短路. 注释:$1\le n\le 1000$,$1\le m \le 10,000$,$1\le k \le ...
随机推荐
- mounted钩子问题
recommend.vue <script type="text/ecmascript-6"> import Slider from 'base/slider/slid ...
- Spring学习笔记_day01_ioc
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day01 spring是一站式的框架, ...
- PHP 之递归遍历目录与删除
/** * @Description: 递归查询目录文件 * @Author: Yang * @param $path * @param int $level * @return array */ f ...
- hint: not have locally. This is usually caused by another repository pushing
git 提交代码前先pull代码,否则会报如下错误 wangju@wangju-HP-348-G4:~/test/reponselogiccheck$ git statusOn branch mast ...
- Java中Math对象的属性与方法
Math.sqrt() ——————>计算平方根Math.cbrt()————————>计算立方根Math.pow(a, b)——————————>计算a的b次方Math.max( ...
- iptables详解(4):iptables匹配条件总结之一
所属分类:IPtables Linux基础 在本博客中,从理论到实践,系统的介绍了iptables,如果你想要从头开始了解iptables,可以查看iptables文章列表,直达链接如下 iptab ...
- CentOS / RHEL 7 : Chrony V/s NTP (Differences Between ntpd and chronyd)
CentOS / RHEL 7 : Chrony V/s NTP (Differences Between ntpd and chronyd) Chosing between Chrony and N ...
- [测试工具]----iperf
iperf https://sourceforge.net/projects/iperf/ http://downloads.es.net/pub/iperf/ https://github.com/ ...
- Python学习【第6篇】:Python之常用模块1
常用模块一. collocations 模块 时间模块 random模块 os模块 sys模块 序列化模块 re模块 常用模块二:这些模块和面向对象有关 hashlib模块 configparse模块 ...
- 00_Rust安装及Hello World
Rust 官网: https://www.rust-lang.org 版本:nightly.beta.stable 如何设计语言的讨论:https://github.com/rust-lang/rfc ...