Remmarguts’ Date(poj 2449)
求第k短路的长度,如果没有,输出-1。
/*
k短路模板
注意当s=t时,k++。
*/
#include<iostream>
#include<cstdio>
#include<queue>
#define N 1010
#define M 100010
using namespace std;
int head1[N],head2[N],dis[N],vis[N],n,m,k,cnt;
bool flag;
struct node
{
int v,pre,t;
};node e1[M],e2[M];
struct Node
{
int from,g,f;
bool operator< (Node x) const
{
if(x.f!=f)return x.f<f;
return x.g<g;
}
};
void add(int i,int x,int y,int z)
{
e1[i].v=y;
e1[i].t=z;
e1[i].pre=head1[x];
head1[x]=i;
e2[i].v=x;
e2[i].t=z;
e2[i].pre=head2[y];
head2[y]=i;
}
void spfa(int s,int t)
{
queue<int> q;
for(int i=;i<=n;i++)dis[i]=N*M;
vis[s]=;dis[s]=;
q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();
vis[x]=;
for(int i=head2[x];i;i=e2[i].pre)
if(dis[e2[i].v]>dis[x]+e2[i].t)
{
dis[e2[i].v]=dis[x]+e2[i].t;
if(!vis[e2[i].v])
{
vis[e2[i].v]=;
q.push(e2[i].v);
}
}
}
}
void a_star(int s,int t)
{
if(s==t)k++;
priority_queue<Node> q;
Node u;u.from=s;u.g=;u.f=dis[s];
q.push(u);
while(!q.empty())
{
u=q.top();q.pop();
if(u.from==t)
{
cnt++;
if(cnt==k)
{
printf("%d",u.f);
flag=true;
return;
}
}
for(int i=head1[u.from];i;i=e1[i].pre)
{
Node v;
v.from=e1[i].v;
v.g=u.g+e1[i].t;
v.f=v.g+dis[e1[i].v];
q.push(v);
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(i,x,y,z);
}
int s,t;
scanf("%d%d%d",&s,&t,&k);
spfa(t,s);
a_star(s,t);
if(!flag)printf("-1");
return ;
}
Remmarguts’ Date(poj 2449)的更多相关文章
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- POJ 2449:Remmarguts' Date(A* + SPFA)
题目链接 题意 给出n个点m条有向边,源点s,汇点t,k.问s到t的第k短路的路径长度是多少,不存在输出-1. 思路 A*算法是启发式搜索,通过一个估价函数 f(p) = g(p) + h(p) ,其 ...
- 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短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...
- POJ 2449 Remmarguts' Date (第k短路径)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions:35025 Accepted: 9467 ...
- POJ——2449Remmarguts' Date(A*+SPFA)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26504 Accepted: 7203 ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
随机推荐
- spring的annotation
spring容器创建bean对象的方式: 1,使用反射调用无参构造器来创建实例(前提是这个类有无参构造器)(常规方式) 2,通过工厂类获得实例(工厂类实现了接口FactoryBean<?> ...
- [C和指针] 4-语句、5-操作符和表达式
第4章 语句 4.1 表达式语句 C并不存在专门的"赋值语句",赋值就是一种操作,就像加法和减法一样,所以赋值就在表达式内进行. 你只要在表达式后面加上一个分号,就可以把表达式转变 ...
- [笔试面试题] 3-C++关键字篇
C/C++关键字篇 语言是编程的基础,掌握基本的语言知识是编程的前提条件.关键字是组成语言的最基本单位,对关键字的理解,有助于编写高质量的代码. 1 static(静态)变量有什么作用? 在函数体 ...
- HDU 1007 平面上最近点对 分治
思路: 分治 套路题 //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> ...
- Agar.io 简单但是有趣的网页游戏
攻略,进阶 上榜第一次 (有点水,九百多分) 上榜第二次 (完成四杀,逆袭上榜) 上榜第三次 (忘写名字,自己补上) 上榜第四次 (人生巅峰!) 上榜第五次 (踩了狗屎运,上榜这么容易了?收了一个小 ...
- Troubleshooting Guide for ORA-12541 TNS: No Listener
Server side checks (not platform specific): 1) Check the result on the server using tnsping to the ...
- sql删除表中重复记录只保留一条记录
最终代码 update T_Fee set gzl_dfg_op = 'delete' where MetReadRecordID in ( select MetReadRecordID from T ...
- 全面介绍Android Studio中Git 的使用(一)
来源 :http://blog.csdn.net/gao_chun/article/details/49817229/
- [AC自动机模板]Keywords Search
只是记录一下代码 AC自动机算法的教程请移步这里 还有这里 指针看着懵逼的还可以看一下这里 #include<iostream> #include<cstdio> #inclu ...
- ThinkPHP---thinkphp会话支持和文件载入
[一]会话控制 会话支持一般指cookie和session,在ThinkPHP里为了方便开发,封装了cookie和session方法. (1)session方法 在函数库封装了session方法 se ...