-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1161.cpp
More file actions
135 lines (128 loc) · 2.21 KB
/
Copy path1161.cpp
File metadata and controls
135 lines (128 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
using namespace std;
struct region_t{
short count;
short town[250];
}region[201];
short town[251][251][2];
short minroad[31][201];
int queue_work[300];
int queue_res[300];
int queue_cnt;
int region_cnt;
int town_cnt;
int member_cnt;
int member_town[30];
void cal_min(int ci)
{
int i,j;
int cur;
int road;
int town1;
int town2;
int tn;
tn=member_town[ci];
for(i=1;i<town_cnt+1;++i)
{
if(town[tn][i][0]!=0)
{
if(0==minroad[ci][town[tn][i][0]])
{
minroad[ci][town[tn][i][0]]=1;
queue_res[queue_cnt++]=town[tn][i][0];
}
if(0==minroad[ci][town[tn][i][1]])
{
minroad[ci][town[tn][i][1]]=1;
queue_res[queue_cnt++]=town[tn][i][1];
}
}
}
road=1;
while(queue_cnt!=0)
{
road++;
memcpy(queue_work,queue_res,sizeof(queue_res));
i=queue_cnt-1;
queue_cnt=0;
for(;i>-1;--i)
{
cur=queue_work[i];
for(j=0;j<region[cur].count;++j)
{
town1=region[cur].town[j];
if(j>0){
town2=region[cur].town[j-1];
}
else{
town2=region[cur].town[region[cur].count-1];
}
if(cur==town[town1][town2][0])
{
if(0==minroad[ci][town[town1][town2][1]])
{
minroad[ci][town[town1][town2][1]]=road;
queue_res[queue_cnt++]=town[town1][town2][1];
}
}
else{
if(0==minroad[ci][town[town1][town2][0]])
{
minroad[ci][town[town1][town2][0]]=road;
queue_res[queue_cnt++]=town[town1][town2][0];
}
}
}
}
}
}
void filltown(int i,int j,int n)
{
if(0 == town[i][j][0]){
town[i][j][0]=n;
town[j][i][0]=n;
}
else{
town[i][j][1]=n;
town[j][i][1]=n;
}
}
int main()
{
int i=0,j=0;
short min=-1;
short t;
cin>>region_cnt;
cin>>town_cnt;
cin>>member_cnt;
for(i=0;i<member_cnt;++i){
cin>>member_town[i];
}
for(i=1;i<region_cnt+1;++i){
cin>>region[i].count;
for(j=0;j<region[i].count;++j){
cin>>region[i].town[j];
if(j>0){
filltown(region[i].town[j],region[i].town[j-1],i);
}
}
filltown(region[i].town[0],region[i].town[j-1],i);
}
for(i=0;i<member_cnt;++i){
cal_min(i);
}
for(i=1;i<region_cnt+1;++i){
t=0;
for(j=0;j<member_cnt;j++){
t+=minroad[j][i]-1;
}
if(-1 == min){
min=t;
}
else if(t<min){
min=t;
}
}
cout<<min<<endl;
return 0;
}