Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 26355   Accepted: 7170

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story.

"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

The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed 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

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

Sample Input

2 2
1 2 5
2 1 4
1 2 2

Sample Output

14

思路:利用A*算法求最短路。第一次搜到终点为最短路,第二次搜到终点为次短路...第k次搜到终点为k短路。A*算法的估价函数f(x)=g(x)+h(x)。g(x):从出发点到当前节点的距离。h(x):从当前节点到终点的距离。可构建反向边利用spfa求得。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Edge{
int to,w,next;
}es[],rves[];
int n,m;
int src,tml,kth;
int head[MAXN],tot;
int rhead[MAXN],rtot;
void addedge(int u,int v,int w)
{
es[tot].to=v;
es[tot].w=w;
es[tot].next=head[u];
head[u]=tot++; rves[rtot].to=u;
rves[rtot].w=w;
rves[rtot].next=rhead[v];
rhead[v]=rtot++;
}
int d[MAXN],vis[MAXN];
void spfa(int s)//利用反向边确定估价函数h(x):i到tml的距离
{
for(int i=;i<=n;i++)
{
d[i]=INF;
vis[i]=;
}
queue<int> que;
que.push(s);
d[s]=;
vis[s]=;
while(!que.empty())
{
int u=que.front();que.pop();
vis[u]=;
for(int i=rhead[u];i!=-;i=rves[i].next)
{
Edge e=rves[i];
if(d[e.to]>d[u]+e.w)
{
d[e.to]=d[u]+e.w;
if(!vis[e.to])
{
vis[e.to]=;
que.push(e.to);
}
}
}
}
}
struct Node{
int nod,g,h;
Node(){}
Node(int cnod,int cg,int ch):nod(cnod),g(cg),h(ch){}
bool operator<(const Node &node) const
{
return g+h > node.g+node.h;
}
};
int astar(int s)
{
priority_queue<Node> que;
que.push(Node(s,,d[s]));//g(x):src到i的距离.h(x):i到tml的距离.
while(!que.empty())
{
Node now=que.top();que.pop();
int u=now.nod;
if(u==tml)
{
kth--;
if(kth==) return now.g+now.h;//第k条最短路
}
for(int i=head[u];i!=-;i=es[i].next)
{
Edge e=es[i];
int g=now.g+e.w;
int h=d[e.to];
que.push(Node(e.to,g,h));
}
}
return -;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
memset(rhead,-,sizeof(rhead));
tot=;
rtot=;
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
scanf("%d%d%d",&src,&tml,&kth);
if(src==tml) kth++;//若src等于tml那么最短路为0,不算
spfa(tml);
printf("%d\n",astar(src));
} return ;
}

POJ2449:K短路的更多相关文章

  1. poj2449(k短路&A_star模板)

    题目链接:http://poj.org/problem?id=2449 题意:给出一个有向图,求s到t的第k短路: 思路:k短路模板题,可以用A_star模板过: 单源点最短路径+高级搜索A*;A*算 ...

  2. POJ2449 K短路模板

    #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...

  3. A_star poj2449 k短路

    赛后填坑系列QAQ 贴代码呀 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  4. POJ2449 Remmarguts' Date 第K短路

    POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...

  5. A*模板(求K短路)(POJ2449)

    A*是bfs的优化,IDA*是dfs的优化 A*算法: 为启发式算法中很重要的一种,被广泛应用在最优路径求解和一些策略设计的问题中.而A*算法最为核心的部分,就在于它的一个估值函数的设计上: f(n) ...

  6. k短路模板 POJ2449

    采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  7. poj2449 第k短路

    题目链接 学习博客:https://blog.csdn.net/Z_Mendez/article/details/47057461 k短路没有我想象的那么难,还是很容易理解的 求s点到t点的第k短路径 ...

  8. 第K短路模板【POJ2449 / 洛谷2483 / BZOJ1975 / HDU6181】

    1.到底如何求k短路的? 我们考虑,要求k短路,要先求出最短路/次短路/第三短路……/第(k-1)短路,然后访问到第k短路. 接下来的方法就是如此操作的. 2.f(x)的意义? 我们得到的f(x)更小 ...

  9. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

随机推荐

  1. 5.设计模式----prototype原型模式

    原型模式:做到是原型,那肯定是自己本身才是原型,原型模式属于对象的创建模式. 关于原型模式的实现方式分2种: (1)简单形式.(2)登记形式,这两种表现形式仅仅是原型模式的不同实现. package ...

  2. Linux软件包分类

    源代码包 优点: 1.给你的就是源代码 2.可以修改源代码 3.可以自由选择所需的功能 4.软件是在自己电脑上编译安装,所以更加稳定高效 5.卸载方便(直接删了你安装软件的那个目录就好了) 缺点: 1 ...

  3. Coreos 安装及配置

    Coreos 安装及配置 本文由Vikings(http://www.cnblogs.com/vikings-blog/) 原创,转载请标明.谢谢! 目前国内使用coreos的场景还不多,搜索core ...

  4. YiiFramework(PHP)

    Yii is a high-performance PHP framework best for developing Web 2.0 applications. Ref:http://www.yii ...

  5. sin6_addr打印:string to sockaddr_in6 and sockaddr_in6 to string

    函式原型: #include <arpa/inet.h> const char *inet_ntop(int af, const void *src, char *dst, socklen ...

  6. Kattis - flippingcards 【并查集】

    题意 给出 N 对 数字 然后 每次从一对中 取出一个数字 判断 能否有一种取出的方案 取出的每个数字 都是不同的 思路 将每一对数字 连上一条边 然后 最后 判断每一个连通块里面 边的个数 是否 大 ...

  7. ajax的原理及使用

    ajax并非是一门新的技术,而是现有技术的一种新的组合用法,即是结合异步javascript和XML,它是一种创建快速动态网页的技术.其中,异步javascript是相对于同步而言的,同步模式通常称为 ...

  8. P3320 [SDOI2015]寻宝游戏

    题目 P3320 [SDOI2015]寻宝游戏 做法 很巧妙的一种思路,懂了之后觉得大水题 首先要知道:在一棵树上标记一些点,然后从任意一点出发,遍历所有的的最小路径为\(dfs\)序从小到大遍历 那 ...

  9. P4965 薇尔莉特的打字机

    题目 P4965 薇尔莉特的打字机 快到十二点了正在颓废突然发现了一道好题 虽然毒瘤,但确实是容斥原理的好题啊,做法也特别巧妙(标程 思路 题目大意(怕自己突然忘) n个初始字符,m个操作(加入或删除 ...

  10. nginx配置大全

    nginx配置大全