"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
(参考博客:https://blog.csdn.net/qq_40772692/article/details/82530467)
(题意:求从s点到t点的第K短路。)
(思路:先从终点开始跑一遍最短路,然后从起点以启发式函数为基础开始BFS,当一个点第K出队列时,便是第K短路。否则,说明不存在第K短路。)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#define ll long long
using namespace std;
const int N=1e3+7;
const int M=1e5+10;
const int inf=0x3f3f3f3f;
int head[N],head1[N],cnt,cnt1,dis[N],k;
bool book[N];
struct node
{
int to,w,next;
}E[M],E1[M];
struct Node
{
int to,w;
Node(){}
Node(int to,int w):to(to),w(w){}
friend bool operator <(Node a,Node b)
{
return a.w>b.w;
}
};
struct NODE
{
int to,w;
NODE(){}
NODE(int to,int w):to(to),w(w){}
friend bool operator <(NODE a,NODE b)
{
return a.w+dis[a.to]>b.w+dis[b.to];
//启发式函数,当前距离+估价距离
}
};
inline void add(int u,int v,int w)
{
E[cnt].to=v;
E[cnt].w=w;
E[cnt].next=head[u];
head[u]=cnt++;
}
inline void add1(int u,int v,int w)
{
E1[cnt1].to=v;
E1[cnt1].w=w;
E1[cnt1].next=head1[u];
head1[u]=cnt1++;
}
inline int dijs(int s,int e)
{
memset(dis,inf,sizeof(dis));
memset(book,0,sizeof(book));
priority_queue<Node> q;
q.push(Node(s,0));
//book[s]=1;
dis[s]=0;
while(q.size())
{
// cout<<"!\n";
Node now=q.top();
q.pop();
if(book[now.to]) continue;
book[now.to]=1;
for(int i=head1[now.to];i!=-1;i=E1[i].next)
{
int to=E1[i].to;
if(book[to]) continue;
if(dis[to]>now.w+E1[i].w)
{
dis[to]=now.w+E1[i].w;
q.push(Node(to,dis[to]));
}
}
}
if(dis[e]==inf)
{
return 1;
}
return 0;
}
inline int bfs(int s,int e)
{
int cnt=0;
priority_queue <NODE> q;
q.push(NODE(s,0));
while(q.size())
{
//cout<<"?\n";
NODE now=q.top();
q.pop();
if(now.to==e)
{
k--;
if(k==0)
return now.w;
}
for(int i=head[now.to];i!=-1;i=E[i].next)
{
q.push(NODE(E[i].to,now.w+E[i].w));
}
}
return -1;
}
int main(void)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
head[i]=head1[i]=-1;
}
cnt=cnt1=0;
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add1(v,u,w);
}
int s,e;
scanf("%d%d%d",&s,&e,&k);
if(dijs(e,s))
{
printf("-1\n");
return 0;
}
if(s==e) k++;
//cout<<"okay\n";
printf("%d\n",bfs(s,e));
return 0;
}