版权声明:本篇随笔版权归作者Etta(http://www.cnblogs.com/Etta/)所有,转载请保留原地址!

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

一、分析问题

乍一看全英文的题面已经要晕,但沉下心来读其实不难。求解的是每个town的人到达同一个region使总共穿过的wall最少。可以把每一个region抽象为一个点,相邻的region间连一条长度为1的边,再上最短路求任意两点间的距离+枚举即可。

二、解决问题

Floyed+枚举

本题新颖在构图。最开始我没有给每个区域存边,只是把点sort排序之后,两个region若包含两个或以上相同的点就连边。但是在一个region中两个点不一定能连边,所以是不对的。最终改为判断region间是否有相同的边来连边。

存区域时,最外圈也要存为一个新的区域(即输入最后一行)。

三、代码实现

#include<cstdio>
#include<algorithm>
using namespace std;const int MA=210,B=260,inf=4e8;int m,n,w,l,ss,h,t,sum=inf;//m region n town l members
int a[B],s[MA];//w where members live
int g[MA][MA];
int k[B][MA],u[MA][B][3],v[B];
bool bo;void init()
{scanf("%d%d%d",&m,&n,&l);for(int i=1;i<=l;++i){scanf("%d",&w);v[++v[0]]=w;}for(int i=1;i<=m;++i){scanf("%d",&ss);//number of townfor(int j=1;j<=ss;++j){ scanf("%d",&a[j]);//the towns surrounding the regionk[a[j]][++k[a[j]][0]]=i;if(j!=1){u[i][++s[i]][0]=a[j-1];u[i][s[i]][1]=a[j];}     if(j==ss){u[i][++s[i]][0]=a[j];u[i][s[i]][1]=a[1];}}}
}void graph()
{for(int i=1;i<=m-1;++i)for(int j=i+1;j<=m;++j)g[i][j]=g[j][i]=inf;for(int i=1;i<=m-1;++i){for(int j=i+1;j<=m;++j){bo=0;if(i!=j)for(int f=1;f<=s[i];++f){for(int z=1;z<=s[j];++z){if((u[j][z][1]==u[i][f][1]&&u[j][z][0]==u[i][f][0])||(u[j][z][1]==u[i][f][0]&&u[j][z][0]==u[i][f][1])){g[i][j]=g[j][i]=1;bo=1;break;     }}if(bo)break;}}}
}void floyed()
{for(int q=1;q<=m;++q)for(int i=1;i<=m;++i)for(int j=1;j<=m;++j)if(g[i][j]>g[i][q]+g[q][j])g[i][j]=g[i][q]+g[q][j];
}void enu()
{for(int i=1;i<=m;++i)//枚举区域
    {int ans=0;for(int j=1;j<=v[0];++j){int minn=inf;for(int r=1;r<=k[v[j]][0];++r)if(g[k[v[j]][r]][i]<minn)minn=g[k[v[j]][r]][i];ans+=minn;}if(ans<sum)sum=ans;}printf("%d\n",sum);
}int main()
{init();graph();floyed();enu();return 0;
}

转载于:https://www.cnblogs.com/Etta/p/6357340.html

【最短路】Walls相关推荐

  1. [C] [最短路] 只有5行的算法:Floyd-Warshall

    终于学到求最短路了,终于来到我最喜欢的算法--Floyd-Warshall了!今天还有点小激动呢! 我喜欢它,当然是因为它逻辑十分简单咯!真的只有5行诶! Floyd-Warshall算法 题目描述 ...

  2. BZOJ4152 AMPPZ2014 The Captain(最短路)

    事实上每次走到横坐标或纵坐标最接近的点一定可以取得最优方案.于是这样连边跑最短路就可以了. #include<iostream> #include<cstdio> #inclu ...

  3. Codeforces.1051F.The Shortest Statement(最短路Dijkstra)

    题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...

  4. BZOJ1491: [NOI2007]社交网络(Floyd 最短路计数)

    Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 2343  Solved: 1266 [Submit][Status][Discuss] Descrip ...

  5. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

  6. E:By Elevator or Stairs? CF595 DP最短路

    题目链接 比赛的时候一看,这不是最短路吗,然后敲了一个最短路. 然后比赛完发现大家基本都写的dp,我真是个憨憨,dp3行 最短路就建个简单的图,dp就是从上一维转化过来就是了 优秀的dp: //#pr ...

  7. The Shortest Statement CodeForces - 1051F LCA+最短路

    太弱了... 一开始看到题感觉是跑一个最小生成树在上边进行LCA就行了,但是发现过不了样例,然后就是就想到了之前做过类似做法的题目,就是非生成树上的边最多只有21条,然后就那些边记录下来,通过每一条边 ...

  8. JZOJ #4722 跳楼机 (最短路模型的完美转化)

    题目描述: 给出$h,x,y,z$,求在$h$以内,$x,y,z$可以凑出多少个不同的数.$(1\leq{h}\leq{10^{18}},1\leq{x,y,z}\leq{10^5})$ 解题思路: ...

  9. matlab单机无限大系统_基于MATLAB的单机无穷大系统短路故障分析_吕鹏

    _______________________________电子技术__丝I 基于MA丁LAB的单机无穷大系统短路故障分析 山东科技大学吕鹏钟家成纪妮妮李漫漫 [摘要]本z l),NIATLAB7. ...

最新文章

  1. leetcode--括号生成--python
  2. tensorflow兼容问题
  3. redis 判断存在性_Redis如何保证接口的幂等性?
  4. linux中DNS的介绍及DNS的高速缓存
  5. combotree 可以异步加载吗_Unity AssetBundle 资源打包,加载,本地缓存方式,安卓,PC本地加载路径问题...
  6. Apache Lucene中的并发查询执行
  7. 云计算演进历程与模式 - 初识云计算知识专栏(2)
  8. python按键盘上哪个键运行_python按什么键运行
  9. java如何代码找错误_如何编写可怕的Java代码?
  10. 【渝粤教育】国家开放大学2018年秋季 0455-22T物流实务 参考试题
  11. 机器学习Scikit-Learn基本操作实战
  12. MySQL优化步骤和my.cnf优化配置
  13. 维护2G网络的稳定必须提升到战略高度
  14. MTK手机平台充电原理
  15. 数据治理之——数据标准体系建设示例
  16. 华三华为设备序列号查看生产日期
  17. 百度通用文字识别离线SDK部署(c#)
  18. 某连锁酒店集团实行积分奖励计划,会员每次入住集团旗下酒店均可以获得一定积分,积分由欢迎积分加消费积分构成。其中欢迎积分跟酒店等级有关,具体标准如表2-1所示;消费积分跟每次入住消费金额有关,具体标准为
  19. TopoJSON格式规范说明
  20. 从技术原理解析区块链为何列入新基建

热门文章

  1. 无法解析的外部符号 __imp__glewinit
  2. lesson3 一阶线性常微分方程解法
  3. 人身三流指什么_保险合同的主体是什么,主体的变更是什么?
  4. 计算机视觉基础:图像处理 Task 03 - 颜色空间互转
  5. 把Faster-RCNN的原理和实现阐述得非常清楚
  6. tensorflow使用object detection实现目标检测超详细全流程(视频+图像集检测)
  7. 深度学习必备的几款流行网络与数据集
  8. 个位数不含4用计算机表示,计算机组成原理
  9. php 算生存曲线,手把手教你解读生存曲线
  10. if和else同时执行_为什么大量的if else这么不受待见?怎么“干掉”它?