POJ 刷题系列:1062. 昂贵的聘礼

来源:互联网 发布:装修效果软件手机软件 编辑:程序博客网 时间:2024/06/08 12:03

POJ 刷题系列:1062. 昂贵的聘礼

传送门:1062. 昂贵的聘礼

题意:

年轻的探险家来到了一个印第安部落里。在那里他和酋长的女儿相爱了,于是便向酋长去求亲。酋长要他用10000个金币作为聘礼才答应把女儿嫁给他。探险家拿不出这么多金币,便请求酋长降低要求。酋长说:”嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币。如果你能够弄来他的水晶球,那么只要5000金币就行了。”探险家就跑到大祭司那里,向他要求皮袄或水晶球,大祭司要他用金币来换,或者替他弄来其他的东西,他可以降低价格。探险家于是又跑到其他地方,其他人也提出了类似的要求,或者直接用金币换,或者找到其他东西就可以降低价格。不过探险家没必要用多样东西去换一样东西,因为不会得到更低的价格。探险家现在很需要你的帮忙,让他用最少的金币娶到自己的心上人。另外他要告诉你的是,在这个部落里,等级观念十分森严。地位差距超过一定限制的两个人之间不会进行任何形式的直接接触,包括交易。他是一个外来人,所以可以不受这些限制。但是如果他和某个地位较低的人进行了交易,地位较高的的人不会再和他交易,他们认为这样等于是间接接触,反过来也一样。因此你需要在考虑所有的情况以后给他提供一个最好的方案。

为了方便起见,我们把所有的物品从1开始进行编号,酋长的允诺也看作一个物品,并且编号总是1。每个物品都有对应的价格P,主人的地位等级L,以及一系列的替代品Ti和该替代品所对应的”优惠”Vi。如果两人地位等级差距超过了M,就不能”间接交易”。你必须根据这些数据来计算出探险家最少需要多少金币才能娶到酋长的女儿。

输入:

输入第一行是两个整数M,N(1 <= N <= 100),依次表示地位等级差距限制和物品的总数。接下来按照编号从小到大依次给出了N个物品的描述。每个物品的描述开头是三个非负整数P、L、X(X < N),依次表示该物品的价格、主人的地位等级和替代品总数。接下来X行每行包括两个整数T和V,分别表示替代品的编号和”优惠价格”。

输出:

输出最少需要的金币数。

思路:
实际上是一个图模型,采用递归,物品之间的交换有一条关系边,比如国王需要物品2,那么可以用8000来交换,此时子问题就变成了从物品2出发,所需要的最少金币数是多少。当然,这里还需要注意等级差不能超过M的限制。在递归时,维护一个全局的等级min,max,在这个范围内的才能互相交换物品。其次,为了加快递归速度,可以采用记忆化搜索。

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.StringTokenizer;public class Main{    String INPUT = "./data/judge/201712/P1062.txt";    public static void main(String[] args) throws IOException {        new Main().run();    }    static final int INF = 0x3f3f3f3f;    List<int[]>[] subs;    int N, M;    int[] goods;    int[] rates;    int[][][] mem;    int dfs(int s, int min, int max, boolean[] vis) {        if (mem[s][min][max] > 0) return mem[s][min][max];        int ans = goods[s];        vis[s] = true;        for (int[] other : subs[s]) {            if (!vis[other[0]] && Math.abs(max - rates[other[0]]) <= M && Math.abs(min - rates[other[0]]) <= M) {                int maxx = Math.max(max, rates[other[0]]);                int minn = Math.min(min, rates[other[0]]);                ans = Math.min(ans, other[1] + dfs(other[0], minn, maxx, vis));            }        }        vis[s] = false;        mem[s][min][max] = ans;        return ans;    }    void read() {        M = ni();        N = ni();        goods = new int[N];        rates = new int[N];        subs = new ArrayList[N];        for (int i = 0; i < N; ++i) subs[i] = new ArrayList<int[]>();        int maxR = 0;        for (int i = 0; i < N; ++i) {            goods[i] = ni();   // P            rates[i] = ni();   // L            maxR = Math.max(maxR, rates[i]);            int X = ni();            for (int j = 0; j < X; ++j) {                int T = ni();                T --;                int V = ni();                subs[i].add(new int[] {T, V});            }        }        mem = new int[N][maxR + 1][maxR + 1];        out.println(dfs(0, rates[0], rates[0], new boolean[N]));    }    FastScanner in;    PrintWriter out;    void run() throws IOException {        boolean oj;        try {            oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm");        } catch (Exception e) {            oj = System.getProperty("ONLINE_JUDGE") != null;        }        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));        in = new FastScanner(is);        out = new PrintWriter(System.out);        long s = System.currentTimeMillis();        read();        out.flush();        if (!oj){            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");        }    }    public boolean more(){        return in.hasNext();    }    public int ni(){        return in.nextInt();    }    public long nl(){        return in.nextLong();    }    public double nd(){        return in.nextDouble();    }    public String ns(){        return in.nextString();    }    public char nc(){        return in.nextChar();    }    class FastScanner {        BufferedReader br;        StringTokenizer st;        boolean hasNext;        public FastScanner(InputStream is) throws IOException {            br = new BufferedReader(new InputStreamReader(is));            hasNext = true;        }        public String nextToken() {            while (st == null || !st.hasMoreTokens()) {                try {                    st = new StringTokenizer(br.readLine());                } catch (Exception e) {                    hasNext = false;                    return "##";                }            }            return st.nextToken();        }        String next = null;        public boolean hasNext(){            next = nextToken();            return hasNext;        }        public int nextInt() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Integer.parseInt(more);        }        public long nextLong() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Long.parseLong(more);        }        public double nextDouble() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Double.parseDouble(more);        }        public String nextString(){            if (next == null){                hasNext();            }            String more = next;            next = null;            return more;        }        public char nextChar(){            if (next == null){                hasNext();            }            String more = next;            next = null;            return more.charAt(0);        }    }    static class D{        public static void pp(int[][] board, int row, int col) {            StringBuilder sb = new StringBuilder();            for (int i = 0; i < row; ++i) {                for (int j = 0; j < col; ++j) {                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));                }            }            System.out.println(sb.toString());        }        public static void pp(char[][] board, int row, int col) {            StringBuilder sb = new StringBuilder();            for (int i = 0; i < row; ++i) {                for (int j = 0; j < col; ++j) {                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));                }            }            System.out.println(sb.toString());        }    }    static class ArrayUtils {        public static void fill(int[][] f, int value) {            for (int i = 0; i < f.length; ++i) {                Arrays.fill(f[i], value);            }        }        public static void fill(int[][][] f, int value) {            for (int i = 0; i < f.length; ++i) {                fill(f[i], value);            }        }        public static void fill(int[][][][] f, int value) {            for (int i = 0; i < f.length; ++i) {                fill(f[i], value);            }        }    }    static class Num{        public static <K> void inc(Map<K, Integer> mem, K k) {            if (!mem.containsKey(k)) mem.put(k, 0);            mem.put(k, mem.get(k) + 1);        }    }}

这里写图片描述

原创粉丝点击