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!& ...
随机推荐
- 【python-opencv】对象测量
opencv 中轮廓特征包括: 如面积,周长,质心,边界框等 *弧长与面积测量 *多边形拟合 *获取轮廓的多边形拟合结果 python-opencv API提供方法: cv2.moments()用来计 ...
- 【PyQt5-Qt Designer】读取txt文件在打印
from PyQt5.QtGui import QFont,QTextDocument,QTextCursor from PyQt5.QtWidgets import QApplication, QM ...
- React-生命周期的相关介绍
1.mounting/组件插入相关 (1)componentWillMount 模板插入前 (2)render 模板插入 (3)componentDidMount 模板插入后 2.Updating/ ...
- HTML PX/EM换算工具 快捷键
换算工具:http://www.runoob.com/tags/ref-pxtoemconversion.html 快捷键:http://www.runoob.com/tags/html-keyboa ...
- test examples/test scripts
//https://www.getpostman.com/docs/v6/postman/scripts/test_examples //Setting an environment variable ...
- [LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- SQL备份数据库代码
SQL备份数据库代码 #region 服务每天备份一次数据库 /// <summary> /// 服务每天备份一次数据库 /// </summary> public void ...
- EF5.0区别于EF4.0的增删改写法
// 实现对数据库的添加功能,添加实现EF框架的引用 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T& ...
- c++实现web服务框架
lamada表达式 声明一个返回数组指针的函数 返回指针数组的函数形式如下所示: 括号必须存在 注意->后不能加() Lambda表达式
- C# 如何把dataTable以参数的形式传入 sql 存储过程
==================================================-- sql代码 示例:CREATE TYPE dbo.Content AS TABLE( ID i ...