Remmarguts' Date
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 18715 | Accepted: 5126 |
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.
"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).
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条边,让你求从s到t的第k最短路。
K短路入门题,基本算法分三步:
1:将有向图所有边反向。以t(终点)为源点,求解点t到所有点的最短距离。这一步可以用dijkstra或者spfa算法。
2:新建一个优先队列,将源点s加入队列中。
3:从优先队列中弹出f(p)最小的点p,如果p就是t,则计算t的出队次数,如果当前为t第k次出队,则当前路径的长度就是s到t的第k最短路的长度,算法结束,否则遍历与p相连的所有边,将扩展出的到p的邻接点信息加入到优先队列。
当s==t时需要计算k+1次短路,因为s到t的长度为0的路不能算在这k短路中,这时只需要将k+1后在求第k最短路即可。
#include<stdio.h>
#include<string.h>
#include<queue>
#define M 1007
#define inf 0x3f3f3f
using namespace std;
int head[M],rehead[M],vis[M],dis[M];
int n,m,s,t,k,cnt;
//int qq[500005 * 5];
struct node
{
int v,w,next;
}edge[500005],reedge[500005];
struct E
{
int f,g,v;
bool operator<(const E e)const
{
if(e.f==f)return e.g<g;
else return e.f<f;
}
};
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
reedge[cnt].v=u;//建立反边
reedge[cnt].w=w;
reedge[cnt].next=rehead[v];
rehead[v]=cnt++;
}
void dijkstra(int t)//求最短路
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++)
dis[i]=inf;
dis[t]=0;
//vis[t]=1;
for(int i=1;i<n;i++)
{
int min=inf,id=-1;
for(int j=1;j<=n;j++)
if(!vis[j]&&min>dis[j])
{
min=dis[j];
id=j;
}
if(id==-1)break;
vis[id]=1;
for(int j=rehead[id];j!=-1;j=reedge[j].next)
{
int v=reedge[j].v;
if(!vis[v]&&dis[v]>dis[id]+reedge[j].w)
dis[v]=dis[id]+reedge[j].w;
}
}
}
//也可以求出最短路径
//void dijkstra(int src)
//{
// for(int i = 1; i <= n; i++) dis[i] = inf;
// memset(vis, 0, sizeof(vis));
// vis[src] = 0;
// int h = 0, t = 1;
// qq[0] = src;
// dis[src] = 0;
// while(h < t)
// {
// int u = qq[h++];
// vis[u] = 0;
// for(int i = rehead[u] ; i != -1; i = reedge[i].next)
// {
// int v = reedge[i].v;
// int w = reedge[i].w;
// if(dis[v] > dis[u] + w)
// {
// dis[v] = dis[u] + w;
// if(!vis[v])
// {
// qq[t++] = v;
// vis[v] = 1;
// }
// }
// }
// }
//}
int k_path(int src,int des)
{
int num=0;
priority_queue<E>q;
if(src==des)k++;//如果src==des时,k要+1,因为src到des这条距离为0的路不能算在这k短路里面
if(dis[src]==inf)return -1;
E now,next;
now.v=src;
now.g=0;
now.f=now.g+dis[src];
q.push(now);
while(!q.empty())
{
next=q.top();
q.pop();
if(next.v==des)
{
num++;
if(num==k)return next.g;
}
for(int i=head[next.v];i!=-1;i=edge[i].next)
{
now.v=edge[i].v;
now.g=next.g+edge[i].w;
now.f=now.g+dis[now.v];
q.push(now);
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
memset(rehead,-1,sizeof(rehead));
cnt=0;
int a,b,c;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
scanf("%d%d%d",&s,&t,&k);
dijkstra(t);//以t为源点
printf("%d\n",k_path(s,t));
}
return 0;
}