问题
https://vjudge.net/problem/UVA-1169
分析
d[i][j]表示在点i,载重为j时,走到最后的累计cost。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=100000+5,Inf=0x3f3f3f3f;
struct Point{
int x,y,w;
}point[maxn];
int dist2origin[maxn],dist[maxn];
int c,n,kase,dp[maxn][105];
int DP(int u,int cost){
int &temp=dp[u][cost];
if(temp!=0) return temp;
if(u==n) return temp=dist2origin[u];
int t=DP(u+1,point[u+1].w)+dist2origin[u]+dist2origin[u+1]; //返回原点,再到下一个
if(cost+point[u+1].w<=c){
t=min(t,DP(u+1,cost+point[u+1].w)+dist[u+1]);
}
return temp=t;
}
int main(void){
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&c,&n);
for(int i=0;i<=n;++i){
for(int j=0;j<=c;++j) dp[i][j]=0;
}
point[0].x=point[0].y=point[0].w=0;
for(int i=1;i<=n;++i){
scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].w);
dist2origin[i]=abs(point[i].x+point[i].y);
dist[i]=abs(point[i].x-point[i-1].x)+abs(point[i].y-point[i-1].y);
}
int ans=DP(0,0);
printf("%d\n",ans);
if(kase>0) printf("\n");
}
}
蓝书上的使用单调队列优化的解法,时间复杂度O(n)。
d[i]表示从原点出发,将前i个垃圾清理完并放入到垃圾桶内的最小代价。
感觉有点复杂。
d
[
i
]
=
m
i
n
{
d
[
j
]
+
d
i
s
t
2
o
r
i
g
i
n
(
j
+
1
)
+
d
i
s
t
(
j
+
1
,
i
)
+
d
i
s
t
2
o
r
i
g
i
n
(
i
)
∣
j
<
=
i
,
w
(
j
+
1
,
i
)
<
=
c
}
d[i]=min\{d[j]+dist2origin(j+1)+dist(j+1,i)+dist2origin(i)|j<=i,w(j+1,i)<=c\}
d[i]=min{d[j]+dist2origin(j+1)+dist(j+1,i)+dist2origin(i)∣j<=i,w(j+1,i)<=c}
其中,dist(j+1,i)表示从第j+1个垃圾出发,依次经过垃圾j+2,j+3,最终到达垃圾i的总距离,dist2origin(i)表示垃圾i到(0,0)的距离
total_dist(i)表示从垃圾1开始,经过2,3,最终到达i的总距离。dist(j+1,i)=total_dist(i)-total_dist(j+1).
所以
d
[
i
]
=
m
i
n
{
d
(
j
)
−
t
o
t
a
l
d
i
s
t
(
j
+
1
)
+
d
i
s
t
2
o
r
i
g
i
n
(
j
+
1
)
∣
w
(
j
+
1
,
i
)
<
=
c
}
+
t
o
t
a
l
d
i
s
t
(
i
)
+
d
i
s
2
o
r
i
g
i
n
(
i
)
d[i]=min\{d(j)-total_dist(j+1)+dist2origin(j+1)|w(j+1,i)<=c\}+total_dist(i)+dis2origin(i)
d[i]=min{d(j)−totaldist(j+1)+dist2origin(j+1)∣w(j+1,i)<=c}+totaldist(i)+dis2origin(i)
令
f
u
n
c
(
j
)
=
d
(
j
)
−
t
o
t
a
l
d
i
s
t
(
j
+
1
)
+
d
i
s
t
2
o
r
i
g
i
n
(
j
+
1
)
func(j)=d(j)-total_dist(j+1)+dist2origin(j+1)
func(j)=d(j)−totaldist(j+1)+dist2origin(j+1),上式可以简化为
d
(
i
)
=
m
i
n
{
f
u
n
c
(
j
)
∣
w
(
j
+
1
,
i
)
<
=
c
}
+
t
o
t
a
l
d
i
s
t
(
i
)
+
d
i
s
t
2
o
r
i
g
i
n
(
i
)
d(i)=min\{func(j)|w(j+1,i)<=c\}+total_dist(i)+dist2origin(i)
d(i)=min{func(j)∣w(j+1,i)<=c}+totaldist(i)+dist2origin(i)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
struct Point{
int x,y,w;
}point[maxn];
int total_dist[maxn],total_weight[maxn],dist2origin[maxn],f[maxn];
int q[maxn],front,rear,d[maxn],c,n,kase;
int func(int i){
return d[i]-total_dist[i+1]+dist2origin[i+1];
}
int main(void){
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&c,&n);
total_dist[0]=total_weight[0]=d[0]=0;
point[0].x=point[0].y=point[0].w=0;
for(int i=1;i<=n;++i){
scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].w);
dist2origin[i]=abs(point[i].x+point[i].y);
total_dist[i]=total_dist[i-1]+abs(point[i].x-point[i-1].x)+abs(point[i].y-point[i-1].y);
total_weight[i]=total_weight[i-1]+point[i].w;
}
int ans=0,j=0;
front=rear=0;
//j<i
for(int i=1;i<=n;++i){
f[i-1]=func(i-1);
d[i]=0;
while(rear>front && f[q[rear-1]]>=f[i-1]){
--rear;
}
q[rear++]=i-1;
while(rear>front && total_weight[i]-total_weight[q[front]]>c){
++front;
}
d[i]=f[q[front]]+total_dist[i]+dist2origin[i];
}
printf("%d\n",d[n]);
if(kase>0) printf("\n");
}
}