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
|
题意:
裸k短路。
POINT:
A*算法做,出队第k次就是k短路。
#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
#define LL long long
typedef pair<int,int> pr;
const int maxn =1001;
const int inf = 0x3f3f3f3f;
int n,m;
struct edge
{
int v,d;
edge(int vv,int dd):
v(vv),d(dd){}
};
vector<edge> G[maxn];
vector<edge> GR[maxn];
void add(int u,int v,int d)
{
G[u].push_back(edge(v,d));
GR[v].push_back(edge(u,d));
}
int dis[maxn];
void dij(int s)
{
for(int i=1;i<=n;i++) dis[i]=inf;
dis[s]=0;
priority_queue<pr,vector<pr>,greater<pr>> q;
q.push(pr(0,s));
while(!q.empty())
{
int u=q.top().second;
int d=q.top().first;
q.pop();
if(d>dis[u]) continue;
for(int i=0;i<GR[u].size();i++)
{
edge e=GR[u][i];
if(dis[e.v]>d+e.d)
{
dis[e.v]=d+e.d;
q.push(pr(dis[e.v],e.v));
}
}
}
}
struct node
{
int f,g,h,u;
node(int ff,int gg,int hh,int uu):
f(ff),g(gg),h(hh),u(uu){}
};
struct cmd
{
bool operator ()(node a,node b)
{
return a.f>b.f;
}
};
int astar(int s,int t,int k)
{
if(dis[s]==inf) return -1;
int cnt=0;
if(s==t)
{
k++;
}
priority_queue<node,vector<node>,cmd> q;
q.push(node(dis[s],0,dis[s],s));
while(!q.empty())
{
node now=q.top();
q.pop();
if(now.u==t)
{
cnt++;
if(k==cnt)
{
return now.g;
}
}
for(int i=0;i<G[now.u].size();i++)
{
edge e=G[now.u][i];
q.push(node(now.g+e.d+dis[e.v],now.g+e.d,dis[e.v],e.v));
}
}
return -1;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v,d;scanf("%d %d %d",&u,&v,&d);
add(u,v,d);
}
int s,t,k;
scanf("%d %d %d",&s,&t,&k);
dij(t);
printf("%d\n",astar(s,t,k));
}