cf 1245D (Java Kruskal - Prim算法实现)

生成树题目。Prim或是Kruskal均可。

Kruskal代码:

import java.io.*;
import java.lang.*;
import java.rmi.*;
import java.security.*;
import java.util.*;
import java.math.*;

public class D {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tok;

    static boolean hasNext() {
        while (tok == null || !tok.hasMoreTokens())
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return false;
            }
        return true;
    }

    static String next() {
        hasNext();
        return tok.nextToken();
    }

    static String nextLine() {
        try {
            return in.readLine();
        } catch (Exception e) {
            return null;
        }
    }

    static long nextLong() {
        return Long.parseLong(next());
    }

    static int nextInt() {
        return Integer.parseInt(next());
    }

    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static final int maxn = 2005;
    static int n;
    static int x[] = new int[maxn];
    static int y[] = new int[maxn];
    static int p[] = new int[maxn];
    static long c[] = new long[maxn];
    static long k[] = new long[maxn];

    static class Edge {
        public int u, v;
        public long w;

        Edge(int x, int y, long z) {
            u = x;
            v = y;
            w = z;
        }
    }

    static int find(int x) {
        return (p[x] == x) ? x : (p[x] = find(p[x]));
    }

    public static void main(String[] args) throws IOException {
        n = nextInt();
        for (int i = 1; i <= n; ++i) {
            x[i] = nextInt();
            y[i] = nextInt();
        }
        for (int i = 1; i <= n; ++i) c[i] = nextInt();
        for (int i = 1; i <= n; ++i) k[i] = nextInt();
        for (int i = 1; i <= n; ++i) p[i] = i;
        ArrayList<Edge> E = new ArrayList<>();
        for (int i = 1; i <= n; ++i) E.add(new Edge(0, i, c[i]));
        for (int i = 1; i <= n; ++i) {
            for (int j = i + 1; j <= n; ++j) {
                E.add(new Edge(i, j, (k[i] + k[j]) * (Math.abs(x[i] - x[j]) + Math.abs(y[i] - y[j]))));
            }
        }
        Collections.sort(E, (o1, o2) -> (Long.signum(o1.w - o2.w)));
        ArrayList<Integer> v1 = new ArrayList<>();
        ArrayList<int[]> v2 = new ArrayList<>();
        long ct = 0;
        for (Edge i : E) {
            int x = find(i.u), y = find(i.v);
            if (x != y) {
                ct += i.w;
                p[x] = y;
                if (i.u == 0) v1.add(i.v);
                else v2.add(new int[]{i.u, i.v});
            }
        }
        out.println(ct);
        out.println(v1.size());
        for (Integer i : v1) out.print(i + " ");
        out.println();
        out.println(v2.size());
        for (int[] i : v2) out.print(i[0] + " " + i[1] + "\n");
        out.flush();
    }
}

prim写法:

import java.io.*;
import java.lang.*;
import java.rmi.*;
import java.security.*;
import java.util.*;
import java.math.*;

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tok;

    static boolean hasNext() {
        while (tok == null || !tok.hasMoreTokens())
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return false;
            }
        return true;
    }

    static String next() {
        hasNext();
        return tok.nextToken();
    }

    static String nextLine() {
        try {
            return in.readLine();
        } catch (Exception e) {
            return null;
        }
    }

    static long nextLong() {
        return Long.parseLong(next());
    }

    static int nextInt() {
        return Integer.parseInt(next());
    }

    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static final int maxn = 2005;
    static int n;
    static int x[] = new int[maxn];
    static int y[] = new int[maxn];
    static long c[] = new long[maxn];
    static long k[] = new long[maxn];
    static boolean vis[] = new boolean[maxn];

    public static void main(String[] args) throws IOException {
        n = nextInt();
        for (int i = 1; i <= n; ++i) {
            x[i] = nextInt();
            y[i] = nextInt();
        }
        for (int i = 1; i <= n; ++i) c[i] = nextInt();
        for (int i = 1; i <= n; ++i) k[i] = nextInt();
        PriorityQueue<long[]> Q = new PriorityQueue<>(new Comparator<long[]>() {
            public int compare(long[] o1, long[] o2) {
                return Long.signum(o1[0] - o2[0]);
            }
        });
        for (int i = 1; i <= n; ++i) Q.add(new long[]{c[i], (long) 0, (long) i});
        ArrayList<Integer> v1 = new ArrayList<>();
        ArrayList<int[]> v2 = new ArrayList<>();
        long ct = 0;
        vis[0] = true;
        while (!Q.isEmpty()) {
            long[] u = Q.poll();
            if (vis[(int) u[2]]) continue;
            ct += u[0];
            if (u[1] == 0) v1.add((int) u[2]);
            else v2.add(new int[]{(int) u[1], (int) u[2]});
            vis[(int) u[2]] = true;
            for (int i = 1; i <= n; ++i) {
                if (!vis[i]) {
                    long d = (k[(int) u[2]] + k[i]) * (Math.abs(x[(int) u[2]] - x[i]) + Math.abs(y[(int) u[2]] - y[i]));
                    if (d < c[i]) {
                        c[i] = d;
                        Q.add(new long[]{d, u[2], i});
                    }
                }
            }
        }
        out.println(ct);
        out.println(v1.size());
        for (Integer i : v1) out.print(i + " ");
        out.println();
        out.println(v2.size());
        for (int[] i : v2) out.print(i[0] + " " + i[1] + "\n");
        out.flush();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值