牛客练习赛24:E青蛙

这道题也是比较简单的最短路问题,不过好久没写最短路了,写起来有点生疏。

注意,要把相邻节点还有隧道都建边:

import java.io.*;
import java.util.*;
public class Main {
	static int inf=Integer.MAX_VALUE/2;
	static boolean used[];
	static int dis[];
	static int map[][];
	static int n;
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	 static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		public static void main(String args[]) throws IOException {
			n=getInt();
			int m=getInt();
			map=new int[n+1][1+n];
			dis=new int[n+1];
			used=new boolean[n+1];
			for(int i=0;i<=n;i++)
				for(int j=0;j<=n;j++)
					map[i][j]=inf;
			while(m--!=0) {
				map[getInt()][getInt()]=1;
			}
			for(int i=1;i<=n;i++) {
				map[i-1][i]=map[i][i-1]=1;
			}
			dis();
			
			System.out.println(dis[n]);
		}
	static void dis() {
		for(int i=0;i<=n;i++)
			dis[i]=inf;
		dis[0]=0;
		while(true) {
			int tt=-1;
			for(int i=0;i<=n;i++) {
				if(!used[i]&&(tt==-1||dis[i]

 

你可能感兴趣的:(ACM,ACM)