Dynamic Planning Summary

Source: Internet
Author: User

State compression DP:

1. Matrix Type:

Enumerates all state states of the current row, plus qualifying conditions.

Enumerates all the states of the previous row, plus a qualifying condition.

......

Find the recursive equation,

Basic method: Enumerates each line of state and is recursively pushed up and down one line.

Classic examples:

# include <iostream>

# include <cstring>

# include <cmath>

using namespace Std;

int map[175][175];

int cl[4096];// All legal states

int num[4096];

int dp[102][202][202];

int n, m;

int a[4096] = {0};

judging a State

bool OK (int i)

{

if ((i& (i<<2)) ==0 && (i& (i>>2)) ==0)

return true;

return false;

}

BOOL Judge (int x, int y)

{

if ((x&y) = = 0)

return true;

return false;

}

BOOL Judge1 (int x, int y)

{

if (((x<<1) &y)! = 0 | | ((x>>1) &y)!=0)

return false;

return true;

}

int shu (int x)// calculates the number of 1 in the binary of x

{

int ret=0;

while (x)

{

if (x&1) ret++;

x>>=1;

}

return ret;

}

bool Vis (int i, int t)

{

if ((a[i]&t)! = t)

return false;

return true;

}

int main ()

{

while (CIN >> n >> m)

{

if (N < 0 | | m < 0)

Continue

Input Processing

memset (map, 0, sizeof (map));

memset (A, 0, sizeof (a));

for (int i = 1; I <= n; i++)

{

A[i] = 0;

for (int j = 1; j <= M; j + +)

{

CIN >> Map[i][j];

if (Map[i][j])

A[i] + = POW (2, j-1);

}

}

int Jishu = 0;

for (int i = 0; i < (1<<m); i++)

{

if (ok (i))

{

Cl[jishu] = i;

num[jishu++] = Shu (i);

}

}

Memset (DP,-1, sizeof (DP));

for (int i = 0; i < Jishu; i++)

if (Vis (1, cl[i]))

Dp[1][i][0] = Num[i];

for (int i = 2; i<= n; i++)

{

for (int j = 0; J < Jishu; J + +)//i

{

if (!vis (i, cl[j]))

Continue

for (int k = 0; k < Jishu; k++)//I-1

{

if (!judge1 (Cl[j], cl[k]))

Continue

if (!vis (I-1, cl[k]))

Continue

for (int x = 0; x < Jishu; × x + +)//I-2

{

if (!vis (I-2, cl[x]))

Continue

if (!judge1 (Cl[k], cl[x]) | |!judge (CL[J], cl[x]))

Continue

Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][k][x] + num[j]);

}

}

}

}

int ret = 0;

for (int i = 0; i < Jishu; i++)

{

for (int j = 0; J < Jishu; J + +)

{

if (Dp[n][i][j] > Ret)

ret = Dp[n][i][j];

}

}

cout << ret << Endl;

}

return 0;

}

2. Linear type

To reach a certain state from a certain starting state

Enumerates all States (starting from the original state);

Traverse all linear items to determine if this state contains the item,if(Yes),,, Else

Basic method: From the previous state deduced after a state, judging conditions, etc.

Examples:

# include <iostream>

# include <string>

# include <cstring>

using namespace Std;

const int MAX = 1<<16;

const int INF = 0X3F3F3F3F;

struct Sub

{

String name;

int date;

int need;

}a[max];

BOOL Vis[max];

struct DP

{

int cost;

int pre;

int core;

}dp[max];

void output (int now)

{

int next = dp[now].pre ^ now;

int id = 0;

Next >>= 1;

while (next)

{

id++;

Next >>= 1;

}

if (Dp[now].pre! = 0)

{

Output (Dp[now].pre);

}

cout << a[id].name << Endl;

}

int main ()

{

int t;

CIN >> T;

while (t--)

{

int n;

CIN >> N;

for (int i = 0; i < n; i++)

{

CIN >> a[i].name >> a[i].date >> a[i].need;

}

Memset (Vis, false, sizeof (false));

for (int i = 0; i < MAX; i++)

{

Dp[i].core = INF;

}

Dp[0].cost = 0;

Dp[0].pre =-1;

Dp[0].core = 0;

Vis[0] = true;

for (int i = 0; i < (1<<n); i++)

{

for (int j = 0; J < N; j + +)

{

if ((i& (1<<j)) = = 0)

{

int next = i| (1<<J);

Dp[next].cost = Dp[i].cost + a[j].need;

int Min = dp[next].cost-a[j].date;

if (Min < 0)

Min = 0;

Min + = Dp[i].core;

if (Vis[next])

{

if (Min < Dp[next].core)

{

Dp[next].core = Min;

Dp[next].pre = i;

}

}

Else

{

Vis[next] = true;

Dp[next].core = Min;

Dp[next].pre = i;

}

}

}

}

cout << dp[(1<<n)-1].core << Endl;

Output ((1<<n)-1);

}

return 0;

}

3,TSP problem

Normally, the Floyd algorithm needs to be preprocessed,

Then enumerate the state, enumerate where it arrives, and determine the optimal scheme to reach the city in a certain state.

Classic examples:

# include <iostream>

# include <cstdio>

# include <cstring>

const int INF = 0X3F3F3F3F;

using namespace Std;

int dis[155][155];

int dp[1<<16][16];

int num[22];

int earn[22];

int cost[22];

int main ()

{

int t;

scanf ("%d", &t);

while (t--)

{

int n, m, Mon;

scanf ("%d%d%d", &n, &m, &mon);

memset (DIS, INF, sizeof (DIS));

for (int i = 1; I <= n; i++)

Dis[i][i] = 0;

int A, b, C;

while (m--)

{

scanf ("%d%d%d", &a, &b, &c);

if (c < dis[a][b])

DIS[A][B] = dis[b][a] = C;

}

for (int k = 1; k <= N; k++)

for (int i = 1; I <= n; i++)

if (dis[i][k]! = INF)

for (int j = 1; J <= N; j + +)

if (dis[k][j]! = INF && dis[i][k] + dis[k][j] < Dis[i][j])

DIS[I][J] = Dis[i][k] + dis[k][j];

Memset (DP,-1, sizeof (DP));

int h, TMP;

scanf ("%d", &h);

for (int i = 0; i < H; i++)

scanf ("%d%d%d", &num[i], &earn[i], &cost[i]);

for (int i = 0; i < H; i++)

{

TMP = Mon-dis[1][num[i]]-cost[i];

if (TMP >= 0)

Dp[1<<i][i] = tmp + earn[i];

}

int st = (1 << h)-1;

for (int i = 1; I <= St; i++)

for (int j = 0; J < H; j + +)

{

if (Dp[i][j] < 0)

Continue

for (int k = 0; k < h; k++)

{

if (I & (1<<k))

Continue

TMP = Dp[i][j]-dis[num[j]][num[k]]-cost[k];

if (TMP >= 0)

{

TMP + = Earn[k];

int stat = i + (1<<k);

Dp[stat][k] = max (dp[stat][k], TMP);

}

}

}

int flag = 0;

for (int i = 0; i < H; i++)

{

TMP = Dp[st][i]-dis[num[i]][1];

if (TMP >= 0)

{

flag = 1;

Break

}

}

if (flag)

printf ("yes\n");

Else

printf ("no\n");

}

return 0;

}

* * two-tone travel business issues

The question has not yet been understood .....

# include <iostream>

# include "Cstdio"

# include "CString"

# include "Algorithm"

# include "Cmath"

using namespace Std;

int N;

struct NODE

{

Double X;

Double y;

}P[205];

Double dis[205][205], dp[205][205];

void Solve ()

{

DP[1][2] = dis[1][2];

for (int j = 3; J <= N; j + +)

{

for (int i = 1; i < j-1; i++)

{

DP[I][J] = Dp[i][j-1] + dis[j-1][j];

}

DP[J-1][J] = 999999999;

for (int i = 1; i < j-1; i++)

{

Dp[j-1][j] = min (Dp[j-1][j], dp[i][j-1] + dis[i][j]);

}

}

Dp[n][n] = Dp[n-1][n] + dis[n-1][n];

}

int main ()

{

while (~SCANF ("%d", &n))

{

for (int i = 1; I <= N; i++)

scanf ("%lf%lf", &p[i].x, &P[I].Y);

for (int i = 1; I <= N; i++)

for (int j = i + 1; J <= n;j++)

DIS[I][J] = sqrt ((p[i].x-p[j].x) * (p[i].x-p[j].x) + (P[I].Y-P[J].Y) * (P[I].Y-P[J].Y));

Solve ();

printf ("%.2lf\n", Dp[n][n]);

}

}

Summing up the topic for these days (continuous update ...) . )

Dynamic Planning Summary

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.