import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main { /** * This class represents a problem setting. */ class Problem implements IProblem { int[] NMT; int[][] abc; int[] v; @Override public void set(ProblemReader reader) throws IOException { NMT = reader.nextIntArray(); abc = reader.nextIntArray2(NMT[1]); v = reader.nextIntArray(NMT[2]); } @Override public String toString() { // TODO return null; } } /** * Solver for the problem. * It's the most important part of codes in this file. */ class Solver implements IProblemSolver { final int INF = 1000000; @Override public String solve(Problem problem) { int N = problem.NMT[0], M = problem.NMT[1], T = problem.NMT[2]; int[][] abc = problem.abc; int[] v = problem.v; return StringUtils.toString(solve(N, M, T, abc, v)); } private int solve(int N, int M, int T, int[][] abc, int[] v) { if (T <= 12) { return solve1(N, M, T, abc, v); } else { return solve2(N, M, T, abc, v); } } private int solve1(int N, int M, int T, int[][] abc, int[] v) { int[][] d = new int[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { d[i][j] = INF; } } for (int i = 0; i < N; i++) { d[i][i] = 0; } for (int i = 0; i < M; i++) { int a = abc[i][0] - 1, b = abc[i][1] - 1; d[a][b] = d[b][a] = Math.min(d[a][b], abc[i][2]); } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]); } } } int[][] dp = new int[1 << T][N]; for (int i = 0; i < 1 << T; i++) { for (int j = 0; j < N; j++) { dp[i][j] = INF; } } for (int i = 0; i < T; i++) { for (int j = 0; j < N; j++) { dp[1 << i][j] = d[v[i] - 1][j]; } } for (int i = 1; i < 1 << T; i++) { for (int j = 0; j < N; j++) { int comb = i; do { dp[i][j] = Math.min(dp[i][j], dp[comb][j] + dp[i ^ comb][j]); comb = (comb - 1) & i; } while (comb != i); } for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { dp[i][j] = Math.min(dp[i][j], dp[i][k] + d[k][j]); } } } int res = INF; for (int i = 0; i < N; i++) { res = Math.min(res, dp[(1 << T) - 1][i]); } return res; } private int solve2(int N, int M, int T, int[][] abc, int[] v) { Edge[] edges = new Edge[M]; for (int i = 0; i < M; i++) { edges[i] = new Edge(abc[i]); } Arrays.sort(edges); long s = 0; for (int i = 0; i < T; i++) { s |= (1l << (v[i] - 1)); } int[] rest = new int[N - T]; int idx = 0; for (int i = 0; i < N; i++) { if (((s >> i) & 1) == 0) { rest[idx++] = i; } } int res = INF; for (int i = 0; i < 1 << (N - T); i++) { long ss = s; for (int j = 0; j < N - T; j++) { if (((i >> j) & 1) == 1) { ss |= 1l << rest[j]; } } res = Math.min(res, min(N, edges, ss, res)); } return res; } private int min(int N, Edge[] edges, long s, int min) { UnionFindTree uft = new UnionFindTree(N); int n = Long.bitCount(s); int cost = 0; for (Edge edge : edges) { if (((s >> edge.a) & 1) == 1 && ((s >> edge.b) & 1) == 1 && !uft.same(edge.a, edge.b)) { cost += edge.c; uft.unite(edge.a, edge.b); if (cost >= min) { return cost; } if (uft.count(edge.a) == n) { return cost; } } } return INF; } class Edge implements Comparable { int a, b, c; Edge(int[] abc) { a = abc[0] - 1; b = abc[1] - 1; c = abc[2]; } @Override public int compareTo(Edge edge) { if (c != edge.c) { return c - edge.c; } else if (a != edge.a) { return a - edge.a; } else { return b - edge.b; } } } class UnionFindTree { int[] parent; int[] rank; int[] count; UnionFindTree(int size) { parent = new int[size]; rank = new int[size]; count = new int[size]; init(); } void init() { for (int i=0; i { @Override public String solve(Problem problem) { // TODO return null; } } class Factory implements IProblemFactory { @Override public Problem newProblem() { // TODO return null; } @Override public Problem newProblem(ProblemReader reader) throws IOException { Problem problem = new Problem(); problem.set(reader); return problem; } } class ResultComparator implements IResultComparator { @Override public boolean same(String res1, String res2) { // TODO return res1.equals(res2); } } ProblemProcessor processor; Main() { Init(); } void Init() { ProblemProcessorConfig config = new ProblemProcessorConfig<>(); config.factory = new Factory(); config.solver = new Solver(); config.solver2 = new Solver2(); config.comparator = new ResultComparator(); processor = new ProblemProcessor(config); } void process(String mode) throws IOException { processor.process(mode); } public static void main(String args[]) throws IOException { // "create", "debug", and "solve" String mode = "solve"; new Main().process(mode); } /* Common library */ interface IProblem { abstract void set(ProblemReader reader) throws IOException; } interface IProblemFactory

{ abstract P newProblem(); abstract P newProblem(ProblemReader reader) throws IOException; } interface IProblemSolver

{ abstract String solve(P problem); } interface IResultComparator { abstract boolean same(String res1, String res2); } class ProblemReader { BufferedReader br = null; ProblemReader() { br = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() throws IOException { return StringUtils.toInt(br.readLine()); } long nextLong() throws IOException { return StringUtils.toLong(br.readLine()); } double nextDouble() throws IOException { return StringUtils.toDouble(br.readLine()); } String nextString() throws IOException { return br.readLine(); } int[] nextIntArray() throws IOException { return StringUtils.toIntArray(br.readLine()); } long[] nextLongArray() throws IOException { return StringUtils.toLongArray(br.readLine()); } double[] nextDoubleArray() throws IOException { return StringUtils.toDoubleArray(br.readLine()); } String[] nextStringArray() throws IOException { return StringUtils.toStringArray(br.readLine()); } int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } double[] nextDoubleArray(int n) throws IOException { double[] a = new double[n]; for (int i = 0; i < n; i++) { a[i] = nextDouble(); } return a; } String[] nextStringArray(int n) throws IOException { String[] strs = new String[n]; for (int i = 0; i < n; i++) { strs[i] = nextString(); } return strs; } int[][] nextIntArray2(int n) throws IOException { int[][] a = new int[n][]; for (int i = 0; i < n; i++) { a[i] = nextIntArray(); } return a; } long[][] nextLongArray2(int n) throws IOException { long[][] a = new long[n][]; for (int i = 0; i < n; i++) { a[i] = nextLongArray(); } return a; } double[][] nextDoubleArray2(int n) throws IOException { double[][] a = new double[n][]; for (int i = 0; i < n; i++) { a[i] = nextDoubleArray(); } return a; } String[][] nextStringArray2(int n) throws IOException { String[][] strs = new String[n][]; for (int i = 0; i < n; i++) { strs[i] = nextStringArray(); } return strs; } void close() throws IOException { if (br != null) { br.close(); } } } class ProblemCreator

{ IProblemFactory

factory; ProblemCreator(IProblemFactory

factory) { this.factory = factory; } void create() throws FileNotFoundException { P problem = factory.newProblem(); System.out.println(problem.toString()); } } class ProblemSolverComparator

{ IProblemFactory

factory; IProblemSolver

solver1, solver2; IResultComparator comparator; ProblemSolverComparator(IProblemFactory

factory, IProblemSolver

solver1, IProblemSolver

solver2, IResultComparator comparator) { this.factory = factory; this.solver1 = solver1; this.solver2 = solver2; this.comparator = comparator; } void compare() throws IOException { ProblemReader reader = null; try { reader = new ProblemReader(); P problem = factory.newProblem(reader); String res1 = solver1.solve(problem); String res2 = solver2.solve(problem); if (comparator.same(res1, res2)) { System.out.println(res1); } else { System.err.println(res1 + " " + res2); } } finally { if (reader != null) { reader.close(); } } } } class ProblemAnswerer

{ IProblemFactory

factory; IProblemSolver

solver; ProblemAnswerer(IProblemFactory

factory, IProblemSolver

solver) { this.factory = factory; this.solver = solver; } void answer() throws IOException { ProblemReader reader = null; try { reader = new ProblemReader(); P problem = factory.newProblem(reader); String res = solver.solve(problem); System.out.println(res); } finally { if (reader != null) { reader.close(); } } } } class ProblemProcessorConfig

{ IProblemFactory

factory; IProblemSolver

solver, solver2; IResultComparator comparator; } class ProblemProcessor

{ ProblemProcessorConfig

config; ProblemCreator

creator; ProblemSolverComparator

comparator; ProblemAnswerer

answerer; ProblemProcessor(ProblemProcessorConfig

config) { this.config = config; creator = new ProblemCreator

(config.factory); comparator = new ProblemSolverComparator

(config.factory, config.solver, config.solver2, config.comparator); answerer = new ProblemAnswerer

(config.factory, config.solver); } void process(String mode) throws IOException { switch (mode) { case "create": creator.create(); break; case "debug": comparator.compare(); break; case "solve": answerer.answer(); break; } } } static class StringUtils { static String toString(int a) { return String.valueOf(a); } static String toString(long a) { return String.valueOf(a); } static String toString(double a) { return String.valueOf(a); } static String toString(String str) { return str; } static String toString(int[] a, String delimiter) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + delimiter); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(long[] a, String delimiter) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + delimiter); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(double[] a, String delimiter) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i < a.length - 1) { sb.append(a[i] + delimiter); } else { sb.append(a[i]); } } return sb.toString(); } static String toString(String[] strs, String delimiter) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.length; i++) { if (i < strs.length - 1) { sb.append(strs[i] + delimiter); } else { sb.append(strs[i]); } } return sb.toString(); } static String toString(int[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(long[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(double[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static String toString(String[][] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j < a[i].length - 1) { sb.append(a[i][j] + " "); } else { sb.append(a[i][j]); } } sb.append("\n"); } sb.setLength(sb.length() - 1); return sb.toString(); } static int toInt(String s) { return Integer.parseInt(s); } static long toLong(String s) { return Long.parseLong(s); } static double toDouble(String s) { return Double.parseDouble(s); } static int[] toIntArray(String s) { String[] tokens = s.split(" "); int[] a = new int[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Integer.parseInt(tokens[i]); } return a; } static long[] toLongArray(String s) { String[] tokens = s.split(" "); long[] a = new long[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Long.parseLong(tokens[i]); } return a; } static double[] toDoubleArray(String s) { String[] tokens = s.split(" "); double[] a = new double[tokens.length]; for (int i = 0; i < tokens.length; i++) { a[i] = Double.parseDouble(tokens[i]); } return a; } static String[] toStringArray(String s) { return s.split(" "); } } }