POJ2137--Cowties--DP

http://poj.org/problem?id=2137

N cows (3 <= N <= 100) are eating grass in the middle of a field. So that they don't get lost, Farmer John wants to tie them together in a loop so that cow i is attached to cows i-1 and i+1. Note that cow N will be tied to cow 1 to complete the loop. 

Each cow has a number of grazing spots she likes and will only be happy if she ends up situated at one of these spots. Given that Farmer John must ensure the happiness of his cows when placing them, compute the shortest length of rope he needs to tie them all in a loop. It is possible for different parts of the loop to cross each other. 

Input

* Line 1: The integer N. 

* Lines 2..N+1: Each line describes one cow using several space-separated integers. The first integer is the number of locations S (1 <= S <= 40) which are preferred by that cow. This is followed by 2*S integers giving the (x,y) coordinates of these locations respectively. The coordinates lie in the range -100..100. 

Output

A single line containing a single integer, 100 times the minimum length of rope needed (do not perform special rounding for this result). 

Sample Input

4
1 0 0
2 1 0 2 0
3 -1 -1 1 1 2 2
2 0 1 0 2

Sample Output

400

Hint

[Cow 1 is located at (0,0); cow 2 at (1,0); cow 3 at (1,1); and cow 4 at (0,1).] 

从第一个奶农开始枚举它的每一个点。

DPij代表第i个奶牛在第J个地点绳子的最小值。预处理出前两个的值

然后第k个由第k-1个推理出来,转移方程是dp[k][l]=min(dp[k][l],dp[k-1][j]+tmp);

# include <iostream>
# include <cstdio>
# include <cstring>
# include<cfloat>
# include <cstdlib>
# include <cmath>
using namespace std;
int n, tot;//-------
double dp[103][45];
struct node
{
    int cnt;
    int x[43], y[43];//
} a[103];
double dist(int x0, int y0, int x1, int y1)
{
    double s = sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
    return s;
}
int main()
{
    while(~scanf("%d",&n))
    {
        //double ans = DBL_MAX;
        double ans=DBL_MAX;
        for(int i=0; i<103; ++i)
            for(int j=0; j<43; ++j)
                dp[i][j] = DBL_MAX;//初始化最大值,第I只牛在第J个点绳子长最小值
        for(int i=0; i<103; i++)
        {
            for(int j=0; j<43; j++)
            {
                dp[i][j]=DBL_MAX;
            }
        }
        /*for(int i=0; i<n; ++i)
        {
            scanf("%d",&a[i].cnt);//每个区域点数量
            for(int j=0; j<a[i].cnt; ++j)
                scanf("%d%d",&(a[i].x[j]),&(a[i].y[j]));
        }*/
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i].cnt);
            for(int j=0;j<a[i].cnt;j++)
            {
                scanf("%d%d",&(a[i].x[j]),&(a[i].y[j]));
            }
        }
       for(int i=0;i<a[0].cnt;++i)
        {
            for(int j=0;j<a[1].cnt;++j)
            {
                double tmp=dist(a[0].x[i],a[0].y[i],a[1].x[j],a[1].y[j]);
                dp[1][j]=tmp;
            }
            for(int j=2;j<n;j++)
            {
                for(int k=0;k<43;k++)
                {
                    dp[j][k]=DBL_MAX;
                }
            }
            for(int k=2;k<n;k++)
            {
                for(int l=0;l<a[k].cnt;l++)
                {
                    for(int j=0;j<a[k-1].cnt;j++)
                    {
                        double tmp=dist(a[k].x[l],a[k].y[l],a[k-1].x[j],a[k-1].y[j]);
                        dp[k][l]=min(dp[k][l],dp[k-1][j]+tmp);
                    }
                }
            }
            for(int j=0;j<a[n-1].cnt;j++)
            {
                double tmp=dist(a[n-1].x[j],a[n-1].y[j],a[0].x[i],a[0].y[i]);
                dp[0][i]=min(dp[0][i],dp[n-1][j]+tmp);
            }
            ans=min(ans,dp[0][i]);
        }
       /*for(int i=0; i<a[0].cnt; ++i)
        {
            //从第0个点的一个个坐标开始
            for(int j=0; j<a[1].cnt; ++j)
            {
                double tmp = dist(a[0].x[i], a[0].y[i], a[1].x[j], a[1].y[j]);
                dp[1][j] = tmp;//处理处第0个牛和第一个牛之间距离。
            }//第一行的点个数
            for(int j=2; j<n; ++j)
                for(int k=0; k<43; ++k)
                    dp[j][k] = DBL_MAX;//默认第2..牛在第J个点的绳子长度
            for(int k=2; k<n; ++k)
                for(int l=0; l<a[k].cnt; ++l)
                    for(int p=0; p<a[k-1].cnt; ++p)
                    {
                        double tp = dist(a[k].x[l], a[k].y[l], a[k-1].x[p], a[k-1].y[p]);
                        dp[k][l] = min(dp[k][l], dp[k-1][p]+tp);
                    }
            //这里处理出了第K个和第K-1个之间的最小值
            //第K个是从第K-1个推出来的
            //接着是链成环
            for(int k=0; k<a[n-1].cnt; ++k)
            {
                double tp = dist(a[n-1].x[k], a[n-1].y[k], a[0].x[i], a[0].y[i]);
                dp[0][i] = min(dp[0][i], dp[n-1][k]+tp);
            }
            ans = min(ans, dp[0][i]);//---
        }*/
        printf("%d\n",(int)(ans*100));
        //
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值