Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一发JAVA

Posted by FIND3532 at 2020-02-25 15:24:57 on Problem 1984
import java.io.BufferedInputStream;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	static int[]root,valuex,valuey,ans;
	static int[] dir;
	static int [][] way;
	static String[] direction;
	static class ask implements Comparable<ask>{
		int from,to,no;
		int line;
		public ask(int from,int to,int no,int line) {
			this.from=from;this.to=to;this.no=no;this.line=line;
		}
		@Override
		public int compareTo(ask o) {
			return this.no-o.no;
		}
	}
	static int find(int a) {
		int temp;
		if(root[a]!=a) {
			temp = root[a];
			root[a]=find(root[a]);
			valuex[a]+=valuex[temp];
			valuey[a]+=valuey[temp];
		}
			return root[a];
	}
	
	public static void main(String[] args) {
			Scanner sc  = new Scanner(new BufferedInputStream(System.in));
			way=new int[40010][3];
			direction=new String[40010];
			root=new int[40010];
			valuex=new int[40010];
			valuey=new int[40010];
			ans = new int [40010];
			int n=sc.nextInt();
			int m=sc.nextInt();
			
			PriorityQueue<ask>asks=new PriorityQueue<ask>();
			for(int i=0;i<root.length;i++)root[i]=i;
			
			asks.clear();

			for(int i=1;i<=m;i++) {    //预先记录地图
				way[i][0]=sc.nextInt();
				way[i][1]=sc.nextInt();
				way[i][2]=sc.nextInt();
				direction[i]=sc.next();
			}
			
			int k=sc.nextInt();
			for(int i=0;i<k;i++) {///预先记录询问
				asks.add(new ask(sc.nextInt(),sc.nextInt(),sc.nextInt(), i+1));
			}
			
			int a,b,c,d;
			int step=1;
			int []see=new int[40010];
			
			while(!asks.isEmpty()) { //开始模拟:边查询边建图
				ask temp = asks.poll(); //对于每一个查询
				while(step<=temp.no  &&  see[step]==0) {
					see[step]=1;
					a=way[step][0];
					b=way[step][1];
					c=way[step][2];
					int roota = find(a);
					int rootb=find(b);
				///	System.out.println(String.format("加入: %d  %d", a,b));
//					System.out.println(String.format("两者根是: %d  %d", roota,rootb));
					if(roota!=rootb) {
						root[rootb]=roota;
						if(direction[step].equals("N")) {
							valuex[rootb]=	valuex[a]-valuex[b];
							valuey[rootb]= valuey[a]-valuey[b]+c;
						}else if(direction[step].equals("E")) {
							valuex[rootb]=	valuex[a]-valuex[b]+c;
							valuey[rootb]= valuey[a]-valuey[b];
						}else if(direction[step].equals("S")) {
							valuex[rootb]=	valuex[a]-valuex[b];
							valuey[rootb]= valuey[a]-valuey[b]-c;
						}else if(direction[step].equals("W")) {
							valuex[rootb]=	valuex[a]-valuex[b]-c;
							valuey[rootb]= valuey[a]-valuey[b];
						}
					}
					step++;
				}
			//	System.out.println(String.format("更新到了: %d ", step));
				int  ra=find(temp.from);
				int  rb=find(temp.to);
			//	System.out.println(String.format("查询的两个根是: %d %d", ra,rb));
				if(ra!=rb) ans[temp.line]=-1;
				else {
					int res = Math.abs( (valuex[temp.from]-valuex[temp.to]) )+ Math.abs( (valuey[temp.from]-valuey[temp.to]) );
					ans[temp.line]=res;
				}
			}
			for(int i=1;i<=k;i++) System.out.println(ans[i]);
			
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator