第k最短路A*启发式搜索
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 21549 | Accepted: 5862 |
Description
"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission."
"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)"
Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help!
DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station
twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate.
Input
sideway from A-th station to B-th station with time T.
The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 1009
#define eps 1e-10
#define inf 100000000
#define mod 100000000
#define INF 0x3f3f3f3f
using namespace std;
struct Lnode
{
int u,v,w,next;
}edge[M*300];
int t,head[M],h[M],num[M],use[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void dijstra(int n,int s)
{
int i,j;
memset(use,0,sizeof(use));
memset(h,INF,sizeof(h));
h[s]=0;
for(i=1;i<=n;i++)
{
int mini=INF;
int tep=-1;
for(j=1;j<=n;j++)
{
if(!use[j]&&h[j]<mini)
{
mini=h[j];
tep=j;
}
}
if(tep==-1)break;
use[tep]=1;
for(j=head[tep];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(h[v]>h[tep]+edge[j].w)
h[v]=h[tep]+edge[j].w;
}
}
}
struct node
{
int v,g,h;
friend bool operator<(node a,node b)
{
return a.g+a.h>b.g+b.h;
}
};
int bfs(int n,int s,int t,int k)
{
int i;
priority_queue<node>q;
memset(num,0,sizeof(num));
node cur;
cur.v=s;
cur.g=0;
cur.h=h[s];
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
num[cur.v]++;
if(num[cur.v]>k)continue;
if(num[t]==k&&t==cur.v)return cur.g;
for(i=head[cur.v];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
node now;
now.v=v;
now.g=cur.g+edge[i].w;
now.h=h[v];
q.push(now);
}
}
return -1;
}
struct line
{
int a,b,c;
}e[M*300];
int main()
{
int n,m,i,start,endl,k;
while(scanf("%d%d",&n,&m)!=-1)
{
init();
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
add(e[i].b,e[i].a,e[i].c);
}
scanf("%d%d%d",&start,&endl,&k);
if(start==endl)k++;//注意的地方
dijstra(n,endl);
init();
for(i=1;i<=m;i++)
add(e[i].a,e[i].b,e[i].c);
int ans=bfs(n,start,endl,k);
printf("%d\n",ans);
}
return 0;
}
第k最短路A*启发式搜索的更多相关文章
- K最短路 A*算法
POJ2449 Remmarguts' Date #include <iostream> #include <algorithm> #include <queue> ...
- POJ 2449 Remmarguts' Date --K短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- POJ--2449--Remmarguts' Date【dijkstra_heap+A*】第K短路
链接:http://poj.org/problem?id=2449 题意:告诉你有n个顶点,m条边.并把这些边的信息告诉你:起点.终点.权值.再告诉你s.t.k.需求出s到t的第k短路,没有则输出-1 ...
- 图的第k短路
[问题描述] 给你一个有向图,求从1到n的第k短路. [解法] SPFA+A*搜索. 1 A*算法 A*算法在人工智能中是一种典型的启发式搜索算法,启发中的估价是用估价函数表示的: h(n)=f(n) ...
- A*算法的认识与求第K短路模板
现在来了解A*算法是什么 现在来解决A*求K短路问题 在一个有权图中,从起点到终点最短的路径成为最短路,第2短的路成为次短路,第3短的路成为第3短路,依此类推,第k短的路成为第k短路.那么,第k短路怎 ...
- poj2449(k短路&A_star模板)
题目链接:http://poj.org/problem?id=2449 题意:给出一个有向图,求s到t的第k短路: 思路:k短路模板题,可以用A_star模板过: 单源点最短路径+高级搜索A*;A*算 ...
- BZOJ-1975: 魔法猪学院 (K短路:A*+SPFA)
题意:有N种化学元素,有M种转化关系,(u,v,L)表示化学物质由u变为v需要L能量,现在你有E能量,问最多有多少种不同的途径,使得1转为为N,且总能量不超过E. 思路:可以转为为带权有向图,即是求前 ...
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
随机推荐
- jQuery EasyUI教程之datagrid应用-1
一.利用jQuery EasyUI的DataGrid创建CRUD应用 对网页应用程序来说,正确采集和管理数据通常很有必要,DataGrid的CRUD功能允许我们创建页面来列表显示和编辑数据库记录.本教 ...
- 重点:怎样正确的使用QThread类(注:包括推荐使用QThread线程的新方法QObject::moveToThread)
背景描述: 以前,继承 QThread 重新实现 run() 函数是使用 QThread唯一推荐的使用方法.这是相当直观和易于使用的.但是在工作线程中使用槽机制和Qt事件循环时,一些用户使用错了.Qt ...
- samba的使用
第一步:了解它 为了实现Window主机与Linux服务器之间的资源共享,Linux操作系统提供了 Samba服务,Samba服务为两种不同的操作系统架起了一座桥梁,使 Linux 系统和Window ...
- C++ 程序可以定义为对象的集合
C++ 基本语法C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互.现在让我们简要地看一下什么是类.对象,方法.即时变量. 对象 - 对象具有状态和行为.例如:一只狗的状态 - 颜色 ...
- rdesktop连接远程windows
$ info rdesktop //看一下帮助信息吧$rdesktop 192.168.1.1 //打开了一个8位色彩的,$rdesktop -a 16 192.168.1.1 //这个是16位色 ...
- RAC DBCA 找不到共享磁盘
(一) 前言: 通过vmware workstation 走iscsi协议.安装RAC 集群架构,DBCA 时不能识别ASM 共享存储(按理来说这一版都是权限的问题).同一时候,本想通过RMAN ...
- SharePoint 2013 设置customErrors显示实际的错误信息
一.首先设置IIS中的Web.config文件 找到对应的IIS应用程序目录,如:C:\inetpub\wwwroot\wss\VirtualDirectories\3000 在此文件夹下包含一个we ...
- ros论坛
ros:http://ros.gaitech.net/forum.php makefile:http://blog.csdn.net/shallnet/article/details/38070745 ...
- ubuntu zip解压
您好,zip xx.zip压缩,unzip xx.zip 解压,tar zcvf xx.tar.gz压缩tar zxvf xx.tar.gz解压
- 超全面的JavaWeb笔记day10<Response&Request&路径&编码>
1.Response 2.Request 3.路径 4.编码 请求响应流程图 response 1.response概述 response是Servlet.service方法的一个参数,类型为java ...