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

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

Source

题解:
题意理解了好大一会儿。。。
并查集找出所有多边形,然后乱搞+暴力就行了
代码:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 500+100
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
29     return x*f;
30 }
31 int n,tot,fa[maxn],p[maxn],f[maxn][maxn],a[maxn];
32 bool v[maxn];
33 inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
34 int main()
35 {
36     freopen("input.txt","r",stdin);
37     freopen("output.txt","w",stdout);
38     n=read();
39     for1(i,n)fa[i]=i;
40     for1(i,n)
41      {
42          int x=find(read()),y=find(read());
43          if(x!=y)fa[x]=y;
44      }
45     for1(i,n)
46      {
47       p[i]=find(i);
48       if(v[p[i]])continue;
49       v[p[i]]=1;
50       a[++tot]=p[i];
51      }
52     memset(f,60,sizeof(f));
53     for1(i,n) 
54      for1(j,n)
55       f[p[i]][p[j]]=min(f[p[i]][p[j]],read());
56     int ans=inf;
57     for1(i,tot)
58      {
59          int t=0;
60          for1(j,tot) if(i!=j)t+=f[a[i]][a[j]];
61          if(t<ans)ans=t;
62      }
63     printf("%d
",ans<<1);     
64     return 0;
65 }
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/4005750.html