HDU 4085 Peach Blossom Spring memory-based search enumeration subset stanna tree

Source: Internet
Author: User

HDU 4085 Peach Blossom Spring memory-based search enumeration subset stanna tree

Question link: Click the open link

Question:

Enter n vertices and m in the first line. k individuals with undirected edges can be constructed.

The following section describes the costs of constructing a side and a side.

At the beginning, k people are at each point of 1-k (one person at each point)

Objective: To build partial edges from m fixed edges to minimize costs

Let k people move to k points after [n-k + 1, n] (put one person at each point ).

Ideas:

The first is a stanna tree, or a dp array first (solution: Click to open the link)

Dp [I] [j] indicates the minimum cost of using I as the root and j as the 8-Point sub-tree of I.

The question is how to find the answer.

Because the path from a person to his target point may not be connected with others, that is, multiple minimal spanning trees,

We enumerate the points of 2 * k points in a connected component,

In status x, the binary value of a person is low k bits, indicating that the target point is high k bits, and 1 in x indicates that these points are in a connected component,

The minimum cost for this x is min (dp [anypoint regard root] [x]).

X must ensure that the number of 1 in the low k bit and the number of 1 in the high k bit are the same (that is, the number of people and the target point are the same)

Then you can perform the memory-based search.

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     #include 
      
       #include 
       
        #include template 
        
         inline bool rd(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c<'0' || c>'9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1;}template 
         
          inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if (x>9) pt(x / 10); putchar(x % 10 + '0');}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;}using namespace std;const int inf = 1e8;const int N = 55;const int M = 500010;int n, m, k, ans;int dis[N][N], dp[N][1 << 10];int e[10];int vis[N];int mem[1 << 10];int check(int x){ int a = 0, b = 0; for (int i = 0; i < k; i++) if ((x&(1 << i))>0)a++; for (int i = 0; i < k; i++) if ((x&(1 << (i + k)))>0)b++; if (a != b)return -1; int ans = inf; for (int i = 0; i < n; i++) ans = min(ans, dp[i][x]); return ans;}int dfs(int x){ if (mem[x] != -1)return mem[x]; int tmp = check(x); if (tmp == -1)return mem[x] = inf; int ans = tmp; for (int i = (x-1)&x; i > 0; i = (i - 1)&x){ ans = min(ans, dfs(i) + dfs(x - i)); } return mem[x] = ans;}void floyd(){ for (int z = 0; z < n; z++) for (int j = 0; j < n; j++) for (int i = 0; i < n; i++) dis[i][j] = min(dis[i][j], dis[j][z] + dis[z][i]);}void input(){ rd(n); rd(m); rd(k); for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)dis[i][j] = (i==j)?0:inf; int u, v, d; while (m-->0){ rd(u); rd(v); rd(d); u--; v--; dis[u][v] = dis[v][u] = min(dis[u][v], d); } for (int z = 0; z < n; z++) for (int j = 0; j < n; j++) for (int i = 0; i < n; i++) dis[i][j] = min(dis[i][j], dis[j][z] + dis[z][i]);}int main(){ int T; rd(T); while (T-->0){ input(); floyd(); for (int i = 0; i < k; i++)e[i] = i; for (int i = 0; i < k; i++)e[i + k] = n - k + i; for (int i = 0; i < n; i++)for (int j = 0; j < (1 << (2 * k)); j++)dp[i][j] = inf; for (int i = 0; i < n; i++){ for (int j = 0; j < 2 * k; j++) dp[i][1 << j] = dis[i][e[j]]; } for (int i = 1; i < (1 << (2 * k)); i++){ if (0 == (i&(i - 1)))continue; for (int j = 0; j < n; j++){ for (int sub = i; sub > 0; sub = (sub - 1)&i) dp[j][i] = min(dp[j][i], dp[j][sub] + dp[j][i-sub]); } for (int j = 0; j < n; j++)vis[j] = 0; for (int j = 0; j < n; j++){ int a = inf, pos = 0; for (int z = 0; z < n; z++) if (vis[z] == 0 && dp[z][i] <= a) a = dp[pos = z][i]; vis[pos] = 1; for (int z = 0; z < n; z++) dp[pos][i] = min(dp[pos][i], dp[z][i] + dis[z][pos]); } } for (int j = k; j < 2 * k; j++)vis[j] = -1; memset(mem, -1, sizeof mem); mem[0] = 0; ans = dfs((1<<(2*k))-1); if (ans == inf)printf("No solution\n"); else printf("%d\n",ans); } return 0;}
         
        
       
      
    
   
  
 


import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;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.StringTokenizer;import java.util.TreeMap;import java.util.TreeSet;import java.util.Queue;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class Main {int n, m, k, ans;int[][] dis = new int[N][N], dp = new int[N][1<<10];int[] e = new int[10];int[] vis = new int[N], mem = new int[1<<10];int check(int x){int a = 0, b = 0;for (int i = 0; i < k; i++)if ((x&(1 << i))>0)a++;for (int i = 0; i < k; i++)if ((x&(1 << (i + k)))>0)b++;if (a != b)return -1;int ans = inf;for (int i = 0; i < n; i++)ans = min(ans, dp[i][x]);return ans;}int dfs(int x){if (mem[x] != -1)return mem[x];int tmp = check(x);if (tmp == -1)return mem[x] = inf;int ans = tmp;for (int i = (x-1)&x; i > 0; i = (i - 1)&x){ans = min(ans, dfs(i) + dfs(x - i));}return mem[x] = ans;}void floyd(){for(int z = 0; z < n; z++)for(int j = 0; j < n; j++)for(int i = 0; i < n; i++)dis[i][j] = min(dis[i][j], dis[j][z] + dis[z][i]);}void input() throws Exception{for(int i = 0; i < n; i++)for(int j = 0; j < n; j++)dis[i][j] = (i==j)?0:inf;while(m-->0){int u = Int() - 1, v = Int() - 1, d = Int();dis[u][v] = dis[v][u] = min(dis[u][v], d);}}void work() throws Exception{int T = Int(); while(T-->0){n = Int(); m = Int(); k = Int();input();floyd();for (int i = 0; i < k; i++)e[i] = i; for (int i = 0; i < k; i++)e[i + k] = n - k + i;for (int i = 0; i < n; i++)for (int j = 0; j < (1 << (2 * k)); j++)dp[i][j] = inf;for (int i = 0; i < n; i++){for (int j = 0; j < 2 * k; j++)dp[i][1 << j] = dis[i][e[j]];}for (int i = 1; i < (1 << (2 * k)); i++){if (0 == (i&(i - 1)))continue;for (int j = 0; j < n; j++){for (int sub = i; sub > 0; sub = (sub - 1)&i)dp[j][i] = min(dp[j][i], dp[j][sub] + dp[j][i-sub]);}for (int j = 0; j < n; j++)vis[j] = 0;for (int j = 0; j < n; j++){int a = inf, pos = 0;for (int z = 0; z < n; z++)if (vis[z] == 0 && dp[z][i] <= a)a = dp[pos = z][i];vis[pos] = 1;for (int z = 0; z < n; z++)dp[pos][i] = min(dp[pos][i], dp[z][i] + dis[z][pos]);}}for(int i = 1; i < (1<<(2*k)); i++)mem[i] = -1;mem[0] = 0;ans = dfs((1<<(2*k))-1);if(ans == inf)out.println("No solution");else out.println(ans);}}    public static void main(String[] args) throws Exception{        Main wo = new Main();    in = new BufferedReader(new InputStreamReader(System.in));    out = new PrintWriter(System.out);  //  in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));  //  out = new PrintWriter(new File("output.txt"));        wo.work();        out.close();    }static int N = 55;static int M = 2005;DecimalFormat df=new DecimalFormat("0.0000");static int inf = (int)1e8;static long inf64 = (long) 1e18*2;static double eps = 1e-8;static double Pi = Math.PI;static int mod = 1000000009 ;private String Next() throws Exception{    while (str == null || !str.hasMoreElements())        str = new StringTokenizer(in.readLine());    return str.nextToken();    }    private int Int() throws Exception{    return Integer.parseInt(Next());    }    private long Long() throws Exception{    return Long.parseLong(Next());    }    private double Double() throws Exception{    return Double.parseDouble(Next());    }    StringTokenizer str;    static Scanner cin = new Scanner(System.in);    static BufferedReader in;    static PrintWriter out;  /*  class Edge{int from, to, dis, nex;Edge(){}Edge(int from, int to, int dis, int nex){this.from = from;this.to = to;this.dis = dis;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, int dis){edge[edgenum] = new Edge(u, v, dis, 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);}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.