Walls
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7677   Accepted: 3719

Description

In a country, great walls have been built in such a way that every great wall connects exactly two towns. The great walls do not cross each other. Thus, the country is divided into such regions that to move from one region to another,
it is necessary to go through a town or cross a great wall. For any two towns A and B, there is at most one great wall with one end in A and the other in B, and further, it is possible to go from A to B by always walking in a town or along a great wall. The
input format implies additional restrictions.



There is a club whose members live in the towns. In each town, there is only one member or there are no members at all. The members want to meet in one of the regions (outside of any town). The members travel riding their bicycles. They do not want to enter
any towns, because of the traffic, and they want to cross as few great walls as possible, as it is a lot of trouble. To go to the meeting region, each member needs to cross a number (possibly 0) of great walls. They want to find such an optimal region that
the sum of these numbers (crossing-sum, for short) is minimized.




The towns are labeled with integers from 1 to N, where N is the number of towns. In Figure 1, the labeled nodes represent the towns and the lines connecting the nodes represent the great walls. Suppose that there are three members, who live in towns 3, 6, and
9. Then, an optimal meeting region and respective routes for members are shown in Figure 2. The crossing-sum is 2: the member from town 9 has to cross the great wall between towns 2 and 4, and the member from town 6 has to cross the great wall between towns
4 and 7.



You are to write a program which, given the towns, the regions, and the club member home towns, computes the optimal region(s) and the minimal crossing-sum.

Input

Your program is to read from standard input. The first line contains one integer: the number of regions M, 2 <= M <= 200. The second line contains one integer: the number of towns N, 3 <= N <= 250. The third line contains one integer:
the number of club members L, 1 <= L <= 30, L <= N. The fourth line contains L distinct integers in increasing order: the labels of the towns where the members live.



After that the input contains 2M lines so that there is a pair of lines for each region: the first two of the 2M lines describe the first region, the following two the second and so on. Of the pair, the first line shows the number of towns I on the border of
that region. The second line of the pair contains I integers: the labels of these I towns in some order in which they can be passed when making a trip clockwise along the border of the region, with the following exception. The last region is the "outside region"
surrounding all towns and other regions, and for it the order of the labels corresponds to a trip in counterclockwise direction. The order of the regions gives an integer labeling to the regions: the first region has label 1, the second has label 2, and so
on. Note that the input includes all regions formed by the towns and great walls, including the "outside region".

Output

Your program is to write to standard output. The first line contains one integer: the minimal crossing-sum.

Sample Input

10
10
3
3 6 9
3
1 2 3
3
1 3 7
4
2 4 7 3
3
4 6 7
3
4 8 6
3
6 8 7
3
4 5 8
4
7 8 10 9
3
5 10 8
7
7 9 10 5 4 2 1

Sample Output

2

Source

IOI 2000

以每一个区域为点建立图,然后暴力搜索最小的区域

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; int m,n,L;
int man[300];//成员
int Area[250][300];//区域
int Dis[250][250];//区域之间的距离
int dis[40];//人到某个区域的距离
void floyd()//计算区域的最短路
{
for(int k=1;k<=m;k++)
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
if(Dis[i][j]>Dis[i][k]+Dis[k][j])
{
Dis[i][j]=Dis[i][k]+Dis[k][j];
}
}
}
}
} int solve()//暴力枚举区域找最小的值
{
int Min=INF;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=L;j++)
{
dis[j]=INF;
}
for(int j=1;j<=m;j++)
{
for(int k=1;k<=Area[j][0];k++)
{
if(man[Area[j][k]]>0&&dis[man[Area[j][k]]]>Dis[i][j])
{
dis[man[Area[j][k]]]=Dis[i][j];
}
}
}
int temp=0;
for(int j=1;j<=L;j++)
{
temp+=dis[j];
}
Min=min(Min,temp);
}
return Min;
}
int main()
{
int a;
while(~scanf("%d",&m))
{
scanf("%d %d",&n,&L);
memset(man,0,sizeof(man));
for(int i=1; i<=L; i++)
{
scanf("%d",&a);
man[a]=i;//在那个点住着的成员编号
}
for(int i=1;i<=m;i++)//初始化
{
for(int j=i;j<=m;j++)
{
if(i==j)
{
Dis[i][j]=0;
}
else
{
Dis[i][j]=Dis[j][i]=INF;
}
}
}
for(int i=1; i<=m; i++)
{
scanf("%d",&Area[i][0]);
for(int j=1; j<=Area[i][0]; j++)
{
scanf("%d",&Area[i][j]);//区域的点集,逆时针方向
}
Area[i][Area[i][0]+1]=Area[i][1];//闭合区域
for(int j=1; j<=Area[i][0]; j++)//判断区域是不是相邻
{
for(int k=1; k<i; k++)
{
for(int s=1; s<=Area[k][0]; s++)
{
if(Area[i][j+1]==Area[k][s]&&Area[i][j]==Area[k][s+1])//判断区域是不是邻接
{
Dis[i][k]=Dis[k][i]=1;
}
}
}
}
}
floyd();
printf("%d\n",solve());
}
return 0;
}

Walls(floyd POJ1161)的更多相关文章

  1. POJ 1161 Walls ( Floyd && 建图 )

    题意 :  在某国,城市之间建起了长城,每一条长城连接两座城市.每条长城互不相交.因此,从一个区域到另一个区域,需要经过一些城镇或者穿过一些长城.任意两个城市A和B之间最多只有一条长城,一端在A城市, ...

  2. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

  3. POJ 1161 Walls(Floyd , 建图)

    题意: 给定n个城市, 然后城市之间会有长城相连, 长城之间会围成M个区域, 有L个vip(每个vip会处于一个城市里)要找一个区域聚会, 问一共最少跨越多少个长城. 分析: 其实这题难就难在建图, ...

  4. POJ 1161 Walls【floyd 以面为点建图】

    题目链接:http://poj.org/problem?id=1161 题目大意: 1.给出m个区域,n个俱乐部点.接下来是n个俱乐部点以及各个区域由什么点围成.求一个区域到各个俱乐部点的距离之和最小 ...

  5. Floyd 求最短路(poj 1161)

    Floyd-Warshall算法介绍: Floyd-Warshall算法的原理是动态规划. 设为从到的只以集合中的节点为中间节点的最短路径的长度. 若最短路径经过点k,则: 若最短路径不经过点k,则. ...

  6. poj 1161 Walls

    https://vjudge.net/problem/POJ-1161 题意:有m个区域,n个小镇,有c个人在这些小镇中,他们要去某一个区域中聚会,从一个区域到另一个区域需要穿墙,问这些人聚到一起最少 ...

  7. floyd算法学习笔记

    算法思路 路径矩阵 通过一个图的权值矩阵求出它的每两点间的最短路径矩阵.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1):又 ...

  8. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  9. 最短路径之Floyd算法

    Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...

随机推荐

  1. 导出所选行为excle

    要实现的是将所选行导出.例如勾选这两条

  2. java项目开发的一些准备工作

    做项目有一段时间了,每次接手一个新项目都要在开发前做些准备工作,方便开发. 有些东西在配置的时候经常会忘记,所有整理一份,方便以后查阅! 1.安装JDK及搭建环境,安装tomcat及搭建环境,这些一般 ...

  3. css - float浮动模块的高度问题 解决方案

    当一个Div中的子元素都是浮动元素时,该div是没有高度的.通常会带来很多困扰,解决方案如下: 低版本统配兼容: overflow: hidden; 下面是不支持低配浏览器,而且似乎该效果对 P 标签 ...

  4. IOS第13天(2,私人通讯录,plist存储,偏好设置,归档)

    ***************plist存储 // 当点点击保存的时候调用 //保存 - (IBAction)save:(id)sender { // 获取沙盒的根路径 // NSString *ho ...

  5. C使用相关笔记

    #将c文件编译成动态库 //hello.c int hello_add(int a, int b) { return a + b; } gcc -O -c -fPIC -o hello.o hello ...

  6. JMeter学习-019-JMeter 监听器之【聚合报告】界面字段解析及计算方法概要说明

    聚合报告是 JMeter 使用过程中使用率非常高的监听器之一,可通过右键单击,依次选择[添加 / 监听器 / 聚合报告] 来进行添加.执行 JMeter 脚本后,聚合报告显示如下:

  7. spring 连接各种数据源的配置(转载)

    在 开发基于数据库的应用系统时,需要在项目中进行数据源的配置来为数据 库的操作取得数据库连接.配置不同数据库的数据源的方法大体上都是相同的,不同的只是不同数据库的JDBC驱动类和连接URL以及相应的数 ...

  8. Unit01-OOP-对象和类(上)

    Unit01-OOP-对象和类(上) 1.什么是类?什么是对象?  1)现实生活是由很多很多对象组成的    基于对象抽出了类  2)对象:真实存在的单个的个体    类:类型.类别,代表一类个体  ...

  9. 一个编程小白,如何入门APP软件开发领域?

    近些年,互联网创业火得不得了!一时间,满世界都在招做App软件开发的专业人员.从大众角度来看,学编程,写代码,是一件非常困难的事情.但是,App开发人员的工资那么诱人,让很多小白也跃跃欲试想学一下.那 ...

  10. db2常用命令大全

    #显示这个DB2错误的解释信息(SQLSTATE 5位数字)db2 ? 42704 #显示这个SQLCODE的解释信息(SQLCODE 四位数字) db2 ? SQL0204N ##查看数据库指定配置 ...