LingAnC @ 2024-03-15 22:47:19
import java.io.*;
import java.util.Arrays;
public class Main {
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static final int N = 5010, INF = 0x3f3f3f3f;
static int n, m;
static int[][] g = new int[N][N]; // 92MB
static int[] dist = new int[N]; // 19 MB
static boolean[] vis = new boolean[N]; // 19MB
public static void main(String[] args) throws Exception{
n = getInt(); m = getInt();
for (int[] gi : g) Arrays.fill(gi, (int) 1e10);
while (m-- > 0) {
int a = getInt(), b = getInt(), c = getInt();
g[a][b] = g[b][a] = Math.min(g[a][b], c);
}
long t = prim();
if (t == INF) pw.println("orz");
else pw.println(t);
pw.flush();
}
public static long prim() {
Arrays.fill(dist, (int) 1e10);
long res = 0;
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
if (i != 0 && dist[t] == INF) return INF;
if (i != 0) res += dist[t];
for (int j = 1; j <= n; j++) {
dist[j] = Math.min(dist[j], g[t][j]);
}
vis[t] = true;
}
return res;
}
public static int getInt() throws Exception {
st.nextToken();
return (int) st.nval;
}
}
by TuringCat @ 2024-03-21 23:38:57
java是这样的, 好多题明明复杂度都对但是就是mle
by Wancy123 @ 2024-04-02 16:20:39
用这个去读 亲测能过
static class Read{
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public int nextInt() throws Exception{
st.nextToken();
return (int)st.nval;
}
}
by Wancy123 @ 2024-04-02 16:53:34
报一丝 刚看到你也是这样读的 我是用kruskal写的能过