Submission #2084583


Source Code Expand

import java.awt.Point;
import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class Main{

	class Edge implements Comparable<Edge>{
		int cost;
		Point to;

		public Edge(int cost,Point to){
			this.cost=cost;
			this.to=to;
		}

		@Override
		public int compareTo(Edge o){
			return cost-o.cost;
		}

		@Override
		public String toString(){
			return "Edge [cost="+cost+", to="+to+"]";
		}
	}

	private void solve(){
		PriorityQueue<Edge> q=new PriorityQueue<>();
		int h=gInt(),w=gInt();
		int[]ch=ints(w).toArray(),cv=ints(h).toArray();
		long r=0;
		q.add(new Edge(0,new Point(0,0)));
		boolean[][]f=new boolean[h+1][w+1];
		while(!q.isEmpty()){
			Edge poll=q.poll();
			if(f[poll.to.y][poll.to.x])
				continue;
			//System.out.println(poll);
			f[poll.to.y][poll.to.x]=true;
			r+=poll.cost;
			if(poll.to.x<w&&!f[poll.to.y][poll.to.x+1])
				q.add(new Edge(ch[poll.to.x],new Point(poll.to.x+1,poll.to.y)));
			if(poll.to.x>0&&!f[poll.to.y][poll.to.x-1])
				q.add(new Edge(ch[poll.to.x-1],new Point(poll.to.x-1,poll.to.y)));
			if(poll.to.y<h&&!f[poll.to.y+1][poll.to.x])
				q.add(new Edge(cv[poll.to.y],new Point(poll.to.x,poll.to.y+1)));
			if(poll.to.y>0&&!f[poll.to.y-1][poll.to.x])
				q.add(new Edge(cv[poll.to.y-1],new Point(poll.to.x,poll.to.y-1)));
		}
		System.out.println(r);
	}

	public static void main(String[]$){
		new Main().solve();
	}

	static Scanner s=new Scanner(System.in);


	static int gInt(){
		return s.nextInt();
	}
	static long gLong(){
		return s.nextLong();
	}
	static long gDouble(){
		return s.nextLong();
	}


	static Range rep(int i){
		return new Range(i);
	}
	static Range rep(int f,int t,int d){
		return new Range(f,t,d);
	}
	static Range rep(int f,int t){
		return rep(f,t,1);
	}
	static Range rrep(int f,int t){
		return rep(f,t,-1);
	}

	static class Range implements Iterable<Integer>,PrimitiveIterator.OfInt{

		int to,cur,d;

		Range(int from,int to,int d){
			this.cur=from-d;
			this.to=to;
			this.d=d;
		}

		Range(int n){
			this(0,n-1,1);
		}

		@Override
		public Iterator<Integer> iterator(){
			return this;
		}

		@Override
		public boolean hasNext(){
			return cur+d==to||(cur!=to&&(cur<to==cur+d<to));
		}

		@Override
		public int nextInt(){
			return cur+=d;
		}
	}


	static IntStream REPS(int v){
		return IntStream.range(0,v);
	}

	static IntStream REPS(int l,int r){
		return IntStream.rangeClosed(l,r);
	}


	static IntStream ints(int n){
		return REPS(n).map(i->gInt());
	}

	static Stream<String> strs(int n){
		return REPS(n).mapToObj(i->s.next());
	}

}

Submission Info

Submission Time
Task C - Gr-idian MST
User fal_rnd
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 2800 Byte
Status WA
Exec Time 1282 ms
Memory 990612 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 2
AC × 6
WA × 4
MLE × 20
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt MLE 1272 ms 976472 KB
02.txt MLE 1204 ms 983172 KB
03.txt MLE 1266 ms 968052 KB
04.txt MLE 1234 ms 976236 KB
05.txt MLE 1259 ms 972396 KB
06.txt MLE 1265 ms 974800 KB
07.txt MLE 1279 ms 955796 KB
08.txt MLE 1252 ms 971184 KB
09.txt MLE 1259 ms 977408 KB
10.txt MLE 1267 ms 971516 KB
11.txt MLE 1214 ms 974932 KB
12.txt MLE 1263 ms 953560 KB
13.txt MLE 1282 ms 969644 KB
14.txt MLE 1229 ms 983964 KB
15.txt MLE 1261 ms 975620 KB
16.txt WA 656 ms 61400 KB
17.txt WA 657 ms 61412 KB
18.txt WA 669 ms 63412 KB
19.txt WA 836 ms 61360 KB
20.txt MLE 1189 ms 990612 KB
21.txt MLE 1193 ms 970284 KB
22.txt MLE 1232 ms 974340 KB
23.txt MLE 1225 ms 968176 KB
24.txt MLE 1261 ms 968732 KB
25.txt AC 178 ms 24656 KB
26.txt AC 184 ms 24656 KB
27.txt AC 187 ms 25044 KB
28.txt AC 174 ms 24528 KB
s1.txt AC 185 ms 24516 KB
s2.txt AC 180 ms 26580 KB