POJ 1161 Walls(最短路+枚举)
POJ 1161 Walls(最短路+枚举)
题目背景
题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了最外面的一个),现在,如果某几个小镇上的人想要聚会,为选择哪个区域为聚会地点,可以使他们所有人总共需要穿过的墙数最小,题目上有说明,不在某个点上聚会(聚会点在某个多边形内部),行进过程中不穿过图中的点(也就是除出发点外的其他小镇)。
输入第1行代表m(2<=M<=200)个区域
第2行代表n(3<=N<=250)个点
第3行代表俱乐部有L(1<=L<=30&&L<=N)个
第四行有L个数,分别标记哪些个点事是俱乐部
接下来2*m行,代表m个区域,每个区域由两行表示,第一行为区域由T个点围成的,
第二行T个数代表是哪些点围成这个区域,这些点按逆时针围成这个区域,相邻两点代表一条边
最后一点和第一点也是一条边,这样就是一个闭合的区域
输出最少的边数之和
题目描述
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.
输入输出格式
输入格式:
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…
输出格式:
Your program is to write to standard output. The first line contains one integer: the minimal crossing-sum.
Sample Input
输入输出样例
输入样例#1:
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
解题报告
这个题目比较奇葩。题意是让求穿过边的数量最小,所以我们可以把一个区域变成一个点,把公共边做成一条路。我们枚举每一个区域作为起点,用堆优dijkstra来求到每个区域的距离。因为题目问的是点到区域的距离,所以我们在计算总距离时,对于每个点我们要枚举其所属的区域,取最小值作为距离累加进答案。最后,再在每个由区域作为起点的答案中取最小值。 对于判断两个区域是否相邻,我的方法比较蠢,就是判断区域有没有公共边。如果数据卡人估计我也过不了。=,=!
#include <stdio.h>
#include <queue>
#include <cmath>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Pair pair<int,int>
#define MAXN 600+10
#define MAXM 1200000+1
using namespace std;
int l,n,m,num,head[MAXN],t,dis[MAXN][MAXN],v[MAXM];
int ans[MAXN],minn=,mian[MAXN][MAXN],pre[MAXN];
int map[MAXN][MAXN];
struct {
int p[MAXN];
void e()
{
memset(p,,sizeof(p));
}
}po[MAXN]; struct Edge{
int dis,next,to,exi,from;
}edge[MAXM]; void add(int from,int to,int dis)
{
edge[++num].next=head[from];
edge[num].to=to;
edge[num].dis=dis;
edge[num].from=from;
head[from]=num;
} void dij(int s)
{
memset(v,,sizeof(v));
priority_queue<Pair,vector<Pair>,greater<Pair> > h;
for(int i=;i<=m;i++) dis[s][i]=;
dis[s][s]=;
h.push(Pair(dis[s][s],s));
while(h.size()>)
{
int k=h.top().second;h.pop();
if(v[k]) continue;
v[k]=;
for(int i=head[k];i;i=edge[i].next)
if(dis[s][k]+edge[i].dis<dis[s][edge[i].to])
{
dis[s][edge[i].to]=dis[s][k]+edge[i].dis;
h.push(Pair(dis[s][edge[i].to],edge[i].to));
if(s==) pre[edge[i].to]=edge[i].from;
}
}
} int main()
{ scanf("%d%d%d",&m,&n,&l);
int numm=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
map[i][j]=map[j][i]=(++numm);
for(int i=;i<=l;i++)
scanf("%d",&ans[i]);
sort(ans+,ans+l+);
for(int i=;i<=m;i++)
{
int t=,f=,tt[MAXN];memset(tt,,sizeof(tt));
scanf("%d",&t);po[i].p[]=t;
for(int j=;j<=t;j++)
{
scanf("%d",&f);tt[j]=f;mian[f][++mian[f][]]=i;
if(j>)
{
po[i].p[j-]=map[tt[j]][tt[j-]];
} }
po[i].p[t]=map[tt[]][tt[t]];
}
for(int i=;i<=m;i++)
{
for(int j=i+;j<=m;j++)
{
int flag=;
for(int a1=;flag==&&a1<=po[i].p[];a1++)
for(int a2=;flag==&&a2<=po[j].p[];a2++)
{
if(po[i].p[a1]==po[j].p[a2])
{add(i,j,);add(j,i,);flag=;break;}
} }
} for(int i=;i<=m;i++)
dij(i);
for(int i=;i<=m;i++)
{
int temp=;
for(int j=;j<=l;j++)
{
int dc=;
for(int d=;d<=mian[ans[j]][];d++)
{
dc=min(dc,dis[i][mian[ans[j]][d]]);
}
temp+=dc;
}
minn=min(minn,temp);
}
printf("%d\n",minn); return ;
}
POJ 1161 Walls(最短路+枚举)的更多相关文章
- POJ 1161 Walls【floyd 以面为点建图】
题目链接:http://poj.org/problem?id=1161 题目大意: 1.给出m个区域,n个俱乐部点.接下来是n个俱乐部点以及各个区域由什么点围成.求一个区域到各个俱乐部点的距离之和最小 ...
- POJ 1161 Walls(Floyd , 建图)
题意: 给定n个城市, 然后城市之间会有长城相连, 长城之间会围成M个区域, 有L个vip(每个vip会处于一个城市里)要找一个区域聚会, 问一共最少跨越多少个长城. 分析: 其实这题难就难在建图, ...
- POJ 1161 Walls ( Floyd && 建图 )
题意 : 在某国,城市之间建起了长城,每一条长城连接两座城市.每条长城互不相交.因此,从一个区域到另一个区域,需要经过一些城镇或者穿过一些长城.任意两个城市A和B之间最多只有一条长城,一端在A城市, ...
- poj 1161 Walls
https://vjudge.net/problem/POJ-1161 题意:有m个区域,n个小镇,有c个人在这些小镇中,他们要去某一个区域中聚会,从一个区域到另一个区域需要穿墙,问这些人聚到一起最少 ...
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- CJOI 05新年好 (最短路+枚举)
CJOI 05新年好 (最短路+枚举) 重庆城里有n个车站,m条双向公路连接其中的某些车站.每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...
- Floyd 求最短路(poj 1161)
Floyd-Warshall算法介绍: Floyd-Warshall算法的原理是动态规划. 设为从到的只以集合中的节点为中间节点的最短路径的长度. 若最短路径经过点k,则: 若最短路径不经过点k,则. ...
- poj 1161 最短路构图
题目链接:http://poj.org/problem?id=1161 #include <cstdio> #include <cmath> #include <algo ...
随机推荐
- 有效解决ajax传中文时,乱码的情况,php处理接收到的值
在抽奖环节时,需把获奖名单通过ajax的post方式传输给php后台进行储存,但是php接收到的值确是乱码.在百度之后并没有找到合适的解决方法. 则使用js的encodeURI函数可以有效解决,但不知 ...
- 小学生都能学会的python(列表[ ])
小学生都能学会的python(列表[ ]) 1. 什么是列表(list) 能装东西的东西 列表中装的数据是没有限制的, 大小基本上是够用的 列表使用[]来表示. 在列表中每个元素与元素之间用逗号隔开 ...
- WinServer-IIS-MIME类型
自定义类型的处理流程 1.浏览器问服务器,这是什么类型的文件 2.服务器告诉浏览器这是什么类型的文件(如果不告诉,那么浏览器就会下载相应文件) 3.浏览器告诉windows注册表这是什么类型的文件 4 ...
- 什么叫openapi
Open API即开放API,也称开放平台. 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网站服务封装成一系列API(Application Programmin ...
- leetCode(24):Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- what's new in vc2015
1. 变量和函数的注解提示非常实用.象C#了. 2.CStdioFile升级了,不再须要象 vc2013中,用CStdioFileEx来修复错误了. 3. 发现再写.
- Swift的构造和析构过程
构造过程 Swift的构造过程通过定义构造器来实现. 只是与Objective-C不同的是,Swift的构造器不须要返回值,相同也不须要表明Func. 另外值得提的是,当构造器中为存储型属性赋值时.不 ...
- iOS 从各种效果图颜色标注生成 UIColor
iOS 从各种效果图颜色标注生成 UIColor 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公 ...
- 【甘道夫】Hadoop2.2.0环境使用Sqoop-1.4.4将Oracle11g数据导入HBase0.96,并自己主动生成组合行键
目的: 使用Sqoop将Oracle中的数据导入到HBase中,并自己主动生成组合行键! 环境: Hadoop2.2.0 Hbase0.96 sqoop-1.4.4.bin__hadoop-2.0.4 ...
- mount ntfs 失败解决办法
在双系统中,ntfs可能会应为windows的缓存而挂载失败.可用下面命令修复. Use ntfsfix in the terminal, even if you can't access Windo ...