POJ 2449 Remmarguts' Date【K短路】

来源:互联网 发布:手机淘宝几天到货 编辑:程序博客网 时间:2024/06/05 05:11

Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 29457 Accepted: 7985

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 21 2 52 1 41 2 2

Sample Output

14

Source

POJ Monthly,Zeyuan Zhu

题目大意:

给你N个点,M条有向边,然你找到第k短路从s到t.


思路:


K短路模板题。


Ac代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define INF 0x3f3f3f3fstruct node{    int from;    int to;    int w;    int next;}e[10200000],e2[10200000];struct NODE{    int to;    int g,f;    bool operator <(const NODE &r)const    {        if(r.f==f)return r.g<g;        return r.f<f;    }};int head2[105000];int head[105000];int dist[105000];int vis[105000];int sss,ttt,kkk,n,m,cont,cont2;void add(int from,int to,int w){    e[cont].to=to;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}void radd(int from,int to,int w){    e2[cont2].to=to;    e2[cont2].w=w;    e2[cont2].next=head2[from];    head2[from]=cont2++;}void SPFA(){    for(int i=1;i<=n;i++)dist[i]=0x3f3f3f3f;    dist[ttt]=0;    memset(vis,0,sizeof(vis));    queue<int >s;    s.push(ttt);    while(!s.empty())    {        int u=s.front();        vis[u]=0;        s.pop();        for(int i=head2[u];i!=-1;i=e2[i].next)        {            int v=e2[i].to;            int w=e2[i].w;            if(dist[v]>dist[u]+w)            {                dist[v]=dist[u]+w;                if(vis[v]==0)                {                    vis[v]=1;                    s.push(v);                }            }        }    }}int kth_SPFA(){    NODE ee,ne;    int cnt=0;    priority_queue<NODE>que;    if(sss==ttt)kkk++;    if(dist[sss]==INF)return -1;    ee.to=sss;    ee.g=0;    ee.f=ee.g+dist[ee.to];    que.push(ee);    while(!que.empty())    {        ee=que.top();        que.pop();        if(ee.to==ttt)        {            cnt++;        }        if(cnt==kkk)        {            return ee.g;        }        for(int i=head[ee.to];i!=-1;i=e[i].next)        {            ne.to=e[i].to;            ne.g=ee.g+e[i].w;            ne.f=ne.g+dist[ne.to];            que.push(ne);        }    }    return -1;}int main(){    while(~scanf("%d%d",&n,&m))    {        cont=0,cont2=0;        memset(head,-1,sizeof(head));        memset(head2,-1,sizeof(head2));        for(int i=0;i<m;i++)        {            int x,y,w;            scanf("%d%d%d",&x,&y,&w);            add(x,y,w);            radd(y,x,w);        }        scanf("%d%d%d",&sss,&ttt,&kkk);        SPFA();        int ans=kth_SPFA();        printf("%d\n",ans);    }}






0 0
原创粉丝点击