-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathpatrol.cpp
102 lines (95 loc) · 1.92 KB
/
patrol.cpp
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
# include <fstream>
# include <vector>
# include <queue>
# include <algorithm>
# define inf 999999999
# define N 1050
using namespace std;
ifstream f("patrol.in");
ofstream g("patrol.out");
vector <int> v[N];
vector <short> ap[N][125];
struct elem
{
int nod, timp;
}E;
queue <elem> q;
int i,j,n,m,x,y,p,L,cmmmc,minn;
int P[N][125], cost[N][125], C[N];
int cmmdc (int a, int b)
{
int R;
while (b!=0)
{
R=a%b;
a=b;
b=R;
}
return a;
}
void citire ()
{
int i,j,ant;
f>>n>>m>>p;
for (i=1; i<=n; ++i)
f>>C[i];
for (i=1; i<=m; ++i)
{
f>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for (i=1; i<=p; ++i)
{
f>>L;
cmmmc=cmmmc*2*(L-1)/cmmdc(cmmmc, 2*(L-1));
for (j=1; j<=L; ++j)
{
f>>x;
P[x][j]=1; P[x][j+2*(L-j)]=1;
if (j>1) ap[ant][x].push_back(j), ap[x][ant].push_back(j+2*(L-j)+1);
ant=x;
}
}
}
void init ()
{
int i,j,k;
for (int i=1; i<=n; ++i)
for (int j=0; j<cmmmc; ++j)
cost[i][j]=inf;
}
void BF (int k, int timp)
{
int i,j,urm,T;
E.nod=k; E.timp=timp;
cost[k][timp]=C[k]; q.push(E);
while (! q.empty())
{
E=q.front(); q.pop();
k=E.nod; timp=E.timp;
if (k==n) minn=min(minn, cost[k][timp]);
if (cost[k][timp]>=minn) continue;
for (i=0; i<v[k].size(); ++i)
{
urm=v[k][i]; T=(timp+1)%cmmmc;
if (cost[urm][T]>cost[k][timp]+C[urm])
if (! P[urm][T] && (find(ap[urm][k].begin(), ap[urm][k].end(), T)==ap[urm][k].end()))
{
cost[urm][T]=cost[k][timp]+C[urm];
E.nod=urm; E.timp=T;
q.push(E);
}
}
}
}
int main ()
{
cmmmc=1;
citire (); cmmmc+=2;
init ();
minn=inf;
BF (1, 1);
g<<minn<<"\n";
return 0;
}