HDU 4085 Peach Blossom Spring eectilinear State compression DP+SPFA

Source: Internet
Author: User

State compression DP+SPFA Chestan Nashu

Enumeration subtree morphology dp[i][j] = min (Dp[i][j], dp[i][k]+dp[i][l]) where K and L are a division of J

relax by the edge dp[i][j] = min (dp[i][j], dp[i '][j]+w[i][j]) where I and I ' have an edge connected

#include <cstdio> #include <cstring> #include <queue>using namespace std;const int maxn = 55;const int ma XM = 1010;const int INF = 9999999;int N, m, K;int D[maxn][maxn];int DP[1&LT;&LT;16][MAXN], dp2[1<<16], a[1<<16 ];int Inq[1<<16][maxn];int FIRST[MAXN], cnt;struct Node{int V, W, next;} E[2222];struct edge{int u, V, W;edge () {}edge (int u, int v, int w): U (U), V (v), W (w) {}};queue <edge> q;void SPFA () {W Hile (! Q.empty ()) {Edge x = Q.front (); Q.pop (); int u = x.u, v = x.v;inq[u][v] = 0;for (int i = 0; i < n; i++) {if (d[v][i]! =-1) {if (Dp[u|a[i]][i] > Dp[u][v] + D[v][i]) {dp[u|a[i]][i] = Dp[u][v] + d[v][i];if (!inq[u|a[i]][i] && (u|a[i]) = = u) {inq[u|a[i]][i] = 1; Q.push (Edge (U|a[i], I, dp[u|a[i]][i]));}}}}    bool OK (int x) {int ans = 0;    for (int i = 0; x; i++, x >>= 1) ans + = (x&1) * (I < K 1:-1); return ans = = 0;} void Floyd () {for (int l = 0, l < n; l++) for (int i = 0, i < n; i++) for (int j = 0; J < N; j + +) if (d[I][L]! =-1 && d[l][j]! =-1) if (d[i][j] = = 1 | | d[i][j] > D[I][L]+D[L][J]) d[i][j] = D[i][l]+d[l][j];} void Steiner () {int all = (1<< (2*k)), for (int i = 0, i < all, i++) for (int j = 0; J < N; j + +) Dp[i][j] = Inf;memse T (A, 0, sizeof (a)); for (int i = 0; i < K; i++) a[i] = (1<<i), dp[(1<<i)][i] = 0;for (int i = k; i < 2*k; i+ +) A[n-2*k+i] = (1<<i), dp[(1<<i)][n-2*k+i] = 0;for (int s = 1; s < all; s++) {for (int i = 0; i < n; i++) {f or (int s0 = (s-1) &s; s0; s0 = (s0-1) &s) {Dp[s][i] = min (Dp[s][i], dp[s0|a[i]][i]+dp[(S^S0) |a[i]][i]);} if (Dp[s][i] < INF) {Q.push (Edge (S, I, dp[s][i])); inq[s][i] = 1;}} SPFA ();} for (int i = 0; i < all; i++) {Dp2[i] = inf;for (int j = 0; J < N; j + +) Dp2[i] = min (Dp2[i], dp[i][j]);} for (int i = 1; i < all; i++) {if (ok (i)) {(int s = (i-1) &i; s; s = (s-1) &i) {if (ok (i^s)) dp2[i] = min (dp2[i), DP2 [S]+dp2[i^s]);}}} if (dp2[all-1] = = INF) puts ("No solution"); elseprintf ("%d\n", Dp2[all-1]);} int main () {int t;sCANF ("%d", &t), while (t--) {scanf ("%d%d%d", &n, &m, &k), memset (d,-1, sizeof (d)), and for (int i = 0; i < m;  i++) {int x, Y, z;scanf ("%d%d%d", &x, &y, &z), x--, y--;if (d[x][y] = = 1 | | d[x][y] > z) d[x][y] = d[y][x] = Z;} Floyd (); Steiner ();} return 0;}



HDU 4085 Peach Blossom Spring eectilinear State compression DP+SPFA

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.