CodeForces 417D Cunning Gena 状压dp

来源:互联网 发布:阿里云文档管理 编辑:程序博客网 时间:2024/04/30 22:16

题目链接:点击打开链接

题意:主角请他的n个朋友帮他做m道题,常数b

下面每两行代表一个朋友:

请这个朋友的花费 请这个朋友的前提是要拥有k台电脑 这个朋友能解的题目数量

下面是这个朋友能解的具体题目号。

买一台电脑花费是b,开始主角手里没有电脑。

思路:

先按k值小到大排序

问题数<=20, 所以状压问题然后把一个个人依次更新到状态里即可。


import java.io.PrintWriter;import java.text.DecimalFormat;import java.util.ArrayDeque;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.Deque;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.Map;import java.util.PriorityQueue;import java.util.Scanner;import java.util.Stack;import java.util.TreeMap;import java.util.TreeSet;import java.util.Queue;public class Main {class Node implements Comparable<Node>{long x, k; int m, t;Node(){}Node(long x, long k, int m){this.x = x; this.k = k; this.m = m;this.t = 0;}public int compareTo(Node o) {return Long.compare(k, o.k);}}int n, m;long b;long[] dp = new long[1<<N];Node[] o = new Node[105];void input(){n = cin.nextInt(); m = cin.nextInt(); b = cin.nextInt();o[0] = new Node(0,0,0);for(int i = 1; i <= n; i++){o[i] = new Node(cin.nextInt(), cin.nextInt(), cin.nextInt());for(int j = 0; j < o[i].m; j ++)o[i].t |= (1<<(cin.nextInt()-1));}}void work(){input();Arrays.sort(o, 1, n+1);for(int i = 0; i < (1<<m); i++)dp[i] = inf64;dp[0] = 0; long ans = inf64;for(int i = 1; i <= n; i++){for(int j = 0; j < (1<<m); j++){if(dp[j] == inf)continue;int now = j|o[i].t;dp[now] = min(dp[now], dp[j] + o[i].x);}ans = min(ans, dp[(1<<m)-1]+o[i].k*b);}if(ans>=inf64)out.println("-1");elseout.println(ans);}Main() {cin = new Scanner(System.in);out = new PrintWriter(System.out);}public static void main(String[] args) {Main e = new Main();e.work();out.close(); }public Scanner cin;public static PrintWriter out;static int N = 20;static int M = 2005;DecimalFormat df=new DecimalFormat("0.0000");static int inf = (int) 1e9 + 7;static long inf64 = (long) 1e18*2;static double eps = 1e-8;static double Pi = Math.PI;static int mod = 1000000007 ;/*class Edge{int from, to, nex;Edge(){}Edge(int from, int to, int nex){this.from = from;this.to = to;this.nex = nex;}}Edge[] edge = new Edge[M<<1];int[] head = new int[N];int edgenum;void init_edge(){for(int i = 0; i < N; i++)head[i] = -1; edgenum = 0;}void add(int u, int v){edge[edgenum] = new Edge(u, v, head[u]);head[u] = edgenum++;}/*int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;int pos = r;r--;while (l <= r) {int mid = (l + r) >> 1;if (A[mid] <= val) {l = mid + 1;} else {pos = mid;r = mid - 1;}}return pos;}/**/int Pow(int x, int y) {int ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;y >>= 1;x = x * x;}return ans;}double Pow(double x, int y) {double ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;y >>= 1;x = x * x;}return ans;}int Pow_Mod(int x, int y, int mod) {int ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;ans %= mod;y >>= 1;x = x * x;x %= mod;}return ans;}long Pow(long x, long y) {long ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;y >>= 1;x = x * x;}return ans;}long Pow_Mod(long x, long y, long mod) {long ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;ans %= mod;y >>= 1;x = x * x;x %= mod;}return ans;}int gcd(int x, int y){if(x>y){int tmp = x; x = y; y = tmp;}while(x>0){y %= x;int tmp = x; x = y; y = tmp;}return y;}int max(int x, int y) {return x > y ? x : y;}int min(int x, int y) {return x < y ? x : y;}double max(double x, double y) {return x > y ? x : y;}double min(double x, double y) {return x < y ? x : y;}long max(long x, long y) {return x > y ? x : y;}long min(long x, long y) {return x < y ? x : y;}int abs(int x) {return x > 0 ? x : -x;}double abs(double x) {return x > 0 ? x : -x;}long abs(long x) {return x > 0 ? x : -x;}boolean zero(double x) {return abs(x) < eps;}double sin(double x){return Math.sin(x);}double cos(double x){return Math.cos(x);}double tan(double x){return Math.tan(x);}double sqrt(double x){return Math.sqrt(x);}}


0 0
原创粉丝点击