http://poj.org/problem?id=3375 (Topic link)
Test instructions
There are $m$ network interface and $n$ computer, give their coordinates (in the same line), an interface can only be connected to a computer, the cost is the absolute value of the difference of two coordinates, ask the minimum cost is how much.
Solution
$f [i][j]$ represents the minimum cost of the former $i$ computer connected to the first $j$ network interface. Transfer: $ $f [I][j]=min (f[i][j-1],f[i-1][j-1]+| x[i]-x[j]|) $$
Considering the scope of the $m$ is very large, there must be a lot of useless, we find the closest interface to $i$ $pos$, from $n-pos$ for to $n+pos$.
Details
LL
Code
//poj3375#include<algorithm> #include <iostream> #include < cstdlib> #include <cstring> #include <cstdio> #include <cmath> #define LL long long#define inf (1ll <<60) #define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int Maxn=200010;int t1[maxn],t2[maxn],pos[maxn],n,m; LL F[2][maxn];int Main () {scanf ("%d%d", &m,&n), for (Int. i=1;i<=m;i++) scanf ("%d", &t2[i]); for (int i=1;i <=n;i++) scanf ("%d", &t1[i]); sort (t1+1,t1+n+1); sort (t2+1,t2+m+1); for (int i=1;i<=n;i++) Pos[i]=lower_ Bound (T2+1,t2+1+m,t1[i])-t2;memset (f,0x7f,sizeof (f)); for (int i=0;i<=m;i++) F[0][i]=0;int p=0,q=1,l,r,u=0,v=m; for (int i=1;i<=n;i++) {L=max (1,pos[i]-n), R=min (m,pos[i]+n);p ^=1,q^=1;for (int j=l;j<=r;j++) f[p][j]=min (f[p][ J-1],f[q][min (j-1,v)]+abs (T1[i]-t2[j]); for (int j=u;j<=v;j++) f[q][j]=inf;u=l,v=r;} printf ("%lld", F[p][r]); return 0;}
"Poj3375" Network Connection