Cowties
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3262 | Accepted: 1125 |
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.
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.
* 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).]
Source
题意:N只牛,每只牛有若干个喜爱坐标,现在要将牛按顺序用绳子连起来,最后一只连第一只,问怎样安排坐标使得绳子总长最小。
思路:dp[i][j]表示第i只牛在第j个坐标的最短绳长,因为绳子成环,所以枚举第一只牛的坐标作为起始点即可。
# 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;
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<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 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);
}
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;
}