POJ 1161 Walls

Walls
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7166   Accepted: 3533

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. 
POJ 1161 Walls

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

 

 

分析:

这一题是要我们化块为边,给每一块空地编号,先预处理求出两块块空地是否相邻,对于空地ij,i,j相邻,则G[i][j]=1,即需要翻越一个城墙,否则G[i][j]=0,然后一遍Floyd求出一个空地到另一个空地需要翻越的最少城墙数,最后枚举在哪个空地开Party就行了

注意:IOI原题要求输出任意一个最优空地,而POJ没有要求,所以在POJ提交的时候把打印Best变量那个地方去掉就完事了。

 

 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 

 5 using namespace std;

 6 

 7 const int INF=99999999;

 8 

 9 int people[260],area[210][260];

10 int g[210][210],dis[260];

11 int N,M,L,ans;

12 

13 void Floyd(){       //Floyd求任意两个空地间需要穿越的最小城墙数

14     int i,j,k;

15     for(k=1;k<=M;k++)

16         for(i=1;i<=M;i++)

17             for(j=1;j<=M;j++)

18                 if(g[i][j]>g[i][k]+g[k][j])

19                     g[i][j]=g[i][k]+g[k][j];

20 }

21 

22 void Solve(){

23     int i,j,k;

24     int tmp;

25     ans=INF;

26     for(i=1;i<=M;i++){      //枚举在哪个空地办Party,Dist数组为每个人到当前枚举空地的最短距离

27         for(j=1;j<=L;j++)

28             dis[j]=INF;

29         for(j=1;j<=M;j++)

30             for(k=1;k<=area[j][0];k++)  

31                 if(people[area[j][k]]>0 && dis[people[area[j][k]]]>g[i][j])

32                     dis[people[area[j][k]]]=g[i][j];

33         tmp=0;

34         for(j=1;j<=L;j++)

35             tmp+=dis[j];

36         if(tmp<ans)

37             ans=tmp;

38     }

39 }

40 

41 int main(){

42 

43     //freopen("input.txt","r",stdin);

44 

45     while(scanf("%d",&M)!=EOF){

46         scanf("%d%d",&N,&L);

47         int i,j;

48         for(i=1;i<=L;i++){

49             scanf("%d",&j);

50             people[j]=i;

51         }

52         memset(g,0,sizeof(g));

53         for(i=1;i<=M;i++)

54             for(j=1;j<=M;j++)

55                 if(i!=j)

56                     g[i][j]=INF;

57         int k1,k2;

58         for(i=1;i<=M;i++){

59             scanf("%d",&area[i][0]);

60             for(j=1;j<=area[i][0];j++)

61                 scanf("%d",&area[i][j]);

62             area[i][area[i][0]+1]=area[i][1];

63             for(j=1;j<=area[i][0];j++)

64                 for(k1=1;k1<i;k1++)

65                     for(k2=1;k2<=area[k1][0];k2++)      //预处理判断两块空地是否相邻(只要有一条边重合即为相邻)

66                         if(area[i][j]==area[k1][k2+1] && area[i][j+1]==area[k1][k2])  //因为题目描述的线段是逆时针构造的

67                             g[i][k1]=g[k1][i]=1;

68         }

69 

70         Floyd();

71         Solve();

72         printf("%d\n",ans);

73     }

74     return 0;

75 }

 

你可能感兴趣的:(poj)