[Usaco2009 Feb] Surround The Islands Circle fence

Source: Internet
Author: User

Description John buys a real estate in the Caribbean and is preparing to raise cows on several islands here. So he's going to fence all the islands. Each island is a polygon. He walked in one direction along a border of the island, sometimes by boat to another island. He can start the work of the fence from any one of the polygon vertices, and at any of the vertices he arrives, he can take a boat to a vertex of another island, but after finishing the fence on that island, he must return to the apex of the departing island at once. Any two vertices have a clean line, each route will require a certain fee. Please help John to calculate the minimum cost so that he can finish all the fences.    Input line 1th enters N (3≤n≤500), which represents the number of all island polygons. The next N rows enter two integers V1 and V2 (1≤v1≤n;1≤v2≤n) for each line, representing two endpoints of a segment that makes up the island. Next, enter a matrix of n rows n columns representing the cost of sailing between the two vertices. Output an integer with a minimum cost. Sample Input 12
1 7
9}
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 Ten 9 0Sample Output -HINT

Test instructions difficult to understand is probably the root cause of the problem through a small number of people ...

And check the set directly to find the number of polygons, and then run directly out of the path it is OK

After writing the code to change the compilation error immediately after the sample ... 233 ... And then he handed it in right away.

Only 20 people A is actually 1 a.

#include <cstdio>#include<algorithm>using namespacestd;intn,m,f[501][501],fa[501],i,j,a,x,y,g[501],num=0, ans=1e8,an;CharC;intRead () {a=0; C=GetChar ();  while(c<'0'|| C>'9') c=GetChar ();  while(c>='0'&&c<='9') a=a*Ten+c- -, c=GetChar (); returnA;}intGfintx) {    if(X==fa[x])returnXElse{fa[x]=GF (fa[x]); returnFa[x]; }}intMain () {scanf ("%d",&N);  for(i=1; i<=n;i++) fa[i]=i;  for(i=1; i<=n;i++) {x=GF (read ()); y=GF (Read ()); if(x!=y) fa[x]=y; }     for(i=1; i<=n;i++)if(I==GF (i)) g[i]=++num;  for(i=1; i<=n;i++) g[i]=G[GF (i)];  for(i=1; i<=n;i++)     for(j=1; j<=n;j++) f[i][j]=100000;  for(i=1; i<=n;i++)     for(j=1; j<=n;j++) F[g[i]][g[j]]=min (F[g[i]][g[j]],read ());  for(i=1; i<=num;i++) { an=0;  for(j=1; j<=num;j++) an+=F[i][j]; if(An<ans) ans=an ; } printf ("%d\n",ans<<1);}

[Usaco2009 Feb] Surround The Islands Circle 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.