POJ 2449 Remmarguts' Date -K短路

来源:互联网 发布:sql server 查询二进制 编辑:程序博客网 时间:2024/05/17 19:22
                            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 21 2 52 1 41 2 2

Sample Output

14

Source

POJ Monthly,Zeyuan Zhu
 
  1 #include <queue>  2 #include <cstdio>  3 #include <ctype.h>  4 #include <cstring>  5 #include <algorithm>  6   7 using namespace std;  8   9 const int MAXM=100010; 10 const int MAXN=1010; 11 const int INF=0x7fffffff; 12  13 struct edge { 14     int to; 15     int next; 16     int val; 17     edge() {} 18     edge(int to,int val,int next):to(to),val(val),next(next) {} 19 }; 20 edge e[MAXM<<1],r[MAXM<<1]; 21  22 int head[MAXN],Head[MAXN],tot; 23  24 int dis[MAXN]; 25  26 int n,m,S,T,k,inr; 27  28 bool vis[MAXN]; 29  30 struct data { 31     int to; 32     int dist; 33     bool operator < (data p) const { 34         return dist+dis[to]>dis[p.to]+p.dist; 35     } 36 }; 37 data s; 38  39 inline void read(int&x) { 40     int f=1;register char c=getchar(); 41     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar()); 42     for(;isdigit(c);x=x*10+c-48,c=getchar()); 43     x=x*f; 44 } 45  46 inline void add(int x,int y,int v) { 47     e[++tot]=edge(y,v,head[x]); 48     r[tot]=edge(x,v,Head[y]); 49     head[x]=Head[y]=tot; 50 } 51  52 inline void init() { 53     memset(head,-1,sizeof head); 54     memset(Head,-1,sizeof Head); 55     tot=0; 56 } 57  58 void spfa() { 59     queue<int> Q; 60     for(int i=1;i<=n;++i) dis[i]=INF,vis[i]=false; 61     Q.push(T); 62     dis[T]=0; 63     vis[T]=true; 64     while(!Q.empty()) { 65         int now=Q.front(); 66         Q.pop(); 67         vis[now]=false; 68         for(int i=Head[now];i!=-1;i=r[i].next) { 69             int to=r[i].to; 70             if(dis[to]>dis[now]+r[i].val) { 71                 dis[to]=dis[now]+r[i].val; 72                 if(!vis[to]) vis[to]=true,Q.push(to); 73             } 74         } 75     } 76     return; 77 } 78  79 int Astar() { 80     inr=0; 81     if(dis[S]==-1) return -1; 82     if(S==T) ++k; 83     priority_queue<data> Q; 84     s.to=S;s.dist=0; 85     Q.push(s); 86     while(!Q.empty()) { 87         data now=Q.top(); 88         Q.pop(); 89         if(now.to==T) ++inr; 90         if(inr==k) return now.dist; 91         for(int i=head[now.to];i!=-1;i=e[i].next) { 92             int to=e[i].to; 93             data LC=now; 94             LC.to=to;LC.dist=now.dist+e[i].val; 95             Q.push(LC); 96         } 97     } 98     return -1; 99 }100 101 int hh() {102     while(~scanf("%d%d",&n,&m)) {103         init();104         for(int x,y,z;m--;) {105             read(x);read(y);read(z);106             add(x,y,z);107         }108         read(S);read(T);read(k);109         spfa();110         int ans=Astar();111         printf("%d\n",ans);112     }113     return 0;114 }115 116 int sb=hh();117 int main() {;}
代码