POJ 2449
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1010;
const int MAXM = 100010;
const int INF = 0x7fffffff;
typedef struct{
int to, next, w;
}Edge;
typedef struct Node{
int p, g, h;
Node(int p, int g, int h){
this->p = p;
this->g = g;
this->h = h;
}
bool operator < (const Node &node) const{
return node.g+node.h < g+h;
}
}Node;
Edge edge[MAXM << 1];
int n, m;
int head[MAXN], tail[MAXN], dist[MAXN];
int cnt[MAXN], vis[MAXN];
void addEdge(int u, int v, int w, int k){
edge[k].to = v;
edge[k].w = w;
edge[k].next = head[u];
head[u] = k++;
edge[k].to = u;
edge[k].w = w;
edge[k].next = tail[v];
tail[v] = k;
}
void init(int s){
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
for(int i = 1;i <= n;i ++) dist[i] = INF;
dist[s] = 0;
vis[s] = 1;
}
void spfa(int s){
init(s);
queue<int>q;
q.push(s);
while(!q.empty()){
int p = q.front();
vis[p] = 0;
q.pop();
for(int i = tail[p];~i;i = edge[i].next){
int u = edge[i].to;
if(dist[u] > dist[p] + edge[i].w){
dist[u] = dist[p] + edge[i].w;
if(!vis[u]){
vis[u] = 1;
q.push(u);
}
}
}
}
}
int aStar(int s, int t, int k){
priority_queue<Node>Q;
if(dist[s] == INF) return -1;
Q.push(Node(s, 0, dist[s]));
while(!Q.empty()){
Node tt = Q.top();
cnt[tt.p] ++ ;
if(cnt[tt.p] > k) continue;
if(cnt[tt.p] == k && tt.p == t) return tt.g+tt.h;
Q.pop();
for(int i = head[tt.p];~i;i = edge[i].next){
int u = edge[i].to;
Q.push(Node(u, tt.g+edge[i].w, dist[u]));
}
}
return -1;
}
int main(){
int s, t, k;
int u, v, w;
//freopen("in.c", "r", stdin);
while(~scanf("%d%d", &n, &m)){
memset(head, -1, sizeof(head));
memset(tail, -1, sizeof(tail));
k = 1;
for(int i = 1;i <= m;i ++){
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w, k);
k += 2;
}
scanf("%d%d%d", &s, &t, &k);
if(s == t) k++;
spfa(t);
int ans = aStar(s, t, k);
printf("%d\n", ans);
}
return 0;
}
POJ 2449的更多相关文章
- poj 2449 Remmarguts' Date 【SPFA+Astar】【古典】
称号:poj 2449 Remmarguts' Date 意甲冠军:给定一个图,乞讨k短路. 算法:SPFA求最短路 + AStar 以下引用大牛的分析: 首先,为了说话方便,列出一些术语: 在启示式 ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- poj 2449(A*求第K短路)
题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...
- 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短路
题目链接:http://poj.org/problem?id=2449 #include<cstdio> #include<cstring> #include<iostr ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- poj 2449 k短路+A*算法
http://poj.org/problem?id=2449 K短路的定义: 1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样. 2.每个顶点和每条边都 ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- 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
[题目链接] http://poj.org/problem?id=2449 [算法] A*(启发式搜索) 首先,求第k短路可以用优先队列BFS实现,当T第k次入队时,就求得了第k短路,但是,这种做法的 ...
随机推荐
- 一个css3流程导图
这也是公司用到的,写个demo出来分享 <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...
- 大型B/S系统技术总结(不断更新)
看了<淘宝技术这十年>和<大型网站系统与Java中间件实践>这些书,对大型B/S系统的构建越来越感兴趣,于是尝试收集和总结一些常用的技术手段.不过大型网站的架构是根据业务需求不 ...
- TypeScript学习指南第一章--基础数据类型(Basic Types)
基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...
- 角色控制器 Character Controller
Unity中,1个单位尺寸代表1米.即在Unity中创建一个Cube的尺寸是1x1x1米大小. Unity推荐把人的身高定为大约2个Unity单位高度(2米). 为了截取角色的全身照,需要把角色Ins ...
- check whether the crystal report runtime is exists 检查crystalreport运行时是否存在
1. Try Dim rptDoc As New CrystalDecisions.CrystalReports.Engine.ReportClass() Dim rptView As New Cry ...
- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试]:谢勤政-11061197,吴润凡-11061185
一,关于结对编程 结对编程的优点: 1)在开发层次,结对编程能提供更好的设计质量和代码质量,两人合作能有更强的解决问题的能力. 2)对开发人员自身来说,结对工作能带来更多的信心,高质量的产出能带来更高 ...
- SpringMVC+Hibernate架构save方法事务未提交
今天同事遇到一个问题,一起研究,最后解决,让我对spring的事务管理又加深了印象. 先简单说一下项目:项目是Spring和Hibernate集成的JavaEE项目,MVC架构. 外包在service ...
- [转].NET程序在windows操作系统上独立运行的技术要点
发现一个不错的网站,转载一篇文章方便查看 转自 http://www.linuxdot.net/bbsfile-3354 ===================================== ...
- PHP file_exists() 函数
定义和用法 file_exists() 函数检查文件或目录是否存在. 如果指定的文件或目录存在则返回 true,否则返回 false. 语法 file_exists(path) 参数 描述 path ...
- php 数组指针相关函数current(),next(),prev(),end()
mixed current(array target_array) current()函数返回位于target_array数组当前指针位置的数组值.与next().prev().和end()函数不同, ...