DP练习(概率,树状,状压)
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练习(概率,树状,状压)的更多相关文章
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)
[题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...
- 【bzoj3672】[Noi2014]购票 斜率优化dp+CDQ分治+树的点分治
题目描述 给出一棵以1为根的带边权有根树,对于每个根节点以外的点$v$,如果它与其某个祖先$a$的距离$d$不超过$l_v$,则可以花费$p_vd+q_v$的代价从$v$到$a$.问从每个点到1花费 ...
- CodeForces 602E【概率DP】【树状数组优化】
题意:有n个人进行m次比赛,每次比赛有一个排名,最后的排名是把所有排名都加起来然后找到比自己的分数绝对小的人数加一就是最终排名. 给了其中一个人的所有比赛的名次.求这个人最终排名的期望. 思路: 渣渣 ...
- luogu4294 [WC2008]游览计划(状压DP/斯坦纳树)
link 题目大意:给定一个网格图,有些点是关键点,选择格点有代价,求把所有关键点联通的最小代价 斯坦纳树模板题 斯坦纳树问题:给定一个图结构,有一些点是关键点,求把这些关键点联通的最小代价e 斯坦纳 ...
- [bzoj4006][JLOI2015]管道连接_斯坦纳树_状压dp
管道连接 bzoj-4006 JLOI-2015 题目大意:给定一张$n$个节点$m$条边的带边权无向图.并且给定$p$个重要节点,每个重要节点都有一个颜色.求一个边权和最小的边集使得颜色相同的重要节 ...
- 【BZOJ2595_洛谷4294】[WC2008]游览计划(斯坦纳树_状压DP)
上个月写的题qwq--突然想写篇博客 题目: 洛谷4294 分析: 斯坦纳树模板题. 简单来说,斯坦纳树问题就是给定一张有边权(或点权)的无向图,要求选若干条边使图中一些选定的点连通(可以经过其他点) ...
- 【ZJOI2017 Round1练习&BZOJ4774】D3T2 road(斯坦纳树,状压DP)
题意: 对于边带权的无向图 G = (V, E),请选择一些边, 使得1<=i<=d,i号节点和 n − i + 1 号节点可以通过选中的边连通, 最小化选中的所有边的权值和. d< ...
- [BZOJ4006][JLOI2015]管道连接 状压dp+斯坦纳树
4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1020 Solved: 552[Submit][Statu ...
- BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)
题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...
随机推荐
- UploadifyAPI-上传插件属性和方法介绍
上一篇文章简单的介绍了Uploadify上传插件的使用.但是对于常用的属性和方法并没有说明.授人以鱼不如授人以渔,我决定将常用的属性列举出来,供大伙参考参考. Uploadify属 ...
- 在 VC6 中使用 GdiPlus-使用
下面用 VC6 来写一个 GdiPlus 的 Demo 工程 Step1:新建一个名为 Demo_GdiPlus 的 MFC AppWizard(exe) 工程 操作步骤:(1)主菜单File-> ...
- 10个实用的PHP正则表达式 (转)
http://www.iteye.com/news/23231 1. 验证E-mail地址 这是一个用于验证电子邮件的正则表达式.但它并不是高效.完美的解决方案.在此不推荐使用. $email = & ...
- activiti学习资料(架构描述)
Activiti学习资料 Activiti是业界很流行的java工作流引擎,关于Activiti与JBPM5的关系和如何选择不是本文要讨论的话题,相关内容可以baidu一下.Activiti从架构角度 ...
- (六) 6.1 Neurons Networks Representation
面对复杂的非线性可分的样本是,使用浅层分类器如Logistic等需要对样本进行复杂的映射,使得样本在映射后的空间是线性可分的,但在原始空间,分类边界可能是复杂的曲线.比如下图的样本只是在2维情形下的示 ...
- 解决VS2013/VS2010简体中文版无法使用ReSharper快捷键的问题
对于简体中文版VS2013,若应用ReSharper快捷键失效,按下面方法修复: 1) 首先在ReSharper设置里,应用你的快捷键设置 2) 关闭VS2013,然后用记事本打开“%LOCALAPP ...
- 查看造成等待事件的具体SQL语句
先查看存在的等待事件:col event for a40col WAIT_CLASS format a20select sid,WAIT_CLASS,event,p1,p2,p3,WAIT_TIME, ...
- hdu 2476(第一道区间dp)
题意:就是给定两个字符串,第一个是初始串,第二个是目标串,问你把初始串变到目标串最少需要多少串! 分析:此题分两步,第一步是假设开始的初始串是空串,然后就进行区间dp,dp[i][j]代表把区间[i, ...
- 改进duilib的richedit控件的部分功能
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 如果要使用透明异形窗体功能,首先要改进duilib库让他本 ...
- js保留小数点后N位的方法介绍
js保留小数点后N位的方法介绍 利用toFixed函数 代码如下 复制代码 <script language="javascript"> document.write( ...