Bzoj3397: [usaco 2009 Feb] surround the islands ring island fence

Source: Internet
Author: User
3397: [usaco 2009 Feb] surround the islands fence time limit: 3 sec memory limit: 128 MB
Submit: 11 solved: 7
[Submit] [Status] Description John buys real estate in the Caribbean and prepares to raise cows on several islands. so he wants to fence all the islands. each island is a polygon. he walks in one direction along a boundary of the island and sometimes ships to another island. he can repair the fence from any polygon vertex. At any vertex that arrives, he can take a boat to a certain vertex of another island, but after repairing the fence of that island, he must return the island vertices from the original path immediately. there is a broken line between any two vertices, and each route requires a certain amount of fee. please help John calculate the minimum cost and let him finish all the fences. input N (3 ≤ n ≤ 1st) in the input 500 line, indicating the number of all island polygon. in the next n rows, enter two integers V1 and V2 (1 ≤ V1 ≤ n; 1 ≤ V2 ≤ n) to represent the two endpoints of a line segment that constitutes an island. next, enter a matrix of N rows and n columns, which indicates the cost of sailing between two vertices. output is an integer with the minimum cost. sample input12
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 8 8 9
9 12 0 25 20 18 16 14 13 7 12 12
20 12 25 0 8 13 14 15 10 10 10
25 10 20 8 0 16 20 18 17 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 output30hint

 

 

Source

Silver

Question: I understood the question for a while... And check the set to find all the polygon, and then mess with the + brute force code:
 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 100000000013 #define maxn 500+10014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #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 100000000723 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\n",ans<<1);     64     return 0;65 }
View code

 

Bzoj3397: [usaco 2009 Feb] surround the islands ring island fence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.