[计蒜客] Made In Heaven A*算法求解K短路问题

A*算法详细思想推荐阅读李煜东的《算法竞赛进阶指南》,可以从中体会A*算法与堆优化的Dijkstra算法有何异同下面直接放本题的代码。



import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader sc = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, sc, out);
        out.close();
    }

    static class Task {
    	public boolean aStar(int begin,int end,ArrayList<int[]>[] G,long[] f,int k,int n,int lim) {
    		PriorityQueue<long[]> que=new PriorityQueue<long[]>(new Comparator<long[]>() {

				@Override
				public int compare(long[] o1, long[] o2) {
					if(o1[1]>o2[1])
						return 1;
					if(o1[1]<o2[1])
						return -1;
					return 0;
				}
        		
        	});
    		if(f[begin]==Integer.MAX_VALUE)
    			return false;
    		int[] cnt=new int[n+1];
    		que.offer(new long[] {begin,f[begin],0});
    		while(!que.isEmpty()) {
    			long[] temp=que.poll();
    			int u=(int) temp[0];
    			cnt[u]+=1;
    			if(cnt[u]>k)
    				continue;
    			if(cnt[u]==k&&u==end) {
    				return temp[1]<=lim;
    			}
    			for(int[] edge:G[u]) {
    				if(cnt[edge[0]]<k) {
    					que.offer(new long[] {edge[0],temp[2]+f[edge[0]]+edge[1],temp[2]+edge[1]});
    				}
    			}
    		}
    		return false;
    	}
    	
        public void solve(int testNumber, InputReader sc, PrintWriter out) throws IOException {
        	while(sc.hasNext()) {
        		int n=sc.nextInt();
            	int m=sc.nextInt();
            	int s=sc.nextInt();
            	int t=sc.nextInt();
            	int k=sc.nextInt();
            	int lim=sc.nextInt();
            	ArrayList<int[]>[] G=new ArrayList[n+1];
            	ArrayList<int[]>[] backG=new ArrayList[n+1];
            	for(int i=0;i<=n;i++) {
            		G[i]=new ArrayList<int[]>();
            		backG[i]=new ArrayList<int[]>();
            	}
            	for(int i=0;i<m;i++) {
            		int u=sc.nextInt();
            		int v=sc.nextInt();
            		int w=sc.nextInt();
            		G[u].add(new int[] {v,w});
            		backG[v].add(new int[] {u,w});
            	}
            	PriorityQueue<long[]> que=new PriorityQueue<long[]>(new Comparator<long[]>() {

    				@Override
    				public int compare(long[] o1, long[] o2) {
    					if(o1[1]>o2[1])
    						return 1;
    					if(o1[1]<o2[1])
    						return -1;
    					return 0;
    				}
            		
            	});
            	que.offer(new long[] {t,0});
            	long[] f=new long[n+1];
            	Arrays.fill(f, Integer.MAX_VALUE);
            	f[t]=0;
            	boolean[] vis=new boolean[n+1];
            	while(!que.isEmpty()) {
            		long[] temp=que.poll();
            		int u=(int) temp[0];
            		long cost=temp[1];
            		if(vis[u])
            			continue;
            		vis[u]=true;
            		for(int[] edge:backG[u]) {
            			if(f[edge[0]]>cost+edge[1]) {
            				f[edge[0]]=cost+edge[1];
            				que.offer(new long[] {edge[0],f[edge[0]]});
            			}
            		}
            	}
            	out.println(aStar(s,t,G,f,k,n,lim)?"yareyaredawa":"Whitesnake!");
        	}
        	
        }
    }

    static class InputReader{
        StreamTokenizer tokenizer;
        public InputReader(InputStream stream){
            tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream)));
            tokenizer.ordinaryChars(33,126);
            tokenizer.wordChars(33,126);
        }
        public String next() throws IOException {
            tokenizer.nextToken();
            return tokenizer.sval;
        }
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        public boolean hasNext() throws IOException {
            int res=tokenizer.nextToken();
            tokenizer.pushBack();
            return res!=tokenizer.TT_EOF;
        }
        
        public double nextDouble() throws NumberFormatException, IOException {
        	return Double.parseDouble(next());
        }
        
        public BigInteger nextBigInteger() throws IOException {
        	return new BigInteger(next());
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值