poj 2449 Remmarguts' Date 求第k短路 Astar算法
=.=好菜
#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <queue> using namespace std;
const int N = 1e3+;
const int M = +;
typedef long long ll;
const ll INF = 1e15; int n,m,head[N],rehead[N],tot;
struct node {
int v,w,next;
}E[M],reE[M]; void init() {
tot=;
memset(head,,sizeof(head));
memset(rehead,,sizeof(rehead));
}
void addEdge(int u,int v,int w) {
++tot;
E[tot].next = head[u];
head[u]=tot;
E[tot].v = v;
E[tot].w = w; reE[tot].next = rehead[v];
rehead[v]=tot;
reE[tot].v = u;
reE[tot].w = w;
} bool inq[N]; ll d[N];
queue<int>que;
void spfa(int src) {
while(!que.empty())
que.pop();
for(int i=;i<N;i++) d[i]=INF;
memset(inq,,sizeof(inq));
d[src]=;
inq[src] = ;
que.push(src);
while(!que.empty()) {
int now = que.front();
que.pop();
//if(vis[now]) continue;
//vis[now]=1;
inq[now] = ;
for(int i=rehead[now]; i; i=reE[i].next) {
int v = reE[i].v;
int w = reE[i].w;
if(d[v] > d[now] + w) {
d[v] = d[now] + w;
if(!inq[v])
que.push(v);
}
}
}
} struct A {
int v;
ll f,g;
///v是current点 f(v)=g(v)+h(v) g(v):st到v的估值, h(v):v到ed的估值
bool operator<(const A other) const {
if(other.f == f) return other.g < g;
return other.f < f;
}
}; int Astar(int st,int ed,int k) {
priority_queue<A> Q;
if(st==ed) k++;
if(d[st]==INF) return -;
int cnt = ;
A t,tt;
t.v=st,t.g=,t.f=t.g+d[st];
Q.push(t);
while (!Q.empty()) {
tt = Q.top(); Q.pop();
int u=tt.v;
if(u == ed) {
cnt++;
if(cnt==k) return tt.g;
}
for(int i=head[u]; i; i=E[i].next) {
t.v = E[i].v;
t.g = tt.g + E[i].w;
t.f = t.g + d[t.v];
Q.push(t); }
}
return -;
} int main () {
//freopen("in.txt","r",stdin);
while (scanf("%d %d", &n, &m)==) {
init();
for(int i=;i<=m;i++) {
int x,y,z; scanf("%d%d%d",&x,&y,&z);
addEdge(x,y,z);
}
int s,t,k; scanf("%d%d%d",&s, &t, &k);
spfa(t);
//for(int i=1;i<=n;i++) cout << d[i]<<endl;
cout<<Astar(s,t,k)<<endl;
}
return ;
}
poj 2449 Remmarguts' Date 求第k短路 Astar算法的更多相关文章
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- poj 2449 Remmarguts' Date【第K短路】
题目 题意:求 点s 到 点t 的 第 k 短 路的距离: 估价函数=当前值+当前位置到终点的距离 f(n)=g(n)+h(n); g(n)表示g当前从s到p所走的路径的长度, h( ...
- POJ 2449 Remmarguts' Date(第K短路 + A* + 最短路)题解
题意:找出第k短路,输出长度,没有输出-1 思路:这题可以用A*做.A*的原理是这样,我们用一个函数:f = g + h 来表示当前点的预期步数,f代表当前点的预期步数,g代表从起点走到当前的步数,h ...
- POJ 2449 Remmarguts' Date (第k短路径)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions:35025 Accepted: 9467 ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
随机推荐
- git clone远程branch和tag
1.查看远程分支 git branch -r 2.测试git clone romete,只是clone远程remote的master,不会clone其他的目录 -------------------- ...
- tornado : 异步、非阻塞
The terms asynchronous and non-blocking are closely related and are often used interchangeably, but ...
- Mirror--如何在主库上增加文件
由于各种原因,如磁盘不空不足,需要对主库增加数据库文件到其他磁盘上,而镜像服务器上没有对应盘符,很多人会选择删除镜像,重新完备还原来搭建镜像,这种方式耗时耗力. 在做此类操作时,需要对主服务器和镜像服 ...
- npm设置淘宝镜像
npm config set registry https://registry.npm.taobao.org --global npm config set disturl https://npm. ...
- [LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...
- Python 进制转换、位运算
一.进制转换 编程用十进制,十进制转换为二进制.八进制.十六进制 In [135]: bin(23) Out[135]: '0b10111' In [136]: oct(23) Out[136]: ' ...
- HDU 1432 Lining Up(几何)
http://acm.hdu.edu.cn/showproblem.php?pid=1432 题目大意: 2维平面上给定n个点,求一条直线能够穿过点数最多是多少. 解题思路: 因为题目给定的n(1~7 ...
- kendo datetimepicker
@(Html.Kendo().DatePicker() .Name("yearCalendar") .Value(DateTime.Now) .Start(CalendarView ...
- windows上mysql的配置文件my.ini内容
# Other default tuning values# MySQL Server Instance Configuration File# --------------------------- ...
- 认识GMT和UTC时间-附带地理知识
GMT-格林尼治标准时 GMT 的全名是格林威治标准时间或格林威治平时 (Greenwich Mean Time),这个时间系统的概念在 1884 年确立,由英国伦敦的格林威治皇家天文台计算并维护,并 ...