BZOJ3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆

时间:2023-03-08 19:37:25

3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 11  Solved: 7
[Submit][Status]

Description

    约翰在加勒比海买下地产,准备在这里的若干个岛屿上养奶牛.所以,他要给所有岛屿围上篱笆.每个岛屿都是多边形.他沿着岛屿的一条边界朝一个方向走,有时候坐船到另一个岛去.他可以从任意一个多边形顶点开始修篱笆的工作;在任意一个到达的顶点,他可以坐船去另一个岛屿的某个顶点,但修完那个岛的篱笆,他必须马上原路返回这个出发的岛屿顶点.任意两个顶点间都有肮线,每条航线都需要一定的费用.请帮约翰计算最少的费用,让他修完所有篱笆.

Input

    第1行输入N(3≤N≤500),表示所有岛屿多边形的个数.
    接下来N行每行输入两个整数V1和V2(1≤V1≤N;1≤V2≤N),表示一条构成岛屿的线段的两个端点.接下来输入N行N列的矩阵,表示两个顶点间航行所需费用.

Output

    一个整数,最少费用.

Sample Input

12
1 7
7 3
3 6
6 10
10 1
2 12
2 9
8 9
8 12
11 5
5 4
11 4
0 15 9 20 25 8 10 13 17 8 8 7
15 0 12 12 10 10 8 15 15 8 8 9
9 12 0 25 20 18 16 14 13 7 12 12
20 12 25 0 8 13 14 15 15 10 10 10
25 10 20 8 0 16 20 18 17 18 9 11
8 10 18 13 16 0 10 9 11 10 8 12
10 8 16 14 20 10 0 18 20 6 16 15
13 15 14 15 18 9 18 0 5 12 12 13
17 15 13 15 17 11 20 5 0 22 8 10
8 8 7 10 18 10 6 12 22 0 11 12
8 8 12 10 9 8 16 12 8 11 0 9
7 9 12 10 11 12 15 13 10 12 9 0

Sample Output

30

HINT

BZOJ3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆

Source

题解:
题意理解了好大一会儿。。。
并查集找出所有多边形,然后乱搞+暴力就行了
代码:
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 500+100
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,tot,fa[maxn],p[maxn],f[maxn][maxn],a[maxn];
bool v[maxn];
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)fa[i]=i;
for1(i,n)
{
int x=find(read()),y=find(read());
if(x!=y)fa[x]=y;
}
for1(i,n)
{
p[i]=find(i);
if(v[p[i]])continue;
v[p[i]]=;
a[++tot]=p[i];
}
memset(f,,sizeof(f));
for1(i,n)
for1(j,n)
f[p[i]][p[j]]=min(f[p[i]][p[j]],read());
int ans=inf;
for1(i,tot)
{
int t=;
for1(j,tot) if(i!=j)t+=f[a[i]][a[j]];
if(t<ans)ans=t;
}
printf("%d\n",ans<<);
return ;
}