Poj Solution 2137

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

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int map[2][50][202];
int len[202];
int N;
int S;
double calc(double x1,double y1,double x2,double y2)
{
    return sqrt((x1 - x2) * (x1 - x2)  +  (y1 - y2) * (y1 - y2));
}
double value[1000][1000];
bool visited[1000][1000];
double search(int i,int j)
{
    double min = 99999999999;
    double sum;
    if (j == N)
    {
        for (int z = 1;z <= len[1];z++)
        {
            double sum2 = calc(map[0][S][1],map[1][S][1],map[0][i][j],map[1][i][j]);

            if (sum2 < min)
                min = sum2;
        }
        return min;
    }
    for (int z = 1;z <= len[j + 1];z++)
    {
        visited[i][j] = true;
        double r = calc(map[0][i][j],map[1][i][j],map[0][z][j + 1],map[1][z][j + 1]);
        if (!visited[z][j + 1])
        {
            sum = search(z,j + 1);
        }
        else
        {
            sum = value[z][j + 1];
        }
        if (sum + r < min)
            min = sum + r;
    }
    value[i][j] = min;
    return min;
}
int main()
{
    while (cin>>N)
    {
        for (int i = 1;i <= N;i++)
        {
            cin>>len[i];
            for (int j = 1;j <= len[i];j++)
            {
                cin>>map[0][j][i]>>map[1][j][i];
            }
        }


        double min = 99999999999;
        for (int i = 1;i <= len[1];i++)
        {
            for (int i = 0;i < 50;i++)
            {
                for (int j = 0;j < 200;j++)
                {
                    value[i][j] = 0;
                    visited[i][j] = false;
                }
            }
            S = i;
            double v = search(i,1);
            if (v < min)
                min = v;
        }
        int res = min * 100;
        printf("%dn",res);
    }
    return 0;
}


/*
Cowties
Time Limit: 1000MS  Memory Limit: 65536K 
Total Submissions: 1514  Accepted: 468 

Description

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).] 
*/
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *