1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| import java.io.*; import java.math.BigInteger; import java.sql.Time; import java.text.SimpleDateFormat; import java.util.*;
public class Main { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out)); public static Scanner sc = new Scanner(System.in);
public static int monthes[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public static int maxd = 200000 + 15; public static int INF = 0x3f3f3f3f; public static int mod = (int) 1e9 + 7; public static int[][] fx = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
public static int node = 0; public static int[] head = new int[maxd]; public static int[] edgePre = new int[maxd*2]; public static int[] edgeW = new int[maxd*2]; public static int[] edgeTo = new int[maxd*2]; public static int[] dis = new int[maxd]; public static boolean[] vis = new boolean[maxd];
static class Edge implements Comparable<Edge> { private int point; private int w; Edge(int point, int w) { this.point = point; this.w = w; } @Override public int compareTo(Edge obj) { return this.w - obj.w; } }
public static void init(int n){ for(int i=1;i<=n;++i){ head[i] = -1; dis[i] = INF; vis[i] = false; } node=0; } public static void add_edge(int a,int b,int c){ edgeTo[node]=b; edgeW[node]=c; edgePre[node] = head[a]; head[a]=node++; } public static int Prim(int start ,int n){ PriorityQueue<Edge> q = new PriorityQueue<>(); int ans = 0; int cnt = 0; q.offer(new Edge(start,0)); while (!q.isEmpty()){ Edge now = q.poll(); if(vis[now.point]) continue; vis[now.point]=true; ans+=now.w; cnt++; for(int i=head[now.point];i!=-1;i=edgePre[i]){ int to = edgeTo[i]; if(dis[to]>edgeW[i]){ dis[to] = edgeW[i]; q.offer(new Edge(to,dis[to])); } } }
if(cnt!=n) return INF; return ans;
}
public static void main(String[] args) throws Exception {
int n =nextInt(); int m = nextInt(); init(n); for(int i=1;i<=m;++i){ int a = nextInt(); int b = nextInt(); int c = nextInt(); add_edge(a,b,c); add_edge(b,a,c); } int prim = Prim(1,n); System.out.println(prim==INF?"orz":prim);
closeAll(); }
public static int gcd(int a, int b) { while (b > 0) { a %= b; b ^= a; a ^= b; b ^= a; } return a; }
public static int log(int x) { return (int) (Math.log(x) / Math.log(2)); }
public static void cinInit() { cin.wordChars('a', 'z'); cin.wordChars('A', 'Z'); cin.wordChars(128 + 32, 255); cin.whitespaceChars(0, ' '); cin.commentChar('/'); cin.quoteChar('"'); cin.quoteChar('\''); cin.parseNumbers(); }
public static int nextInt() throws Exception { cin.nextToken(); return (int) cin.nval; }
public static long nextLong() throws Exception { cin.nextToken(); return (long) cin.nval; }
public static double nextDouble() throws Exception { cin.nextToken(); return cin.nval; }
public static String nextString() throws Exception { cin.nextToken(); return cin.sval; }
public static void closeAll() throws Exception { cout.close(); in.close(); out.close(); } }
|