• 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
    ", 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
    ", f[0][3]);
     53         printf("%d
    ", 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
    ", 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);
    103     for (int i = 1; i <= N; ++i) {
    104             printf("%d :  %d %d
    ", i, id[i][0], id[i][1]);
    105     }*/
    106     //dfs(0);
    107     floyd();
    108     //cout << "Hello world!" << endl;
    109     return 0;
    110 }
    View Code
  • 相关阅读:
    window 7/8/10 安装nginx
    全面了解 Nginx 到底能做什么
    MySQL优化
    office 2013 破解工具 及 软件下载
    centos6+如何对外开放80,3306端口号或者其他端口号
    CentOS 中查看软件的版本号
    CentOS 中安装 mysql 5.7+
    STL入门大全(待编辑)
    Feign
    微信公众号
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3844915.html
Copyright © 2020-2023  润新知