POJ3612Telephone Wire[DP]

本文探讨了如何通过调整电线杆的高度来最小化电话线布线成本的问题。采用动态规划方法,实现了一种高效的解决方案,并提供了完整的代码示例。
Telephone Wire
Time Limit: 1000MS      Memory Limit: 65536K
Total Submissions: 3187     Accepted: 1113
Description

Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize N (2 ≤ N ≤ 100,000) already-installed telephone poles, each with some heighti meters (1 ≤ heighti ≤ 100). The new wire will connect the tops of each pair of adjacent poles and will incur a penalty cost C × the two poles' height difference for each section of wire where the poles are of different heights (1 ≤ C ≤ 100). The poles, of course, are in a certain sequence and can not be moved.

Farmer John figures that if he makes some poles taller he can reduce his penalties, though with some other additional cost. He can add an integer X number of meters to a pole at a cost of X2.

Help Farmer John determine the cheapest combination of growing pole heights and connecting wire so that the cows can get their new and improved service.

Input

* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains a single integer: heighti

Output

* Line 1: The minimum total amount of money that it will cost Farmer John to attach the new telephone wire.

Sample Input

5 2
2
3
5
1
4
Sample Output

15
Source

USACO 2007 November Gold

题意如下:

就是一道简单的DP
先贴代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<time.h>
#include<stack>
#include<set>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
#define fr(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define fd(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define ln putchar('\n')
#define mem(x,y) memset(x,y,sizeof(x));
#define gc getchar()
#define pc(x) putchat(x)
#define uns unsigned
#define sqr(x) (x)*(x)
#define st struct
#define vc vector
#define f1r(i,j) fr(i,1,j)
#define f11r(i,j,x,y) f1r(i,x) f1r(j,y)
#define frr(i,j,a,b,c,d) fr(i,a,b) fr(j,c,d)
#define f1d(i,j) fd(i,j,1)
#define f11d(i,j,x,y) f1d(i,x) f1d(j,y)
#define fdd(i,j,a,b,c,d) fd(i,a,b) fd(j,c,d)
#define inf 0x3f3f3f3f
#define ct continue
#define rt return
#define llf double
#define ll long long
#define frei(s) freopen(s,"r",stdin)
#define freo(s) freopen(s,"w",stdout)
#define mes(x,y) memset(x,y,sizeof(x))
#define mec(x,y) memcpy(x,y,sizeof(y))
int n,c,ans,a[100010],l,t[110],d[110],f[110];
int main(){
    OPENFILE();
    scanf("%d%d",&n,&c);
    f1r(i,n)
        scanf("%d",&a[i]);
    mes(d,inf);
    fr(i,a[1],100)
        d[i]=sqr(i-a[1]);
    fr(i,2,n){
        fr(j,0,100){
            f[j]=d[j];
            d[j]=t[j]=inf;
        }
        l=f[0];
        t[100]=f[100]+100*c;
        fd(j,99,0)
            t[j]=min(t[j+1],f[j]+j*c);
        f1r(j,a[i]-1)
            l=min(l,f[j]-j*c);
        fr(j,a[i],100)
            d[j]=min(t[j]-j*c,l=min(l,f[j]-j*c)+j*c)+sqr(j-a[i]);
    }
    ans=inf;
    fr(i,a[n],100)
        ans=min(d[i],ans);
    printf("%d\n",ans);
    rt 0;
}

其中d[i]是当前这个电线杆高度是i时的最小花费。
t[i]、l分类讨论d的情况

可以得知
t[j]=min(t[j+1],f[j]+jc)(j[0,99],t[100]=f[100]+100c)
l[j]=min(l[j1],f[j]jc)(j[1,100],l[0]=f[0])
d[j]=min(t[j]jc,l[j])+(ja[i])2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值