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 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写最小环,但是始终没想出来怎么写,结果发现好简单啊……果然还是自己蒟蒻

回归floyd的三维形式g[k,i,j]也就是用前k个点更新了i,j,然后我们发现我们求的环就是

i到j不包括k的最短路+i到k距离+k到j距离

我们更新一次floyd二维矩阵存储的就是g[k-1,i,j],然后我们得到的k点是新的,未被使用过,这样就可以做了

【这道题图给的形式真是好恶心啊……】

 1 /*
 2 ID: ivorysi
 3 PROG: fence6
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <set>
12 #include <vector>
13 #include <string.h>
14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
18 #define inf 0x1f1f1f1f
19 #define ivorysi
20 #define mo 97797977
21 #define ha 974711
22 #define ba 47
23 #define fi first
24 #define se second
25 #define pii pair<int,int>
26 using namespace std;
27 typedef long long ll;
28 int s,len[105],num[105][4],cnt,a1[105],a2[105],use[105];
29 int g[205][205],f[205][205];
30 int n,t1,t2,ans;
31 void solve(){
32     scanf("%d",&n);
33     siji(i,1,n) {
34         scanf("%d",&s);
35         scanf("%d",&len[s]);
36         scanf("%d%d",&t1,&t2);
37
38         bool f1=1,f2=1;
39         siji(j,1,t1) {
40             scanf("%d",&a1[j]);
41             if(use[a1[j]]) f1=0;
42         }
43         siji(j,1,t2) {
44             scanf("%d",&a2[j]);
45             if(use[a2[j]]) f2=0;
46         }
47         if(num[s][0]>=2)continue;
48         if(f1) {
49             num[s][++num[s][0]]=++cnt;
50             siji(j,1,t1) {
51                 num[a1[j]][++num[a1[j]][0]]=cnt;
52             }
53         }
54         if(f2) {
55             num[s][++num[s][0]]=++cnt;
56             siji(j,1,t2) {
57                 num[a2[j]][++num[a2[j]][0]]=cnt;
58             }
59         }
60         use[s]=1;
61     }
62     memset(g,inf,sizeof(g));
63     memset(f,inf,sizeof(f));
64     siji(i,1,n) {
65         g[num[i][1]][num[i][2]]=g[num[i][2]][num[i][1]]=len[i];
66         f[num[i][1]][num[i][2]]=f[num[i][2]][num[i][1]]=len[i];
67     }
68     siji(i,1,cnt) {g[i][i]=0;f[i][i]=0;}
69     ans=inf;
70     siji(k,1,cnt) {
71         xiaosiji(i,1,k) {//因为不能用k点所以是<k
72             xiaosiji(j,i+1,k) {
73                 ans=min(ans,g[i][j]+f[i][k]+f[k][j]);
74             }
75         }
76         siji(i,1,cnt) {
77             siji(j,1,cnt) {
78                 g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
79             }
80         }
81     }
82
83     printf("%d\n",ans);
84 }
85 int main(int argc, char const *argv[])
86 {
87 #ifdef ivorysi
88     freopen("fence6.in","r",stdin);
89     freopen("fence6.out","w",stdout);
90 #else
91     freopen("f1.in","r",stdin);
92 #endif
93     solve();
94 }
时间: 2024-12-26 13:23:47

USACO 4.1 Fence Loops(Floyd求最小环)的相关文章

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

HDU - 1599 find the mincost route(Floyd求最小环)

find the mincost route Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个

【BZOJ 1027】 (凸包+floyd求最小环)

[题意] 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金的铁铝锡比重为用户所需要的比重. 现在,用户给出了n种他们需要的合金,以及每种合金中铁铝锡的比重.公司希望能够订购最少种类的原材料,并且使用这些原材料可以加工出用户需要的所有种类的合金. [分析] 只要考虑前两个物质的比例,因为加起来等于1. 如果你看过zoj3154,就会知道这题的模型,用二元组表

多源最短路径Floyd、Floyd求最小环【模板】

Floyd算法:用来找出每对点之间的最短距离.图可以是无向图,也可以是有向图,边权可为正,也可以为负,唯一要求是不能有负环. 1.初始化:将Map[][]中的数据复制到Dist[][]中作为每对顶点之间的最短路径的初值,Pre[i][j] = i 表示 i 到 j 路径中 j 的前一节点. 2. k 从 1 到 N 循环 N 次,每次循环中,枚举图中不同的两点 i,j,如果Dist[i][j] > Dist[i][k] + Dist[k][j],则更新Dist[i][j] = Dist[i][k

Floyd求最小环!(转载,非原创)

//Floyd 的 改进写法可以解决最小环问题,时间复杂度依然是 O(n^3),储存结构也是邻接矩阵 int mincircle = infinity; Dist = Graph; for(int k=0;k<nVertex;++k){ //新增部分: for(int i=0;i<k;++i) for(int j=0;j<i;++j) mincircle = min(mincircle,Dist[i][j]+Graph[j][k]+Graph[k][i]); //通常的 floyd 部分

HDU 1599 find the mincost route (Floyd求最小环) &gt;&gt;

Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区.现在8600需要你帮他找一条这样的路线,并且花费越少越好. Input 第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数. 接下来的M行里,每行包括

弗洛伊德Floyd求最小环

模板: #include<bits/stdc++.h> using namespace std; const int MAXN = 110; const int INF = 0xffffff0; int temp,Map[MAXN][MAXN],Dist[MAXN][MAXN],pre[MAXN][MAXN],ans[MAXN*3]; void Solve(int i,int j,int k) { temp = 0; //回溯,存储最小环 while(i != j) { ans[temp++]

Floyd求最小环

首先求最小环有一个比较好想的方法:每次删掉一条边,看看这条边n所连的点i之间的距离(dijkstra),时间复杂度O(m*V^2*logv) 其实floyd也能完成这个功能.f[i][j][k]表示i到j在中间点为1~k的最近距离 对于一个环,我们假设i和j中只夹这一个数k,则环长为f[i][j][k-1]+map[i][k]+map[k][j],枚举一下 #include<iostream> #include<cstdio> #include<algorithm> #

BZOJ_1027_[JSOI2007]_合金_(计算几何+Floyd求最小环)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1027 共三种金属,\(m\)种材料,给出每种材料中三种金属的占比. 给出\(n\)种合金的三种金属占比.用材料做合金,问最少需要多少种材料. 分析 首先,由于三种金属的占比相加为1,所以确定了前两项,最后一项也就确定了,我们可以用二唯坐标\((x,y)\)表示前两项,这样每种材料和合金就是二维平面上的一个点. 接下来是用材料做合金. 首先来考虑用两种材料做合金,两种材料为\((x1,y1)和