http://vjudge.net/contest/view.action?cid=51211#overview

花了好长时间了,终于把这个专题做了绝大部分了

A:HDU 3853

最简单的概率DP求期望,从终点推到起点就是了,注意一个坑就是如果p1=1那么他一旦到达这个点,那么就永远走不出去了

题解

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout) //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; double p1[MAXN][MAXN], p2[MAXN][MAXN], p3[MAXN][MAXN], dp[MAXN][MAXN]; int main()
{
int R, C;
while(~scanf("%d %d", &R, &C))
{
for(int i=;i<=R;i++)
for(int j=;j<=C;j++)
scanf("%lf%lf%lf", &p1[i][j], &p2[i][j], &p3[i][j]);
mem0(dp);
for(int i=R;i>=;i--)
for(int j=C;j>=;j--)
{
if(i==R && j==C) continue;
if(fabs(p1[i][j] - ) < eps) continue;
dp[i][j] = (dp[i][j+]*p2[i][j] + dp[i+][j]*p3[i][j] + ) / (-p1[i][j]) ;
}
printf("%.3lf\n", dp[][]);
}
return ;
}

B:HDU 3920

状态压缩DP  题解

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int T, N;
typedef double Point[];
Point st, p[MAXN];
double dis[MAXN][MAXN], d[MAXN], dp[<<]; double calc(Point a, Point b)
{
double x = a[] - b[];
double y = a[] - b[];
return sqrt(x*x + y*y);
} void getDis()
{
for(int i=;i<N;i++)
{
d[i] = calc(st, p[i]);
for(int j=i+;j<N;j++)
{
dis[j][i] = dis[i][j] = calc(p[i], p[j]);
}
}
} int main()
{
int t = ;
scanf("%d", &T);
while(T--)
{
scanf("%lf %lf", &st[], &st[]);
scanf("%d", &N);
N <<= ;
for(int i=;i<N;i++)
scanf("%lf %lf", &p[i][], &p[i][]);
getDis();
dp[] = ;
for(int i=;i<(<<N);i++) dp[i] = INF;
queue<int>q;
q.push();
while(!q.empty())
{
int now = q.front(); q.pop();
int f=, r;
while( now & (<<f) && f < N)
f++;
for(r = f + ; r < N; r ++ )
if(!(now & (<<r)))
{
int next = now | (<<f) | (<<r);
double minDis = MIN(d[f], d[r]) + dis[f][r];
if( fabs(dp[next] - INF) < eps )
{
q.push(next);
dp[next] = dp[now] + minDis;
}
else if( dp[now] + minDis < dp[next] )
dp[next] = dp[now] + minDis;
}
}
printf("Case #%d: %.2lf%\n", ++t, dp[(<<N)-]);
}
return ;
}

C:HDU 1520

简单的树形DP  题解

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int N, a[MAXN], dp[MAXN][];
int fa[MAXN];
vector<int>e[MAXN]; void DFS(int u)
{
int s0 = , s1 = ;
for(int i=;i<e[u].size();i++)
{
DFS(e[u][i]);
s0 += MAX( dp[e[u][i]][], dp[e[u][i]][] );
s1 += dp[e[u][i]][];
}
dp[u][] = s0;
dp[u][] = s1 + a[u];
} int main()
{
//FOPENIN("in.txt");
while(~scanf("%d", &N))
{
mem0(dp);
for(int i=;i<=N;i++)
{
scanf("%d", &a[i]);
fa[i] = i;
e[i].clear();
}
int x, y;
while(scanf("%d %d", &x, &y) && (x||y) ){
e[y].push_back(x);
fa[x] = y;
}
int ans = ;
for(int i=;i<=N;i++) if(fa[i] == i)
{
DFS(i);
ans += MAX(dp[i][], dp[i][]);
}
printf("%d\n", ans);
}
return ;
}

D:HDU 4284

状态压缩DP  题解

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e8
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int T, N, M, H, Money;
int dis[MAXN][MAXN], add[MAXN], cost[MAXN];
int citys[], dp[<<][]; void init()
{
int u, v, w, c, a;
mem0(add); mem0(cost);mem1(dp);
scanf("%d %d %d", &N, &M, &Money);
for(int i = ; i <= N ;i ++ )
{
dis[i][i] = ;
for(int j = ; j <= N ; j ++ )
if(i != j) dis[i][j] = INF;
}
for(int i = ; i < M; i ++ )
{
scanf("%d %d %d", &u, &v, &w);
dis[u][v] = dis[v][u] = MIN(dis[u][v], w);
}
scanf("%d", &H);
for(int i = ; i <= H; i ++ )
{
scanf("%d %d %d", &u, &a, &c);
citys[i] = u;
add[i] = a;
cost[i] = c;
}
} void floyd()
{
for(int k=;k<=N;k++)
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
{
dis[i][j] = MIN(dis[i][j], dis[i][k] + dis[k][j]);
}
} int main()
{
//FOPENIN("in.txt");
while(~scanf("%d", &T)) while(T--)
{
init();
floyd();
int ans = -INF; H += ;
citys[] = ; cost[] = add[] = ;
dp[][] = Money;
for(int now = ; now < (<<H); now ++ )
{
for(int u = ; u < H; u ++ ) if(dp[now][u] != -)
{
for(int v = ; v < H; v ++ ) if( (now & (<<v)) == )
{
int next = now | (<<v);
if(dp[now][u] >= dis[citys[u]][citys[v]] + cost[v] )
{
dp[next][v] = MAX(dp[now | (<<v)][v], dp[now][u] - dis[citys[u]][citys[v]] - cost[v] + add[v]);
}
if(next == (<<H) - )
{
ans = MAX(ans, dp[next][v]);
}
}
}
}
//printf("%d\n", ans);
printf("%s\n", ans >= ? "YES" : "NO");
}
return ;
}

E:HDU 2196

比较好的题目了,两次DFS,求树上任意点触发的最长距离 题解

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e8
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int N;
int head[MAXN], next[MAXM], tot;
int u[MAXM], v[MAXM], w[MAXM];
int fir[MAXN], sec[MAXN], ans[MAXN]; void addEdge(int U, int V, int W)
{
u[tot] = U;
v[tot] = V;
w[tot] = W;
next[tot] = head[U];
head[U] = tot;
tot ++;
} int dfs1(int x, int fa)
{
fir[x] = sec[x] = ;
for(int e = head[x]; e != -; e = next[e]) if(v[e] != fa)
{
int dis = dfs1(v[e], x) + w[e];
if(dis >= fir[x]) { sec[x] = fir[x]; fir[x] = dis; }
else if(dis > sec[x]) sec[x] = dis;
}
return fir[x];
} void dfs2(int x, int fa, int dis)
{
ans[x] = MAX(fir[x], dis);
for(int e = head[x]; e != -; e = next[e]) if(v[e] != fa)
{
int y = v[e];
if(fir[y] + w[e] == fir[x])
dfs2(y, x, MAX( dis, sec[x]) + w[e] );
else
dfs2(y, x, MAX( dis, fir[x]) + w[e] );
}
} int main()
{ while(~scanf("%d", &N))
{
tot = ;
mem1(head);
int V, W;
for(int i = ; i <= N; i ++)
{
scanf("%d %d", &V, &W);
addEdge(i, V, W);
addEdge(V, i, W);
}
dfs1(, );
dfs2(, , );
for(int i = ; i <= N; i ++ )
printf("%d\n", ans[i]);
}
return ;
}

F:HDU 4539

最原始最薄里的状压DP  题

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e8
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int N, M;
int ma[];
int dp[][][]; int stNum;
int st[], num[]; int judge(int x)
{
if(x & (x<<)) return ;
return ;
} int get1(int x)
{
int ret = ;
while(x)
{
ret += x & ;
x >>= ;
}
return ret;
} void init()
{
mem1(dp);mem0(ma);
stNum = ;
for(int i=;i<(<<M);i++) if(judge(i))
{
st[stNum] = i;
num[stNum++] = get1(i);
}
} int main()
{
//FOPENIN("in.txt");
while(~scanf("%d %d%*c", &N, &M))
{
init();
int x, cur = ;
for(int i=;i<=N;i++)
for(int j=;j<M;j++) {
scanf("%d", &x);
if(x==) ma[i] |= (<<j);
}
for(int i=;i<stNum;i++) {
if(st[i] & ma[]) continue;
dp[cur][i][] = num[i];
}
for(int r = ; r <= N; r ++)
{
cur = !cur;
for(int i = ; i < stNum; i ++)
{
if(st[i] & ma[r]) continue;
for(int j = ; j < stNum; j ++ )
{
if(st[j] & ma[r-]) continue;
int p = (st[i]<<) | (st[i]>>);
if(st[j] & p) continue;
for(int k = ; k < stNum; k ++) if(dp[!cur][j][k] != -)
{
int pp = st[i] | ((st[j]<<) | (st[j]>>));
if(st[k] & pp) continue;
dp[cur][i][j] = MAX(dp[cur][i][j], dp[!cur][j][k] + num[i]);
} }
}
}
int ans = ;
for(int i=;i<stNum;i++) for(int j=;j<stNum;j++)
ans = MAX(ans, dp[cur][i][j]);
printf("%d\n", ans);
}
return ;
} /* 6
3
2
12 */

G:HDU 4035

推公式很重要,是看别人做的。。。。题解

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e8
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int T, N;
vector<int>v[MAXN];
double k[MAXN], e[MAXN];
double A[MAXN], B[MAXN], C[MAXN]; void init()
{
int U, V;
scanf("%d", &N);
for(int i=;i<=N;i++) v[i].clear();
for(int i=;i<N-;i++)
{
scanf("%d %d", &U, &V);
v[U].push_back(V);
v[V].push_back(U);
}
for(int i=;i<=N;i++)
{
scanf("%d %d", &U, &V);
k[i] = (double)U / 100.0;
e[i] = (double)V / 100.0;
}
} bool DFS(int x, int fa)
{
A[x] = k[x];
B[x] = ( - k[x] - e[x]) / v[x].size();
C[x] = - k[x] - e[x];
if(v[x].size() == && x != fa)
return true;
double temp = ;
for(int i = ; i < v[x].size() ; i ++ )
{
int y = v[x][i];
if(y == fa) continue;
if(!DFS(y, x)) return false;
A[x] += A[y] * B[x];
C[x] += C[y] * B[x];
temp += B[y] * B[x];
}
if(fabs(temp - 1.0) < eps) return false;
A[x] = A[x] / ( - temp);
B[x] = B[x] / ( - temp);
C[x] = C[x] / ( - temp);
return true;
} int main()
{
//FOPENIN("in.txt");
scanf("%d", &T);
for(int t = ; t <= T; t ++ )
{
init();
if( DFS(, ) && fabs(A[] - 1.0) > eps )
{
printf("Case %d: %lf\n", t, C[] / ( - A[]));
}
else
{
printf("Case %d: impossible\n", t);
}
}
}

I:HDU 1561

树形DP,注意状态转化的过程  题解

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF ((LL)100000000000000000)
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
int dx[] = {-, , , };
int dy[] = {, -, , }; int N, M;
int G[MAXN][MAXN], vis[MAXN], num[MAXN];
int DP[MAXN][MAXN], D[MAXN]; void DFS(int u)
{
vis[u] = ;
DP[u][] = num[u];
for(int i=;i<=N;i++) if(G[u][i])
{
if(!vis[i]) DFS(i);
for(int j=M;j>;j--)//这里为了保证是先从父节点更新,需要逆序
for(int k=;k<j;k++)//k<j保证父节点不会被更新掉
DP[u][j] = MAX(DP[u][j], DP[u][j-k] + DP[i][k]);
}
} int main()
{
//FOPENIN("in.txt");
while(~scanf("%d %d", &N, &M) &&( N||M))
{
int a;mem0(G);mem0(D);mem0(vis);mem0(DP);
for(int i=;i<=N;i++) {
scanf("%d %d", &a, &num[i]);
if(a != )G[a][i] = ;
else D[i] = ;
}
for(int i=;i<=N;i++) if(!vis[i]){
DFS(i);
if(D[i]) for(int j=M;j>=;j--)//同样需要逆序,可以取到0
{
for(int k=;k<=j;k++)
DP[][j] = MAX(DP[][j], DP[][j-k] + DP[i][k]);
}
}
printf("%d\n", DP[][M]); }
return ;
}

J:HDU 4870

1。高斯消元的做法

2。公式推倒很重要

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF ((LL)100000000000000000)
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-; double t[]; int main()
{
//FOPENIN("in.txt");
double p;
while(~scanf("%lf", &p))
{
t[] = ;
t[] = / p;
t[] = / p + / p / p;
for(int i=;i<=;i++)
{
t[i] = / p * t[i-] + / p - (-p) / p * t[i-];
}
double ans = *t[] + t[] - t[];
printf("%lf\n", ans);
}
return ;
}

K:POJ 1160

我就喜欢暴力求解  题解

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 100000000
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN ( T a, T b ) { return a < b; }
template<class T> T CMP_MAX ( T a, T b ) { return a > b; }
template<class T> T MAX ( T a, T b ) { return a > b ? a : b; }
template<class T> T MIN ( T a, T b ) { return a < b ? a : b; }
template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; }
template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; }
template<class T> T SWAP( T& a, T& b ) { T t = a; a = b; b = t; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-; int V, P;
int x[], DP[][][];
int dis[][], pre[]; void initDis()
{
mem0(dis);mem0(pre);
for(int i=;i<=V;i++)
for(int j=i;j<=V;j++)
for(int k=i;k<=j;k++)
dis[i][j] += MIN(x[k]-x[i], x[j]-x[k]);
for(int i=;i<=V;i++)for(int j=i;j>=;j--)
pre[i] += x[i] - x[j];
} int main()
{
//FOPENIN ( "in.txt" );
//FOPENOUT("out.txt");
while(~scanf("%d %d", &V, &P))
{
for(int i=;i<=V;i++)
scanf("%d", &x[i]);
initDis();
for(int i=;i<;i++)
for(int j=;j<=MIN(i,P);j++)
for(int k=;k<=i;k++){
DP[i][j][k] = INF;
}
int now = ;
for(int i=;i<=V;i++)
{
now = !now;
int s = ;
for(int j=;j<=i;j++) DP[now][][j] = pre[j];
for(int j=;j<=MIN(i, P); j++)
{
DP[now][j][i] = INF;
for(int k=j-;k<i;k++)if(DP[!now][j-][k] != INF)
{
DP[now][j][i] = MIN(DP[now][j][i], DP[now][j-][k] + dis[k][i]);
}
for(int k=j;k<i;k++) DP[now][j][k] = DP[!now][j][k];
}
}
int ans = INF;
for(int k=P;k<=V;k++)if(DP[now][P][k] != INF)
{
int s = ;
for(int j = k + ; j <= V; j ++ ) s += x[j] - x[k];
ans = MIN(ans, DP[now][P][k] + s);
}
printf("%d\n", ans);
}
return ;
}

DP练习(概率,树状,状压)的更多相关文章

  1. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  2. 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)

    [题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...

  3. 【bzoj3672】[Noi2014]购票 斜率优化dp+CDQ分治+树的点分治

    题目描述  给出一棵以1为根的带边权有根树,对于每个根节点以外的点$v$,如果它与其某个祖先$a$的距离$d$不超过$l_v$,则可以花费$p_vd+q_v$的代价从$v$到$a$.问从每个点到1花费 ...

  4. CodeForces 602E【概率DP】【树状数组优化】

    题意:有n个人进行m次比赛,每次比赛有一个排名,最后的排名是把所有排名都加起来然后找到比自己的分数绝对小的人数加一就是最终排名. 给了其中一个人的所有比赛的名次.求这个人最终排名的期望. 思路: 渣渣 ...

  5. luogu4294 [WC2008]游览计划(状压DP/斯坦纳树)

    link 题目大意:给定一个网格图,有些点是关键点,选择格点有代价,求把所有关键点联通的最小代价 斯坦纳树模板题 斯坦纳树问题:给定一个图结构,有一些点是关键点,求把这些关键点联通的最小代价e 斯坦纳 ...

  6. [bzoj4006][JLOI2015]管道连接_斯坦纳树_状压dp

    管道连接 bzoj-4006 JLOI-2015 题目大意:给定一张$n$个节点$m$条边的带边权无向图.并且给定$p$个重要节点,每个重要节点都有一个颜色.求一个边权和最小的边集使得颜色相同的重要节 ...

  7. 【BZOJ2595_洛谷4294】[WC2008]游览计划(斯坦纳树_状压DP)

    上个月写的题qwq--突然想写篇博客 题目: 洛谷4294 分析: 斯坦纳树模板题. 简单来说,斯坦纳树问题就是给定一张有边权(或点权)的无向图,要求选若干条边使图中一些选定的点连通(可以经过其他点) ...

  8. 【ZJOI2017 Round1练习&BZOJ4774】D3T2 road(斯坦纳树,状压DP)

    题意: 对于边带权的无向图 G = (V, E),请选择一些边, 使得1<=i<=d,i号节点和 n − i + 1 号节点可以通过选中的边连通, 最小化选中的所有边的权值和. d< ...

  9. [BZOJ4006][JLOI2015]管道连接 状压dp+斯坦纳树

    4006: [JLOI2015]管道连接 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1020  Solved: 552[Submit][Statu ...

  10. BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)

    题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...

随机推荐

  1. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  2. 51nod1122 机器人走方格 V4

    矩阵快速幂求出每个点走n步后到某个点的方案数.然后暴力枚举即可 #include<cstdio> #include<cstring> #include<cctype> ...

  3. [反汇编练习] 160个CrackMe之022

    [反汇编练习] 160个CrackMe之022. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  4. 【英语】Bingo口语笔记(55) - work系列

  5. 剑指offer-第二章排序之年龄排序

    题目:对某个公司的人的年龄(0-99)进行排序,该公司的总人数为几万人.要求时间复杂度为O(n),可以辅助O(n)的空间. 思路:实现函数为void SortAge(int ages[],int le ...

  6. 【转】bzero, memset ,setmem 区别

    原文网址:http://blog.csdn.net/agathe/article/details/6066157 bzero  原型: extern void bzero(void *s, int n ...

  7. ANDROID模拟器访问本地WEB应用

    一个早上就是想让android的模拟器可以访问到web的应用程序,但是一直是不可以,弄的不知所措. 在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的 ...

  8. Apache二级域名配置方法

    下面这个Apache二级域名配置方法是今天在其它BBS看到的,以前我配置是都是配置每个为一个虚拟目录今天正在想如何写没想到找到了. Apache二级域名实现方法介绍 首先,你的拥有一个有泛域名解析的顶 ...

  9. 使用jQuery Mobile实现新闻浏览器(3)

    在本教程的前两篇文章中,笔者分别向大家介绍了使用jQuery Mobile框架如何去设计手机新闻浏览器,其中实现了一个WEB版本的新闻浏览器,在本教程的最后一篇中,将讲解如何将已实现的web版本的新闻 ...

  10. CSS隐藏元素的几种妙法

    一说起CSS隐藏元素,我想大部分小伙伴们都会想到的第一种方法就是设置display为none.这是最为人所熟知也是最常用的方法.我相信还有不少人想到使用设置visibility为hidden来隐藏元素 ...