poj 1160(四边形不等式)

针对IOI邮局选址问题,采用动态规划并结合四边形不等式优化,实现高效求解最小总距离的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实早就想练DP了,毕竟自己的DP都是XJB乱写的。。然后不小心看到四边形不等式就钻进来瞧瞧。。

这题好像初中就做过,才发现竟然是IOI的题?!

考虑到邮局并没有先后顺序,所以从左往右依次设置邮局,然后得到转移方程如下:

设d[i][j]为使用i个邮局负责前j个村庄的最小费用,w[i][j]为在i到j村庄中设置一个邮局的费用

d[i][j]=min{d[i-1][k]+w[k+1][j]}  (k=i-1..j-1)

感觉直接转移复杂度O(p*v*v)并不会超时吧。。不过不优化一下做这题就没意义了。。

事实上四边形不等式的证明我还不是很懂。。然而这并不影响我们做题。。好像是yjq说过我们可以用普通的DP把决策方案算出来然后打表。。然后决策单调性什么的都是非常显然的了。。

然后就把d[i][j]对应的策略s[i][j]给打了出来。。

0 0 0 0 0 0 0 0 0 0
0 1 1 3 3 3 3 7 8 8
0 0 2 3 3 5 5 7 8 8
0 0 0 3 3 5 6 7 8 8
0 0 0 0 4 5 6 7 8 9

发现i确定时,k随着j递增。。因此可以把复杂度降成O(p*v)

对w可以前缀和+差分预处理一下。。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1e9
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 305
#define pi 3.141592653
using namespace std;

int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f*x;
}




int n,m,a[NM],b[NM],w[NM][NM],d[NM][NM];
int main(){
	//freopen("data.in","r",stdin);
	n=read();m=read();
	inc(i,1,n)a[i]=read();
	inc(i,1,n)b[i]=b[i-1]+a[i];
	inc(i,1,n)inc(j,i,n)w[i][j]=b[j]-b[(i+j)/2]-b[(i+j-1)/2]+b[i-1];
	inc(j,1,n)d[1][j]=w[1][j];
	inc(i,2,m){
		int t=i-1;
		inc(j,i,n){
			d[i][j]=inf;
			inc(k,t,j-1)
				if(d[i-1][k]+w[k+1][j]<d[i][j])d[i][j]=d[i-1][k]+w[k+1][j],t=k;
		}
	}
	//inc(i,1,m){inc(j,1,n)printf("%d ",s[i][j]);putchar('\n');}
	printf("%d\n",d[m][n]);
	return 0;
}

Post Office
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20394 Accepted: 11013

Description

There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.

You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.

Input

Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

Output

The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

Sample Input

10 5
1 2 3 6 7 9 11 22 44 50

Sample Output

9

Source

[Submit]   [Go Back]   [Status]   [Discuss]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值