poj2449 Remmarguts' Date K短路模板(SPFA+A*)链式前向星存图

连接:http://poj.org/problem?id=2449

 

Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 17237   Accepted: 4727

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 2
1 2 5
2 1 4
1 2 2

Sample Output

14

 

//SPFA + A* 求K短路应该是比较高效的算法,由于SPFA的特殊性
//它的效率无法计算,对1000个点以内的图,下面的算发对于
//ACM竞赛还是可以的
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int INF = 0x3fffffff;
const int maxn = 1100;//点的规模
const int maxm = 110000;//边的规模
int dis[maxn],head[maxn],head1[maxn];
struct NODE{//求单源最短路用到
       int to,w,next;
}edge[maxm],edge1[maxm];
struct NODE1{//求K短路用到
       int to;
       int g,f;
       bool operator < (const NODE1 &r)const {
              if(r.f==f)return r.g<g;
              return r.f<f;
       }
};
bool SPFA(int s,int n,int head[maxn],NODE edge[maxn],int dist[maxn])
{//求s点到各个点的最短路,存到dist里面,如果有负圈,返回false
       int i,k;
       bool visit[maxn];
       int queue[maxm];//注意别写错!
       int iq;
       int top;
       int outque[maxn];
       for(i=0;i<=n;i++)dist[i]=INF;
       memset(visit,0,sizeof(visit));
       memset(outque,0,sizeof(outque));
       iq=0;
       queue[iq++]=s;
       visit[s]=true;
       dist[s]=0;
       i=0;
       while(i!=iq){
              top=queue[i];
              visit[top]=false;
              outque[top]++;
              if(outque[top]>n)return false;
              k=head[top];
              while(k>=0){
                     if(dist[edge[k].to]-edge[k].w>dist[top]){
                            dist[edge[k].to]=dist[top]+edge[k].w;
                            if(!visit[edge[k].to]){
                                   visit[edge[k].to]=true;
                                   queue[iq]=edge[k].to;
                                   iq++;
                            }
                     }
                     k=edge[k].next;
              }
              i++;
       }
       return true;
}
int a_star(int start,int end,int n,int k,int head[maxn],NODE edge[maxn],int dist[maxn])
{//A* 求出K短路,-1表示K短路不存在
       NODE1 e,ne;
       int cnt=0;
       priority_queue<NODE1>que;
       if(start==end)k++;//如果s==t,0这条路不能算在K短路中,所以需要求K+1短路
       if(dist[start]==INF)return -1;
       e.to=start;
       e.g=0;
       e.f=e.g+dist[e.to];
       que.push(e);
       while(!que.empty()){
              e=que.top();que.pop();
              if(e.to==end)cnt++;
              if(cnt==k)return e.g;
              for(int i=head[e.to];i!=-1;i=edge[i].next){
                     ne.to=edge[i].to;
                     ne.g=e.g+edge[i].w;
                     ne.f=ne.g+dist[ne.to];
                     que.push(ne);
              }
       }
       return -1;
}
int main()
{
//       freopen("in.txt","r",stdin);
       int n,m,a,b,w,s,t,k;//n个点,m条边
	while(~scanf("%d%d",&n,&m)){
	       memset(head,-1,sizeof(head));
	       memset(head1,-1,sizeof(head1));//记得要初始化
	       for(int i=0;i<m;i++){
	              scanf("%d%d%d",&a,&b,&w);//a点到b点权值为w
	              edge[i].to=b;
	              edge[i].w=w;
	              edge[i].next=head[a];
	              head[a]=i;//存原图
	              edge1[i].to=a;
	              edge1[i].w=w;
	              edge1[i].next=head1[b];
	              head1[b]=i;//存反向图
	       }
	       scanf("%d%d%d",&s,&t,&k);//起点s,终点t,K短路
	       SPFA(t,n,head1,edge1,dis);
	       printf("%d\n",a_star(s,t,n,k,head,edge,dis));
	}
	return 0;
}

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Date)