算法训练 安慰奶牛

ALGO-6 安慰奶牛

资源限制

时间限制:1.0s 内存限制:256.0MB

问题描述

Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路。道路被用来连接N个牧场,牧场被连续地编号为1到N。每一个牧场都是一个奶牛的家。FJ计划除去P条道路中尽可能多的道路,但是还要保持牧场之间 的连通性。你首先要决定那些道路是需要保留的N-1条道路。第j条双向道路连接了牧场Sj和Ej(1 <= Sj <= N; 1 <= Ej <= N; Sj != Ej),而且走完它需要Lj的时间。没有两个牧场是被一条以上的道路所连接。奶牛们非常伤心,因为她们的交通系统被削减了。你需要到每一个奶牛的住处去安慰她们。每次你到达第i个牧场的时候(即使你已经到过),你必须花去Ci的时间和奶牛交谈。你每个晚上都会在同一个牧场(这是供你选择的)过夜,直到奶牛们都从悲伤中缓过神来。在早上 起来和晚上回去睡觉的时候,你都需要和在你睡觉的牧场的奶牛交谈一次。这样你才能完成你的 交谈任务。假设Farmer John采纳了你的建议,请计算出使所有奶牛都被安慰的最少时间。

输入格式

第1行包含两个整数N和P。
接下来N行,每行包含一个整数Ci。
接下来P行,每行包含三个整数Sj, Ej和Lj。

输出格式

输出一个整数, 所需要的总时间(包含和在你所在的牧场的奶牛的两次谈话时间)。

测试样例

输入:
5 6
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6

输出:
176

数据规模与约定

5 <= N <= 10000,N-1 <= P <= 100000,0 <= Lj <= 1000,1 <= Ci <= 1,000。

ps: 这个测试用例的结果是错误的,正确答案应为 178

Prim :
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        int N = in.nextInt(), P = in.nextInt(), cnt = Integer.MAX_VALUE;
        Graph<Edge> graph = new Graph(N);
        int[] distTo = new int[graph.size];
        int[] VWeight = new int[graph.size];
        boolean[] marked = new boolean[graph.size];
        Queue<PQItem> queue = new PriorityQueue(graph.size);
        Arrays.fill(distTo, Integer.MAX_VALUE);
        for (int i = 1; i <= N; i++) if (cnt > (VWeight[i] = in.nextInt())) cnt = VWeight[i];
        for (int V, E, weight; P > 0; P--) {
            V = in.nextInt();
            E = in.nextInt();
            weight = in.nextInt() * 2 + VWeight[V] + VWeight[E];
            graph.add(V, new Edge(E, weight));
            graph.add(E, new Edge(V, weight));
        }
        queue.offer(new PQItem(0, 1));
        while (queue.size() > 0) {
            int V = queue.poll().V;
            marked[V] = true;
            for (Edge e : graph.adj(V)) {
                if (marked[e.E]) continue;
                if (e.weight < distTo[e.E]) {
                    distTo[e.E] = e.weight;
                    PQItem item = new PQItem(e.weight, e.E);
                    queue.remove(item);
                    queue.offer(item);
                }
            }
        }
        for (int i = 2; i <= N; i++) if (distTo[i] != Integer.MAX_VALUE) cnt += distTo[i];
        System.out.print(cnt);
        in.close();
    }

    static class PQItem implements Comparable<PQItem>{

        int index;
        int V;

        PQItem (int index, int V) {
            this.V = V;
            this.index = index;
        }

        @Override
        public int compareTo(PQItem o) {
            if (this.index > o.index) return +1; // 不建议这么写
            return -1;
        }

        @Override
        public boolean equals(Object o) {
            return this.V == ((PQItem)o).V;
        }
    }

    static class Edge {

        int E;
        int weight;

        Edge(int E, int weight) {
            this.E = E;
            this.weight = weight;
        }
    }

    static class Graph<T> {

        List adj[];
        int size;

        Graph(int size) {
            this.size = size + 1;
            adj = new List[this.size];
            for (int i = 1; i <= size; i++) adj[i] = new ArrayList();
        }

        void add(int V, T E) { adj[V].add(E); }

        List<T> adj(int V) { return adj[V]; }
    }

    static class InputReader {

        BufferedReader read;
        StringTokenizer tokens;
        String delimiters;

        InputReader (InputStream in, String delimiters) {
            this.read = new BufferedReader(new InputStreamReader(in));
            this.delimiters = delimiters;
            this.tokens = new StringTokenizer("", delimiters);
        }

        InputReader (InputStream in) { this(in, " \t\n\r\f"); }

        String next() throws IOException {
            while (!tokens.hasMoreTokens()) tokens = new StringTokenizer(read.readLine(), delimiters);
            return tokens.nextToken();
        }

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

        void close() throws IOException { read.close(); }
    }
}

把队列封装一下可能会看的明白一点

IndexPQ:

static class IndexPQ<T> {

    Queue<Item> queue;
    int compare1;
    int compare2;

    IndexPQ() {
        this("MIN");
    }

    IndexPQ(String priority) {
        if (priority.equalsIgnoreCase("max")) {
            compare1 = +1;
            compare2 = -1;
        } else {
            compare1 = -1;
            compare2 = +1;
        }
        this.queue = new PriorityQueue();
    }

    boolean offer(T item, float index) { return queue.offer(new Item(item, index)); }

    T poll() {
        if (queue.size() == 0) return null;
        return queue.poll().item;
    }

    int size() { return queue.size(); }

    boolean contains(T item) { return queue.contains(new Item(item, 0f)); }

    class Item implements Comparable<Item> {

        float index;
        T item;

        Item(T item, float index) {
            this.item = item;
            this.index = index;
        }

        @Override
        public int compareTo(Item o) {
            if (this.index < o.index) return compare1;
            if (this.index > o.index) return compare2;
            return 0;
        }

        @Override
        public boolean equals(Object o) {
            return this.item.equals(((Item)o).item);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值