poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u013081425/article/details/26729375
http://poj.org/problem?id=2449
大致题意:给出一个有向图,求从起点到终点的第K短路。
K短路与A*算法具体解释 学长的博客。。
。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#define LL long long
#define _LL __int64
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1010;
const int maxm = 100010;
struct node
{
int u,v,w;
};
int s,t,k;
int n,m;
vector <struct node> edge[maxn],edge1[maxn]; //邻接表存图以及反向图
int dis[maxn]; // 终点到全部点的最短路
int time[maxn];// 每一个点的出队列次数
int ans;
bool operator > (const struct node &a, const struct node &b)
{
return a.w+dis[a.v] > b.w + dis[b.v];
}
priority_queue < node, vector<node>, greater<node> >q;
void init()
{
for(int i = 1; i <= n; i++)
{
edge[i].clear();
edge1[i].clear();
}
}
//spfa求终点到其它左右点的最短路
void spfa()
{
int inque[maxn];
queue<int> que;
while(!que.empty()) que.pop();
memset(inque,0,sizeof(inque));
memset(dis,INF,sizeof(dis));
dis[t] = 0;
inque[t] = 1;
que.push(t);
while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = 0;
for(int i = 0; i < (int)edge1[u].size(); i++)
{
int v = edge1[u][i].v;
int w = edge1[u][i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(!inque[v])
{
inque[v] = 1;
que.push(v);
}
}
}
}
}
void solve()
{
while(!q.empty()) q.pop();
memset(time,0,sizeof(time));
struct node tmp;
bool flag = false;
//起点进队列
tmp.v = s;
tmp.w = 0;
q.push(tmp);
while(!q.empty())
{
struct node u = q.top();
q.pop();
time[u.v]++;
if(time[u.v] >= k) //出队次数大于等于K时
{
if(u.v == t) //假设是终点,推断与起点是否同样
//若不同样,当前值便是第K短路。否则第K+1次才是最短路
{
if(t != s || (t == s && time[u.v] == k+1))
{
flag = true;
ans = u.w;
break;
}
}
if(time[u.v] > k)//假设不是终点。当出队次数大于K次就不再进队列
continue;
}
int now = u.v;
for(int i = 0; i < (int)edge[now].size(); i++)
{
struct node tmp;
tmp.v = edge[now][i].v;
tmp.w = u.w + edge[now][i].w;
q.push(tmp);
}
}
if(!flag)
ans = -1;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
init();
int u,v,w;
for(int i = 1; i <= m; i++)
{
scanf("%d %d %d",&u,&v,&w);
edge[u].push_back( (struct node){u,v,w} );
edge1[v].push_back( (struct node) {v,u,w} );
}
scanf("%d %d %d",&s,&t,&k);
spfa();
solve();
printf("%d\n",ans);
}
return 0;
}
poj 2449 Remmarguts' Date(K短路,A*算法)的更多相关文章
- 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*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- POJ 2449 Remmarguts' Date --K短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
- 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 求第K短路
第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- 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短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
随机推荐
- js获取表格视图所选行号的ids
实例化数组 遍历所选行push到数组中 将数组join转换为以,分割的字符串 /*获取指定id的datagrid的表格视图的选择的ids*/ function getDataGridSelectRow ...
- MySQL 事件跟踪器
/*第一步 创建以下两个 日志表 */ CREATE TABLE `slow_log` ( `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMEST ...
- OC 复合
在Objective-C中复合是通过包含作为实例变量的对象指针实现的 严格来说,只有对象间的组合才叫复合 --------------------Car.h---------------------- ...
- python 利用quick sort思路实现median函数
# import numpy as np def median(arr): #return np.median(arr) arr.sort() return arr[len(arr)>>1 ...
- 提高C++程序运行效率的10个简单方法
转载: 一.尽量减少值传递,多用引用来传递参数.至于其中的原因,相信大家也很清楚,如果参数是int等语言自定义的类型可能能性能的影响还不是很大,但是如果参数是一个类的对象,那么其效率问题就不言而喻了. ...
- Inno Setup 编译器操作
Inno Setup 编译器 1◆ 下载inno ha_innosetup5502_skygz_DownG.com 2◆ 安装 next 3◆ 操作 success 4◆ 测试安装 5◆ 卸载 uni ...
- 我的一起开源网 www.17ky.net上线了
.net开源生态的落后,使得.net开发人员所拥有的开源资源比其他语言的开发者少了很多,这也使得笔者很早之前就喜欢收集各种开源项目,经常会去逛codeplex,开源中国社区等网站,同时也喜欢在自己或公 ...
- bga
本文记录在bga布线的难题. 1一开始就要预留好布线局域. 最近出现布线太密,修改时就麻烦了. http://bbs.elecfans.com/jishu_521995_1_1.html
- java并发编程之三--CyclicBarrier的使用
CyclicBarrier 允许一组线程全部等待彼此达到共同屏障点的同步辅助. 循环阻塞在涉及固定大小的线程方的程序中很有用,这些线程必须偶尔等待彼此. 屏障被称为循环 ,因为它可以在等待的线程被释放 ...
- OO面向对象 课程总结
测试与正确性论证的效果差差异 测试和正确性论证都是对程序进行可靠性的验证. 测试: IEEE提出了对软件测试的定义:使用人工或者自动的手段来运行或测定某个系统的过程,其目的在于检验它是否满足规定的需求 ...