USACO 4.1 Fence Loops

Fence Loops

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.

Happily, no fence connects to itself.

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:

  • The first line of each record contains four integers: s, the segment number (1 <= s <= N); Ls, the length of the segment (1 <= Ls <= 255); N1s (1 <= N1s <= 8) the number of items on the subsequent line; and N2sthe number of items on the line after that (1 <= N2s <= 8).
  • The second line of the record contains N1 integers, each representing a connected line segment on one end of the fence.
  • The third line of the record contains N2 integers, each representing a connected line segment on the other end of the fence.

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

无向图求最小环
把图中边信息转化成点,floyd求最小环。
 /*
ID:hyx34931
LANG:C++
TASK:fence6
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector> using namespace std; const int MAX_N = ;
const int INF = 1e6 + ;
int g[MAX_N][MAX_N], f[MAX_N][MAX_N];
int id[MAX_N][];
int N, n = ;
bool done[MAX_N]; void dfs(int u) {
printf("%d\n", u);
done[u] = ;
for (int i = ; i < n; ++i) {
if (f[u][i] && !done[i]) dfs(i);
}
} void floyd() {
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
f[i][j] = g[i][j];
}
} int ans = INF;
for (int k = ; k < n; ++k) {
for (int i = ; i <= k - ; ++i) {
for (int j = i + ; j <= k - ; ++j) {
ans = min(ans, f[i][j] + g[i][k] + g[k][j]);
}
} for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
}
} //printf("%d\n", f[0][3]);
printf("%d\n", ans);
}
int main()
{
freopen("fence6.in", "r", stdin);
freopen("fence6.out", "w", stdout);
scanf("%d", &N);
memset(id, -, sizeof(id));
//printf("%d %d\n", id[7][0], id[7][1]);
for (int i = ; i <= N; ++i) {
int n1, n2, cost, u;
scanf("%d%d%d%d", &u, &cost, &n1, &n2); int t[];
for (int j = ; j <= n1; ++j) {
scanf("%d", &t[j]);
}
if (id[u][] != id[ t[]][] && id[u][] != id[ t[] ] []) swap(id[u][], id[u][]);
if (id[u][] == -) {
id[u][] = n++;
for (int j = ; j <= n1; ++j) {
if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
else if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
}
}
for (int j = ; j <= n2; ++j) {
scanf("%d", &t[j]);
} if (id[u][] == -) {
id[u][] = n++;
for (int j = ; j <= n2; ++j) {
if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
else if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
}
} g[ id[u][] ][ id[u][] ] = g[ id[u][] ][ id[u][] ] = cost;
} for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
if (!g[i][j]) g[i][j] = INF;
if (i == j) g[i][j] = ; }
}
//memset(done, 0, sizeof(done));
/*printf("n = %d\n", n);
for (int i = 1; i <= N; ++i) {
printf("%d : %d %d\n", i, id[i][0], id[i][1]);
}*/
//dfs(0);
floyd();
//cout << "Hello world!" << endl;
return ;
}
上一篇:USACO 4.1 Fence Rails


下一篇:usaco training 4.1.2 Fence Rails 题解