[poj2449](java,spfa,A*,前向星)

题目大意就是给出一个图,然后给出一个起点个一个终点,求这两点间的第K短路。

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

oj 

这一篇对A*算法有很详细的解释

这一篇也不错

f=g()+h() / 相当于是bfs和迪杰斯特拉算法的集合,g是bfs ,h是dijkstra

求s(起点)到t(终点)先将所有边反向,用spfa求出t到各个边的最短距离,然后再A*

第一次接触A*算法。。

 

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class poj2449 {
	static int head[];
	static int rhead[];
	static int dis[];
	static int vis[];
	static node edge[];
	static node redge[];
	static int inf=Integer.MAX_VALUE/100;
	static int cur;
	static int n,k,m;
	public static class node{
		int to,v,next;

	}
	public static class A implements Comparable{
		int v,f,g;   //f=g+h
		public int compareTo(A t) {
			if(f==t.f)
				return g-t.g;
			return f-t.f;
		}
	}
	public static void add(int x,int y,int v) {//前向星
		edge[cur]=new node();
		edge[cur].to=y;
		edge[cur].v=v;
		edge[cur].next=head[x];
		head[x]=cur;
		redge[cur]=new node();
		redge[cur].to=x;
		redge[cur].v=v;
		redge[cur].next=rhead[y];
		rhead[y]=cur++;
	}
	public static void spfa(int t) {
		for(int i=0;i<=n;i++) {
			dis[i]=inf;
		}
		dis[t]=0;
		vis[t]=1;
		Queue que=new LinkedList();
		que.offer(t);
		while(!que.isEmpty())
		{
			int tt=que.poll();
			vis[tt]=0;
			for(int i=rhead[tt];i!=-1;i=redge[i].next) {
				int v=redge[i].v;
				int cur=redge[i].to;
				if(dis[cur]>dis[tt]+v) {
					dis[cur]=dis[tt]+v;
					if(vis[cur]==0) {
						que.offer(cur);
						vis[cur]=1;
					}
				}
			}
		}
	}
	public static int Amultiply(int s,int t) {
		PriorityQueue pri=new PriorityQueue();
		int cnt=0;
		A a=new A();
		a.v=s;
		a.g=0;
		a.f=a.g+dis[s];
		pri.offer(a);
		A tt=new A();
		if(dis[s]==inf) return -1;
		if(s==t) k++; //真的坑这一点
		while(!pri.isEmpty()) {
			tt=pri.poll();
			if(tt.v==t) {
				cnt++;
				if(cnt==k)
					return tt.f;
			}
			for(int i=head[tt.v];i!=-1;i=edge[i].next) {
				A tp=new A();
				tp.v=edge[i].to;
				tp.g=edge[i].v+tt.g;
				tp.f=tp.g+dis[tp.v];
				pri.offer(tp);
			}
		}
		
		
		return -1;
	}
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		while(in.hasNext()) {
			cur=0;
			n=in.nextInt();
			m=in.nextInt();
			dis=new int[n+1];
			vis=new int[n+1];
			edge=new node[500005];
			redge=new node[500005];
			head=new int[n+1];
			rhead=new int[n+1];
			for(int i=0;i<=n;i++) {
				head[i]=-1;
				rhead[i]=-1;
			}
			for(int i=1;i<=m;i++) {
				int x=in.nextInt();
				int y=in.nextInt();
				int v=in.nextInt();
				add(x,y,v);
			}
			int s=in.nextInt();
			int t=in.nextInt();
			k=in.nextInt();
			spfa(t);
			System.out.println(Amultiply(s,t));
		}
	}

}

 

你可能感兴趣的:(搜索)