poj 2449 Remmarguts' Date

来源:互联网 发布:windows vista旗舰版 编辑:程序博客网 时间:2024/05/03 06:09
Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 16159 Accepted: 4444

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
 
分析:K短路问题,单向路,A*算法+spfa
#include<cstdio>#include<cstring>#include<queue>using namespace std;const int INF=0x3f3f3f3f;const int N=1002;const int M=100010;int head[N],dis[N],n,m,to[M],nxt[M],l[M],cnt;int head1[N],to1[M],nxt1[M],l1[M],cnt1;struct node{int to,len;bool operator<(node p)const{return p.len+dis[p.to]<len+dis[to];}};void add(int x,int y,int f){to[cnt]=y;l[cnt]=f;nxt[cnt]=head[x];head[x]=cnt++;}void add1(int x,int y,int f){to1[cnt1]=y;l1[cnt1]=f;nxt1[cnt1]=head1[x];head1[x]=cnt1++;}void spfa(int s){int Q[N],front=0,rear=0,i,j,u;bool inQ[N];memset(dis,0x3f,sizeof(dis));memset(inQ,0,sizeof(inQ));Q[(rear++)%N]=s;inQ[s]=1;dis[s]=0;while(front%N!=rear%N){i=Q[(front++)%N];inQ[i]=0;for(j=head[i];j!=-1;j=nxt[j]){u=to[j];if(dis[i]+l[j]<dis[u]){dis[u]=dis[i]+l[j];if(!inQ[u]){Q[(rear++)%N]=u;inQ[u]=1;}}}}}int Astar(int s,int k){int vis[N]={0},i;priority_queue<node> Q;node p,q;p.len=0,p.to=s;Q.push(p);while(!Q.empty()){p=Q.top(),Q.pop();vis[p.to]++;if(vis[p.to]==k)return p.len+dis[p.to];for(i=head1[p.to];i!=-1;i=nxt1[i]){q.len=p.len+l1[i];q.to=to1[i];Q.push(q);}}return INF;}int main(){int a,b,c,s,t,k;while(~scanf("%d%d",&n,&m)){cnt=cnt1=0;memset(head,-1,sizeof(head));memset(head1,-1,sizeof(head1));while(m--){scanf("%d%d%d",&a,&b,&c);add1(a,b,c);add(b,a,c);}scanf("%d%d%d",&s,&t,&k);spfa(t);if(dis[s]==INF){puts("-1");continue;}if(s==t)k++;int min=Astar(s,k);printf("%d\n",min>=INF?-1:min);}return 0;}

原创粉丝点击