The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences join together at a given endpoint. The result is a web of fences enclosing his pastures. Farmer Brown wants to start to straighten things out. In particular, he wants to know which of the pastures has the smallest perimeter.
Farmer Brown has numbered his fence segments from 1 to N (N = the total number of segments). He knows the following about each fence segment:
Given a list of fence segments that represents a set of surrounded pastures, write a program to compute the smallest perimeter of any pasture. As an example, consider a pasture arrangement, with fences numbered 1 to 10 that looks like this one (the numbers are fence ID numbers):
1 +---------------+ |\ /| 2| \7 / | | \ / | +---+ / |6 | 8 \ /10 | 3| \9 / | | \ / | +-------+-------+ 4 5
The pasture with the smallest perimeter is the one that is enclosed by fence segments 2, 7, and 8.
Line 1: | N (1 <= N <= 100) |
Line 2..3*N+1: | N sets of three line records:
|
10 1 16 2 2 2 7 10 6 2 3 2 2 1 7 8 3 3 3 2 1 8 2 4 4 8 1 3 3 9 10 5 5 8 3 1 9 10 4 6 6 6 1 2 5 1 10 7 5 2 2 1 2 8 9 8 4 2 2 2 3 7 9 9 5 2 3 7 8 4 5 10 10 10 2 3 1 6 4 9 5
12
题意:给你一张图,求其中封闭图形的最小边长,即求图的最小环
分析:求最小环当然想到floyd来做,可是具体怎么实现呢。。。说实在的,我早忘了,自己写了三四遍都错 T_T,百度个模板才A掉,又退化了啊、、、
当然这题的构图比较特殊,我用来个标记两边之间的节点来做,具体看makenode()。。。
代码:
/* ID: 15114582 PROG: fence6 LANG: C++ */ #include<cstdio> #include<iostream> #include<cstring> using namespace std; const int mm=111; int id[mm][mm],g[mm][mm],d[mm][mm],v[mm]; int i,j,k,n,m,s,len,s1,s2,ans; int makenode(int n,int u) { int i,now=0; for(i=0;i<n;++i) { scanf("%d",&v[i]); if(id[u][v[i]])now=id[u][v[i]]; } if(!now)now=++m; for(i=0;i<n;++i)id[u][v[i]]=id[v[i]][u]=now; return now; } int main() { freopen("fence6.in","r",stdin); freopen("fence6.out","w",stdout); while(~scanf("%d",&n)) { memset(id,0,sizeof(id)); memset(d,10,sizeof(d)); memset(g,10,sizeof(g)); m=0; while(n--) { scanf("%d%d%d%d",&s,&len,&s1,&s2); s1=makenode(s1,s); s2=makenode(s2,s); g[s1][s2]=g[s2][s1]=d[s1][s2]=d[s2][s1]=len; } ans=1e9; for(k=1;k<=m;++k) { for(i=1;i<k;++i) for(j=i+1;j<k;++j) ans=min(ans,d[i][j]+g[i][k]+g[j][k]); for(i=1;i<=m;++i) for(j=1;j<=m;++j) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } printf("%d\n",ans); } return 0; }