Submission #61170


Source Code Expand

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {

	int n, m, K;
	Set<R>[] adj;
	boolean[][] cycle;
	boolean[] u;
	int[] tr;
	
	boolean check(int v, int k, int pre){
		for(int i=0;i<k;i++)if(tr[i]==v){
			for(int j=i;j+1<=k;j++)cycle[tr[j]][tr[j+1]]=cycle[tr[j+1]][tr[j]]=true;
			return true;
		}
		u[v] = true;
		for(R r:adj[v]){
			if(r.t==pre)continue;
			tr[k+1] = r.t;
			if(check(r.t, k+1, v))return true;
		}
		return false;
	}
	
	class R implements Comparable<R>{
		int t, c;
		public R(int t, int c) {
			this.t = t;
			this.c = c;
		}
		@Override
		public int compareTo(R o) {
			return c-o.c;
		}
	}
	
	void dfs(int v){
		if(u[v])return;
		u[v]=true;
		for(R r:adj[v])dfs(r.t);
	}
	
	void run() {
		Scanner sc = new Scanner();
		n = sc.nextInt(); m = sc.nextInt(); K = sc.nextInt();
		cycle = new boolean[n][n];
		adj = new Set[n];
		for(int i=0;i<n;i++)adj[i]=new HashSet<R>();
		int[][] edge = new int[m][3];
		for(int i=0;i<m;i++){
			int s = sc.nextInt()-1, t = sc.nextInt()-1, c = sc.nextInt();
			edge[i][0]=s; edge[i][1]=t; edge[i][2]=c;
			adj[s].add(new R(t, c));
			adj[t].add(new R(s, c));
		}
		u = new boolean[n];
		int cnt = 0;
		for(int i=0;i<n;i++)if(!u[i]){
			cnt++;
			dfs(i);
		}
		if(K <= cnt){
			System.out.println(0); return;
		}
		K-=cnt;
		boolean isCycle = false;
		Arrays.fill(u, false);
		tr = new int[n+5];
		for(int i=0;i<n&&!isCycle;i++)if(!u[i]){
			tr[0] = i;
			u[i] = true;
			isCycle = check(i, 0, -1);
		}
//		System.out.println("K:"+K);
		if(!isCycle){
			List<Integer> l = new ArrayList<Integer>();
			for(int i=0;i<m;i++)l.add(edge[i][2]);
			Collections.sort(l);
			int res = 0;
			for(int i=0;i<K;i++)res+=l.get(i);
			System.out.println(res);
		}
		else{
			int res = 0;
			List<Integer> l = new ArrayList<Integer>();
			for(int i=0;i<m;i++)l.add(edge[i][2]);
			Collections.sort(l);
			for(int i=0;i<=K;i++)res+=l.get(i);
			l.clear();
			for(int i=0;i<m;i++)if(!cycle[edge[i][0]][edge[i][1]])l.add(edge[i][2]);
			Collections.sort(l);
			int r2 = 0;
			if(l.size() < K)r2 = 1<<29;
			else for(int i=0;i<K;i++)r2+=l.get(i);
			System.out.println(Math.min(res, r2));
		}
	}

	void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}

	class Scanner {
		int nextInt() {
			try {
				int c = System.in.read();
				while (c != '-' && (c < '0' || '9' < c))
					c = System.in.read();
				if (c == '-') return -nextInt();
				int res = 0;
				do {
					res *= 10;
					res += c - '0';
					c = System.in.read();
				} while ('0' <= c && c <= '9');
				return res;
			} catch (Exception e) {
				return -1;
			}
		}
		long nextLong() {
			try {
				int c = System.in.read();
				while (c != '-' && (c < '0' || '9' < c))
					c = System.in.read();
				if (c == '-') return -nextLong();
				long res = 0;
				do {
					res *= 10;
					res += c - '0';
					c = System.in.read();
				} while ('0' <= c && c <= '9');
				return res;
			} catch (Exception e) {
				return -1;
			}
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String next() {
			try {
				StringBuilder res = new StringBuilder("");
				int c = System.in.read();
				while (Character.isWhitespace(c))
					c = System.in.read();
				do {
					res.append((char) c);
				} while (!Character.isWhitespace(c = System.in.read()));
				return res.toString();
			} catch (Exception e) {
				return null;
			}
		}
		String nextLine(){
			try{
				StringBuilder res =new StringBuilder("");
				int c = System.in.read();
				while (c=='\r' || c=='\n')
					c = System.in.read();
				do {
					res.append((char) c);
					c = System.in.read();
				} while (c!='\r' && c!='\n');
				return res.toString();
			}catch (Exception e) {
				return null;
			}
		}
	}
	
	public static void main(String... args) {
		new Main().run();
	}
}

Submission Info

Submission Time
Task E - 独立記念日
User nanikaka
Language Java (OpenJDK 1.7.0)
Score 100
Code Size 4103 Byte
Status AC
Exec Time 418 ms
Memory 16864 KB

Compile Error

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name nonloop loop
Score / Max Score 30 / 30 70 / 70
Status
AC × 34
AC × 66
Set Name Test Cases
nonloop nonloop/case_000.txt, nonloop/case_001.txt, nonloop/case_002.txt, nonloop/case_003.txt, nonloop/case_004.txt, nonloop/case_005.txt, nonloop/case_006.txt, nonloop/case_007.txt, nonloop/case_008.txt, nonloop/case_009.txt, nonloop/case_010.txt, nonloop/case_011.txt, nonloop/case_012.txt, nonloop/case_013.txt, nonloop/case_014.txt, nonloop/case_015.txt, nonloop/case_016.txt, nonloop/case_017.txt, nonloop/case_018.txt, nonloop/case_019.txt, nonloop/case_020.txt, nonloop/case_021.txt, nonloop/case_022.txt, nonloop/case_023.txt, nonloop/case_024.txt, nonloop/case_025.txt, nonloop/case_026.txt, nonloop/case_027.txt, nonloop/case_028.txt, nonloop/case_029.txt, nonloop/case_030.txt, nonloop/case_031.txt, nonloop/case_032.txt, nonloop/case_033.txt
loop loop/case_000.txt, loop/case_001.txt, loop/case_002.txt, loop/case_003.txt, loop/case_004.txt, loop/case_005.txt, loop/case_006.txt, loop/case_007.txt, loop/case_008.txt, loop/case_009.txt, loop/case_010.txt, loop/case_011.txt, loop/case_012.txt, loop/case_013.txt, loop/case_014.txt, loop/case_015.txt, loop/case_016.txt, loop/case_017.txt, loop/case_018.txt, loop/case_019.txt, loop/case_020.txt, loop/case_021.txt, loop/case_022.txt, loop/case_023.txt, loop/case_024.txt, loop/case_025.txt, loop/case_026.txt, loop/case_027.txt, loop/case_028.txt, loop/case_029.txt, loop/case_030.txt, loop/case_031.txt, loop/case_032.txt, loop/case_033.txt, loop/loop_case_000.txt, loop/loop_case_001.txt, loop/loop_case_002.txt, loop/loop_case_003.txt, loop/loop_case_004.txt, loop/loop_case_005.txt, loop/loop_case_006.txt, loop/loop_case_007.txt, loop/loop_case_008.txt, loop/loop_case_009.txt, loop/loop_case_010.txt, loop/loop_case_011.txt, loop/loop_case_012.txt, loop/loop_case_013.txt, loop/loop_case_014.txt, loop/loop_case_015.txt, loop/loop_case_016.txt, loop/loop_case_017.txt, loop/loop_case_018.txt, loop/loop_case_019.txt, loop/loop_case_020.txt, loop/loop_case_021.txt, loop/loop_case_022.txt, loop/loop_case_023.txt, loop/loop_case_024.txt, loop/loop_case_025.txt, loop/loop_case_026.txt, loop/loop_case_027.txt, loop/loop_case_028.txt, loop/loop_case_029.txt, loop/loop_case_030.txt, loop/loop_case_031.txt
Case Name Status Exec Time Memory
loop/case_000.txt AC 364 ms 16732 KB
loop/case_001.txt AC 356 ms 16604 KB
loop/case_002.txt AC 357 ms 16608 KB
loop/case_003.txt AC 358 ms 16640 KB
loop/case_004.txt AC 369 ms 16648 KB
loop/case_005.txt AC 364 ms 16608 KB
loop/case_006.txt AC 357 ms 16736 KB
loop/case_007.txt AC 401 ms 16604 KB
loop/case_008.txt AC 362 ms 16624 KB
loop/case_009.txt AC 358 ms 16644 KB
loop/case_010.txt AC 360 ms 16732 KB
loop/case_011.txt AC 362 ms 16608 KB
loop/case_012.txt AC 352 ms 16700 KB
loop/case_013.txt AC 362 ms 16608 KB
loop/case_014.txt AC 370 ms 16768 KB
loop/case_015.txt AC 364 ms 16644 KB
loop/case_016.txt AC 356 ms 16604 KB
loop/case_017.txt AC 355 ms 16632 KB
loop/case_018.txt AC 351 ms 16612 KB
loop/case_019.txt AC 353 ms 16604 KB
loop/case_020.txt AC 373 ms 16732 KB
loop/case_021.txt AC 355 ms 16760 KB
loop/case_022.txt AC 359 ms 16736 KB
loop/case_023.txt AC 363 ms 16608 KB
loop/case_024.txt AC 360 ms 16604 KB
loop/case_025.txt AC 362 ms 16728 KB
loop/case_026.txt AC 361 ms 16732 KB
loop/case_027.txt AC 364 ms 16604 KB
loop/case_028.txt AC 365 ms 16600 KB
loop/case_029.txt AC 361 ms 16604 KB
loop/case_030.txt AC 369 ms 16728 KB
loop/case_031.txt AC 361 ms 16768 KB
loop/case_032.txt AC 366 ms 16732 KB
loop/case_033.txt AC 357 ms 16864 KB
loop/loop_case_000.txt AC 357 ms 16728 KB
loop/loop_case_001.txt AC 370 ms 16596 KB
loop/loop_case_002.txt AC 361 ms 16728 KB
loop/loop_case_003.txt AC 361 ms 16756 KB
loop/loop_case_004.txt AC 363 ms 16608 KB
loop/loop_case_005.txt AC 361 ms 16604 KB
loop/loop_case_006.txt AC 358 ms 16648 KB
loop/loop_case_007.txt AC 357 ms 16604 KB
loop/loop_case_008.txt AC 361 ms 16600 KB
loop/loop_case_009.txt AC 354 ms 16604 KB
loop/loop_case_010.txt AC 362 ms 16728 KB
loop/loop_case_011.txt AC 363 ms 16728 KB
loop/loop_case_012.txt AC 367 ms 16732 KB
loop/loop_case_013.txt AC 365 ms 16724 KB
loop/loop_case_014.txt AC 361 ms 16636 KB
loop/loop_case_015.txt AC 359 ms 16604 KB
loop/loop_case_016.txt AC 361 ms 16712 KB
loop/loop_case_017.txt AC 362 ms 16728 KB
loop/loop_case_018.txt AC 360 ms 16728 KB
loop/loop_case_019.txt AC 360 ms 16732 KB
loop/loop_case_020.txt AC 367 ms 16732 KB
loop/loop_case_021.txt AC 371 ms 16644 KB
loop/loop_case_022.txt AC 363 ms 16828 KB
loop/loop_case_023.txt AC 359 ms 16732 KB
loop/loop_case_024.txt AC 366 ms 16720 KB
loop/loop_case_025.txt AC 361 ms 16764 KB
loop/loop_case_026.txt AC 357 ms 16728 KB
loop/loop_case_027.txt AC 360 ms 16604 KB
loop/loop_case_028.txt AC 356 ms 16760 KB
loop/loop_case_029.txt AC 358 ms 16636 KB
loop/loop_case_030.txt AC 362 ms 16732 KB
loop/loop_case_031.txt AC 368 ms 16736 KB
nonloop/case_000.txt AC 361 ms 16656 KB
nonloop/case_001.txt AC 360 ms 16612 KB
nonloop/case_002.txt AC 364 ms 16732 KB
nonloop/case_003.txt AC 363 ms 16732 KB
nonloop/case_004.txt AC 359 ms 16732 KB
nonloop/case_005.txt AC 373 ms 16712 KB
nonloop/case_006.txt AC 384 ms 16608 KB
nonloop/case_007.txt AC 361 ms 16604 KB
nonloop/case_008.txt AC 363 ms 16600 KB
nonloop/case_009.txt AC 359 ms 16604 KB
nonloop/case_010.txt AC 374 ms 16636 KB
nonloop/case_011.txt AC 367 ms 16728 KB
nonloop/case_012.txt AC 358 ms 16604 KB
nonloop/case_013.txt AC 360 ms 16604 KB
nonloop/case_014.txt AC 362 ms 16728 KB
nonloop/case_015.txt AC 363 ms 16764 KB
nonloop/case_016.txt AC 365 ms 16736 KB
nonloop/case_017.txt AC 418 ms 16724 KB
nonloop/case_018.txt AC 366 ms 16728 KB
nonloop/case_019.txt AC 361 ms 16724 KB
nonloop/case_020.txt AC 368 ms 16760 KB
nonloop/case_021.txt AC 372 ms 16732 KB
nonloop/case_022.txt AC 364 ms 16736 KB
nonloop/case_023.txt AC 366 ms 16732 KB
nonloop/case_024.txt AC 361 ms 16640 KB
nonloop/case_025.txt AC 363 ms 16732 KB
nonloop/case_026.txt AC 364 ms 16776 KB
nonloop/case_027.txt AC 365 ms 16604 KB
nonloop/case_028.txt AC 366 ms 16636 KB
nonloop/case_029.txt AC 361 ms 16600 KB
nonloop/case_030.txt AC 364 ms 16732 KB
nonloop/case_031.txt AC 363 ms 16732 KB
nonloop/case_032.txt AC 371 ms 16748 KB
nonloop/case_033.txt AC 363 ms 16728 KB