Remmarguts' Date
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 Source
POJ Monthly,Zeyuan Zhu
|
题目大意:有n个点,m个边,现在规定给你一个起点和一个终点,现在要计算从起点到终点的路径上第k短路径
解题思路:在k短路问题上,要引用一个比较新的算法,叫做A*算法,是一种启发式算法,他有一个函数:f(n)=g(n)+h(n)
其中f(n)表示的n这个点的估值,g(n)表示从起点到当前点的花费,h(n)表示当前点n到终点的估值
通常的算法对于h(n)这个来说,反向建图然后以终点为起点反向跑最短路预处理出来即可,
接下来对于f(n)来说,我需要建立一个优先队列,每次从里面取出来f(n)最小的,然后判断对于每次取出来的这个点是否是终点,然后终点出现次数为k次就是k短路,直接输出f(n)即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define INF 1000000000
using namespace std;
typedef long long LL;
vector<int> tu[500005],cost[500005],rtu[500005],ccost[500005];
int n,m,s,t,T;
int dis[500005],vis[500005];
void build(int x,int y,int c)
{
tu[x].push_back(y);
cost[x].push_back(c);
rtu[y].push_back(x);
ccost[y].push_back(c);
}
struct A
{
int pos,f,g;//g表示的是源点s到当前点pos的距离,f表示的是pos这个点的估值
bool operator <(const A a)const {
if(a.f == f) return a.g < g;
return a.f < f;
}
};
void spfa(int x)
{
int i,tot,k,c;
for(i=1;i<=n;i++)
{
dis[i]=INF;
vis[i]=0;
}
queue<int> qua;
qua.push(x);
dis[x]=0;//dis数组表示的是到终点t的最短距离
vis[x]=1;
while(!qua.empty())
{
tot=qua.front();
qua.pop();
for(i=0;i<rtu[tot].size();i++)
{
k=rtu[tot][i];
c=ccost[tot][i];
if(dis[k]>dis[tot]+c)
{
dis[k]=dis[tot]+c;
if(vis[k]!=0)
{
vis[k]=1;
qua.push(k);
}
}
}
vis[tot]=0;
}
}
int Astar(int x,int y)
{
int i,tot,c;
int cnt=0;
priority_queue<A> qua;
if(x==y) T++;
if(dis[x]==INF)
{
return -1;
}
A a,b;
a.pos=x,a.g=0,a.f=a.g+dis[x];//f(n)=g(n)+h(n),h(n)就是dis[n]
qua.push(a);
while(!qua.empty())
{
b=qua.top();
qua.pop();
if(b.pos==y)
{
//cout<<b.pos<<" "<<b.f<<" "<<b.g<<endl;
cnt++;
if(cnt==T)
return b.g;
}
for(i=0;i<tu[b.pos].size();i++)
{
a.pos=tu[b.pos][i];
a.g=b.g+cost[b.pos][i];
a.f=a.g+dis[a.pos];
//cout<<"------"<<a.pos<<" "<<a.g<<" "<<a.f<<endl;
qua.push(a);
}
}
return -1;
}
int main()
{
int x,y,c,i;
while(cin>>n>>m)
{
for(i=1;i<=m;i++)
{
cin>>x>>y>>c;
build(x,y,c);
}
cin>>s>>t>>T;
spfa(t);//反向建图跑
cout<<Astar(s,t)<<endl;
}
}