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点是新的,未被使用过,这样就可以做了

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

 /*
ID: ivorysi
PROG: fence6
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x1f1f1f1f
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int s,len[],num[][],cnt,a1[],a2[],use[];
int g[][],f[][];
int n,t1,t2,ans;
void solve(){
scanf("%d",&n);
siji(i,,n) {
scanf("%d",&s);
scanf("%d",&len[s]);
scanf("%d%d",&t1,&t2); bool f1=,f2=;
siji(j,,t1) {
scanf("%d",&a1[j]);
if(use[a1[j]]) f1=;
}
siji(j,,t2) {
scanf("%d",&a2[j]);
if(use[a2[j]]) f2=;
}
if(num[s][]>=)continue;
if(f1) {
num[s][++num[s][]]=++cnt;
siji(j,,t1) {
num[a1[j]][++num[a1[j]][]]=cnt;
}
}
if(f2) {
num[s][++num[s][]]=++cnt;
siji(j,,t2) {
num[a2[j]][++num[a2[j]][]]=cnt;
}
}
use[s]=;
}
memset(g,inf,sizeof(g));
memset(f,inf,sizeof(f));
siji(i,,n) {
g[num[i][]][num[i][]]=g[num[i][]][num[i][]]=len[i];
f[num[i][]][num[i][]]=f[num[i][]][num[i][]]=len[i];
}
siji(i,,cnt) {g[i][i]=;f[i][i]=;}
ans=inf;
siji(k,,cnt) {
xiaosiji(i,,k) {//因为不能用k点所以是<k
xiaosiji(j,i+,k) {
ans=min(ans,g[i][j]+f[i][k]+f[k][j]);
}
}
siji(i,,cnt) {
siji(j,,cnt) {
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
}
} printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fence6.in","r",stdin);
freopen("fence6.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}

最新文章

  1. Win10连接远程桌面时提示“您的凭据不工作”
  2. [PAT]素因子分解(20)
  3. C#编写WIN32系统托盘程序
  4. js---html---body标签
  5. 剑指Offer:面试题23——从上往下打印二叉树(java实现)
  6. 视频学习_css基础学习
  7. Android:打包apk
  8. 李洪强漫谈iOS开发[C语言-023]-取余数运算符
  9. 近期刷题的c语言总结。
  10. MVC ASPX(webForm)视图引擎 &amp;lt;%:%&amp;gt; 与&amp;lt;%=%&amp;gt;的差别
  11. Fuel4D 2.1 免费跨平台游戏引擎 现已发布
  12. Qt中调用PolarSSL库(一)
  13. GIT分支管理是一门艺术(转)
  14. Linux下常用的压缩与解压命令
  15. 40个Java多线程问题
  16. 算法题丨Longest Consecutive Sequence
  17. psql的安装与数据库创建(ubuntu)
  18. HDFS介绍及简单操作
  19. 一些常用SQL语句大全
  20. HDU 1019 Least Common Multiple 数学题解

热门文章

  1. C#中使用消息队列RabbitMQ
  2. at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
  3. Android: 自定义Tab样式,一种简单的方式。
  4. ADFS 2.0 配置简介 PartⅢ – 声明规则语言
  5. 一步一步实现基于Task的Promise库(五)waitFor和waitForAny的实现
  6. ASP.NET MVC应用程序更新相关数据
  7. 【译】Objectively Speaking 2: A Crash Course in Objective-C for iOS 6
  8. Couchbase集群和Redis集群解析
  9. [转]解决MySQL出现大量unauthenticated user的问题
  10. PureMVC(JS版)源码解析