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:
- the length of the segment
- the segments which connect to it at one end
- the segments which connect to it at the other end.
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.
PROGRAM NAME: fence6
INPUT FORMAT
Line 1: | N (1 <= N <= 100) |
Line 2..3*N+1: | N sets of three line records:
|
SAMPLE INPUT (file fence6.in)
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
OUTPUT FORMAT
The output file should contain a single line with a single integer that represents the shortest surrounded perimeter.SAMPLE OUTPUT (file fence6.out)
12
思路:点边互化,然后用floy判最小环,注意判环要有不同的点元素在。
Executing... Test 1: TEST OK [0.000 secs, 3852 KB] Test 2: TEST OK [0.000 secs, 3852 KB] Test 3: TEST OK [0.000 secs, 3852 KB] Test 4: TEST OK [0.000 secs, 3852 KB] Test 5: TEST OK [0.000 secs, 3852 KB] Test 6: TEST OK [0.000 secs, 3852 KB] Test 7: TEST OK [0.000 secs, 3852 KB] Test 8: TEST OK [0.011 secs, 3852 KB] Test 9: TEST OK [0.011 secs, 3852 KB]
/*
ID:nealgav1
PROG:fence6
LANG:C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int mm=200+9;
const int oo=1e9;
vector<int>ll[mm],rr[mm];
int g[mm][mm],n,len[mm],dis[mm][mm];
int main()
{ freopen("fence6.in","r",stdin);
freopen("fence6.out","w",stdout);
int a,b,c,d;
while(~scanf("%d",&n))
{
for(int i=0;i<n;++i)
{
scanf("%d",&a);
scanf("%d%d%d",&len[a],&b,&c);
for(int j=0;j<b;++j)
{
scanf("%d",&d);ll[a].push_back(d);
}
for(int j=0;j<c;++j)
{
scanf("%d",&d);rr[a].push_back(d);
}
}
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
g[i][j]=oo;
int z;
for(int i=1;i<=n;++i)
{ z=ll[i].size();
for(int j=0;j<z;++j)
g[i][ll[i][j]]=len[i]+len[ll[i][j]];
z=rr[i].size();
for(int j=0;j<z;++j)
g[i][rr[i][j]]=len[i]+len[rr[i][j]];
}
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
dis[i][j]=g[i][j];
int ans=oo,u,v;
for(int i=1;i<=n;++i)
{
for(int j=0;j<ll[i].size();++j)
{
u=ll[i][j];
if(u<i)
{
for(int k=0;k<rr[i].size();++k)
{
v=rr[i][k];
if(v<i)
{
ans=min(ans,dis[u][v]+g[u][i]+g[v][i]);
}
}
}
}
for(int j=1;j<=n;++j)
for(int k=1;k<=n;++k)
dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
}
printf("%d\n",ans/2);
}
return 0;
}