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());
}
}
}