Poj2449:k Short Circuit

Source: Internet
Author: User
Tags integer numbers

Remmarguts ' Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 26355 Accepted: 7170

Description

"Good man never makes girls wait or breaks an appointment!" said the Mandarin duck father. Softly touching he 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 WI Th UDF if and only if the Prince go and meet her via the k-th shortest path. (In fact, UYUW-does not want-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's the station W ITH number S or T. Different paths with same length would be considered disparate.

Input

The first line contains the numbers n and M (1 <= n <=, 0 <= M <= 100000). Stations is 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 sh Ortest 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

Idea: Using a * algorithm to find the shortest way. The first search to the end of the shortest, the second search to the end of the short-circuit ... K to the end of the search to a short circuit. The valuation function of the A * algorithm f (x) =g (x) +h (x). g (x): The distance from the starting point to the current node. H (x): The distance from the current node to the end point. The reverse edge can be constructed using SPFA.
#include <cstdio>#include<cstring>#include<queue>using namespacestd;Const intmaxn=1005;Const intinf=0x3f3f3f3f;structedge{intTo,w,next;} es[100005],rves[100005];intn,m;intsrc,tml,kth;intHead[maxn],tot;intRhead[maxn],rtot;voidAddedge (intUintVintW) {es[tot].to=v; ES[TOT].W=W; Es[tot].next=Head[u]; Head[u]=tot++; Rves[rtot].to=T; RVES[RTOT].W=W; Rves[rtot].next=Rhead[v]; RHEAD[V]=rtot++;}intD[MAXN],VIS[MAXN];voidSPFA (intS//Determining the Valuation function h (x) using the inverse edge: the distance from I to tml{     for(intI=1; i<=n;i++) {D[i]=INF; Vis[i]=0; } Queue<int>que;    Que.push (s); D[s]=0; Vis[s]=1;  while(!Que.empty ()) {        intu=Que.front (); Que.pop (); Vis[u]=0;  for(inti=rhead[u];i!=-1; i=Rves[i].next) {Edge e=Rves[i]; if(d[e.to]>d[u]+e.w) {d[e.to]=d[u]+E.W; if(!Vis[e.to]) {Vis[e.to]=1;                Que.push (e.to); }            }        }    } }structnode{intnod,g,h; Node () {} node (intCnod,intCgintch): Nod (Cnod), G (CG), h (ch) {}BOOL operator< (ConstNode &node)Const    {        returnG+h > node.g+node.h; }};intAstarints) {Priority_queue<Node>que; Que.push (Node (s),0, D[s]));//g (x): The distance from SRC to I. h (x): The distance from I to tml.     while(!Que.empty ()) {Node now=que.top (); Que.pop (); intu=Now.nod; if(u==tml) {kth--; if(kth==0)returnNow.g+now.h;//section K Shortest Way        }         for(inti=head[u];i!=-1; i=Es[i].next) {Edge e=Es[i]; intg=now.g+E.W; intH=D[e.to];            Que.push (Node (e.to,g,h)); }    }    return-1;}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {memset (head,-1,sizeof(head)); memset (Rhead,-1,sizeof(Rhead)); Tot=0; Rtot=0;  for(intI=0; i<m;i++)        {            intu,v,w; scanf ("%d%d%d",&u,&v,&W);        Addedge (U,V,W); } scanf ("%d%d%d",&src,&tml,&kth); if(src==tml) kth++;//if SRC equals tml then the shortest circuit is 0, not countingSPFA (tml); printf ("%d\n", Astar (SRC)); }        return 0;}

Poj2449:k Short Circuit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.