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求最小环。

  1 /*
  2 ID:hyx34931
  3 LANG:C++
  4 TASK:fence6
  5 */
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <vector>
 12
 13 using namespace std;
 14
 15 const int MAX_N = 205;
 16 const int INF = 1e6 + 7;
 17 int g[MAX_N][MAX_N], f[MAX_N][MAX_N];
 18 int id[MAX_N][2];
 19 int N, n = 0;
 20 bool done[MAX_N];
 21
 22 void dfs(int u) {
 23         printf("%d\n", u);
 24         done[u] = 1;
 25         for (int i = 0; i < n; ++i) {
 26                 if (f[u][i] && !done[i]) dfs(i);
 27         }
 28 }
 29
 30 void floyd() {
 31         for (int i = 0; i < n; ++i) {
 32                 for (int j = 0; j < n; ++j) {
 33                         f[i][j] = g[i][j];
 34                 }
 35         }
 36
 37         int ans = INF;
 38         for (int k = 0; k < n; ++k) {
 39                 for (int i = 0; i <= k - 1; ++i) {
 40                         for (int j = i + 1; j <= k - 1; ++j) {
 41                                 ans = min(ans, f[i][j] + g[i][k] + g[k][j]);
 42                         }
 43                 }
 44
 45                 for (int i = 0; i < n; ++i) {
 46                         for (int j = 0; j < n; ++j) {
 47                                 f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
 48                         }
 49                 }
 50         }
 51
 52         //printf("%d\n", f[0][3]);
 53         printf("%d\n", ans);
 54 }
 55 int main()
 56 {
 57     freopen("fence6.in", "r", stdin);
 58     freopen("fence6.out", "w", stdout);
 59     scanf("%d", &N);
 60     memset(id, -1, sizeof(id));
 61     //printf("%d %d\n", id[7][0], id[7][1]);
 62     for (int i = 1; i <= N; ++i) {
 63             int n1, n2, cost, u;
 64             scanf("%d%d%d%d", &u, &cost, &n1, &n2);
 65
 66
 67             int t[10];
 68             for (int j = 1; j <= n1; ++j) {
 69                     scanf("%d", &t[j]);
 70             }
 71             if (id[u][0] != id[ t[1]][0] && id[u][0] != id[ t[1] ] [1]) swap(id[u][0], id[u][1]);
 72             if (id[u][0] == -1) {
 73                     id[u][0] = n++;
 74                     for (int j = 1; j <= n1; ++j) {
 75                             if (id[ t[j] ][0] == -1) id[ t[j] ][0] = id[u][0];
 76                             else if (id[ t[j] ][1] == -1) id[ t[j] ][1] = id[u][0];
 77                     }
 78             }
 79             for (int j = 1; j <= n2; ++j) {
 80                     scanf("%d", &t[j]);
 81             }
 82
 83             if (id[u][1] == -1) {
 84                     id[u][1] = n++;
 85                     for (int j = 1; j <= n2; ++j) {
 86                             if (id[ t[j] ][0] == -1) id[ t[j] ][0] = id[u][1];
 87                             else if (id[ t[j] ][1] == -1) id[ t[j] ][1] = id[u][1];
 88                     }
 89             }
 90
 91             g[ id[u][0] ][ id[u][1] ] = g[ id[u][1] ][ id[u][0] ] = cost;
 92     }
 93
 94     for (int i = 0; i < n; ++i) {
 95             for (int j = 0; j < n; ++j) {
 96                     if (!g[i][j]) g[i][j] = INF;
 97                     if (i == j) g[i][j] = 0;
 98
 99             }
100     }
101     //memset(done, 0, sizeof(done));
102     /*printf("n = %d\n", n);
103     for (int i = 1; i <= N; ++i) {
104             printf("%d :  %d %d\n", i, id[i][0], id[i][1]);
105     }*/
106     //dfs(0);
107     floyd();
108     //cout << "Hello world!" << endl;
109     return 0;
110 }

USACO 4.1 Fence Loops

时间: 2024-10-10 12:38:14

USACO 4.1 Fence Loops的相关文章

USACO 4.1 Fence Loops(Floyd求最小环)

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

洛谷P2738 [USACO4.1]篱笆回路Fence Loops

P2738 [USACO4.1]篱笆回路Fence Loops 11通过 21提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 农夫布朗的牧场上的篱笆已经失去控制了.它们分成了1~200英尺长的线段.只有在线段的端点处才能连接两个线段,有时给定的一个端点上会有两个以上的篱笆.结果篱笆形成了一张网分割了布朗的牧场.布朗想将牧场恢复原样,出于这个考虑,他首先得知道牧场上哪一块区域的周长最小. 布朗将他的每段篱笆从1到N进行了标号

洛谷 P2738 [USACO4.1]篱笆回路Fence Loops

P2738 [USACO4.1]篱笆回路Fence Loops 题目描述 农夫布朗的牧场上的篱笆已经失去控制了.它们分成了1~200英尺长的线段.只有在线段的端点处才能连接两个线段,有时给定的一个端点上会有两个以上的篱笆.结果篱笆形成了一张网分割了布朗的牧场.布朗想将牧场恢复原样,出于这个考虑,他首先得知道牧场上哪一块区域的周长最小. 布朗将他的每段篱笆从1到N进行了标号(N=线段的总数).他知道每段篱笆有如下属性: 该段篱笆的长度 该段篱笆的一端所连接的另一段篱笆的标号 该段篱笆的另一端所连接

USACO 4.1 Fence Rails

Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber s

USACO 6.3 Fence Rails(一道纯剪枝应用)

Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber s

USACO 3.3 fence 欧拉回路

题意:求给定图的欧拉回路(每条边只走一次) 若欧拉回路存在,图中只可能有0个or2个奇数度的点. 求解时,若有奇数度的点,则必须从该点开始.否则可以从任一点开始 求解过程:dfs 1 //主程序部分 2 # circuit is a global array 3 find_euler_circuit 4 circuitpos = 0 5 find_circuit(node 1) 6 --------------------------------------------- 7 # nextnod

USACO fence

题目的意思就是给你一个图, 输出他的欧拉路(欧拉通路 或者 欧拉回路),无向图欧拉回路判断条件是:1:图连通 2:所有点的度数为偶数     无向图欧拉通路的条件是:1:图连通 2:有且只有两个点的度数为奇数, 不过寻找欧拉路的代码是一样的,学习了新的建图方法,代码如下: /* ID: m1500293 LANG: C++ PROG: fence */ #include <cstdio> #include <cstring> #include <algorithm> #

usaco Electric Fence

这种有小数的题目总会令我格外头疼. /* ID: modengd1 PROG: fence9 LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <math.h> using namespace std; long long ans,x1,x2; int n,m,p; int main() { freopen("fence9.in"

USACO 3.4 Electric Fence 皮克定理

题意:在方格纸上画出一个三角形,求三角形里面包含的格点的数目 因为其中一条边就是X轴,一开始想的是算出两条边对应的数学函数,然后枚举x坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i.边上格点数目b的关系:A = i + b/2 - 1. 有了这条定理就好办了. 三角形面积直接用公式就能算出来. 对于从点(0,0)到点(x,y)的线段,该线段上的格点数目即gcd(x,y)+1 这样A和b都有了,套公式就行了.