#include <bits/stdc++.h>
#define INF 2000000000000000000
using namespace std;
long long di[] = {0 , 0 , 0 , 1 , -1};
long long dj[] = {0 , 1 , -1 , 0 , 0};
priority_queue <pair <pair <long long,long long> , pair <long long,long long> > > h;
pair <long long,long long> v[100010];
deque <pair <long long,long long> > dq;
long long f[510][510];
long long dp[510][510][6];
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
long long n , m , a , b , c , k , ic , jc , iv , jv , dirc , dirv , i , j , val , d;
fscanf (fin,"%lld%lld%lld%lld%lld%lld",&n,&m,&a,&b,&c,&k);
n++;
m++;
for (i = 1 ; i <= k ; i++){
fscanf (fin,"%lld%lld",&v[i].first,&v[i].second);
v[i].first++;
v[i].second++;
if (i != k){ /// modifica aici daca vrei sa se plimbe si k
dq.push_back(v[i]);
f[v[i].first][v[i].second] = 1;
}
}
while (!dq.empty()){
ic = dq.front().first;
jc = dq.front().second;
dq.pop_front();
for (d = 1 ; d <= 4 ; d++){
iv = ic + di[d];
jv = jc + dj[d];
if (iv > 0 && jv > 0 && iv <= n && jv <= m && !f[iv][jv]){
f[iv][jv] = 1 + f[ic][jc];
dq.push_back(make_pair(iv , jv));
}
}
}
/// f[i][j] - 1 = distanta minima de la orice jucator (in afr de k) la i j
/// dp[i][j][dir] = cost minim sa ajungi in i j si directia sa fie dir
/// dir = 0 => apartine unui jucator
for (i = 1 ; i <= n ; i++){
for (j = 1 ; j <= m ; j++){
for (d = 0 ; d < 5 ; d++)
dp[i][j][d] = INF;
}
}
dp[v[1].first][v[1].second][0] = 0; /// stare initiala
h.push(make_pair( make_pair(0 , 0) , v[1] ) );
while (!h.empty()){
val = -h.top().first.first;
dirc = h.top().first.second;
ic = h.top().second.first;
jc = h.top().second.second;
h.pop();
//if (ic == 2 && jc == 5 && dirc == 1)
// printf ("a");
if (dp[ic][jc][dirc] != val)
continue;
//printf ("%lld %lld %lld\n" , ic , jc , dirc);
if (ic == v[k].first && jc == v[k].second){
fprintf (fout,"%lld",val);
return 0;
}
if (dirc != 0){ /// nu apartine niciunui jucator si e intr un sut
/// cazul 1 : un jucator o sa vina sa o ia aici
dirv = 0;
iv = ic;
jv = jc;
if (dp[iv][jv][dirv] > dp[ic][jc][dirc] + c * ( f[iv][jv] - 1 ) ){
dp[iv][jv][dirv] = dp[ic][jc][dirc] + c * ( f[iv][jv] - 1 );
h.push(make_pair( make_pair(-dp[iv][jv][dirv] , dirv) , make_pair(iv , jv) ) );
}
/// cazul 2 : continua pe directia aia
dirv = dirc;
iv = ic + di[dirc];
jv = jc + dj[dirc];
if (iv > 0 && jv > 0 && iv <= n && jv <= m && dp[iv][jv][dirv] > dp[ic][jc][dirc] + a ){
dp[iv][jv][dirv] = dp[ic][jc][dirc] + a;
h.push(make_pair( make_pair(-dp[iv][jv][dirv] , dirv) , make_pair(iv , jv) ) );
}
}
else {
/// acum apartine unui jucator
/// cazul 1 : jucatorul asta face un pas cu ea
dirv = 0;
for (d = 1 ; d < 5 ; d++){
iv = ic + di[d];
jv = jc + dj[d];
if (iv > 0 && jv > 0 && iv <= n && jv <= m && dp[iv][jv][dirv] > dp[ic][jc][dirc] + c ){
dp[iv][jv][dirv] = dp[ic][jc][dirc] + c;
h.push(make_pair( make_pair(-dp[iv][jv][dirv] , dirv) , make_pair(iv , jv) ) );
}
}
/// cazul 2 : jucatorul care o are acum ii da un sut intr o directie
for (d = 1 ; d < 5 ; d++){
iv = ic + di[d];
jv = jc + dj[d];
dirv = d;
if (iv > 0 && jv > 0 && iv <= n && jv <= m && dp[iv][jv][dirv] > dp[ic][jc][dirc] + a + b ){
dp[iv][jv][dirv] = dp[ic][jc][dirc] + a + b;
h.push(make_pair( make_pair(-dp[iv][jv][dirv] , dirv) , make_pair(iv , jv) ) );
}
}
}
}
return 0;
}
Compilation message
soccer.cpp: In function 'int main()':
soccer.cpp:16:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%lld%lld%lld%lld%lld%lld",&n,&m,&a,&b,&c,&k);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
soccer.cpp:20:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%lld%lld",&v[i].first,&v[i].second);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
7796 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
83 ms |
22772 KB |
Output is correct |
4 |
Correct |
40 ms |
18668 KB |
Output is correct |
5 |
Correct |
63 ms |
7680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
18664 KB |
Output is correct |
2 |
Correct |
28 ms |
16500 KB |
Output is correct |
3 |
Correct |
112 ms |
19932 KB |
Output is correct |
4 |
Correct |
271 ms |
22732 KB |
Output is correct |
5 |
Correct |
149 ms |
22112 KB |
Output is correct |
6 |
Correct |
173 ms |
22748 KB |
Output is correct |
7 |
Correct |
107 ms |
22876 KB |
Output is correct |
8 |
Correct |
97 ms |
22752 KB |
Output is correct |
9 |
Correct |
26 ms |
16740 KB |
Output is correct |
10 |
Correct |
23 ms |
8564 KB |
Output is correct |
11 |
Correct |
182 ms |
22884 KB |
Output is correct |
12 |
Correct |
194 ms |
22752 KB |
Output is correct |
13 |
Correct |
119 ms |
19936 KB |
Output is correct |
14 |
Correct |
79 ms |
22884 KB |
Output is correct |
15 |
Correct |
20 ms |
15228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
7796 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
83 ms |
22772 KB |
Output is correct |
4 |
Correct |
40 ms |
18668 KB |
Output is correct |
5 |
Correct |
63 ms |
7680 KB |
Output is correct |
6 |
Correct |
40 ms |
18664 KB |
Output is correct |
7 |
Correct |
28 ms |
16500 KB |
Output is correct |
8 |
Correct |
112 ms |
19932 KB |
Output is correct |
9 |
Correct |
271 ms |
22732 KB |
Output is correct |
10 |
Correct |
149 ms |
22112 KB |
Output is correct |
11 |
Correct |
173 ms |
22748 KB |
Output is correct |
12 |
Correct |
107 ms |
22876 KB |
Output is correct |
13 |
Correct |
97 ms |
22752 KB |
Output is correct |
14 |
Correct |
26 ms |
16740 KB |
Output is correct |
15 |
Correct |
23 ms |
8564 KB |
Output is correct |
16 |
Correct |
182 ms |
22884 KB |
Output is correct |
17 |
Correct |
194 ms |
22752 KB |
Output is correct |
18 |
Correct |
119 ms |
19936 KB |
Output is correct |
19 |
Correct |
79 ms |
22884 KB |
Output is correct |
20 |
Correct |
20 ms |
15228 KB |
Output is correct |
21 |
Correct |
35 ms |
8184 KB |
Output is correct |
22 |
Correct |
265 ms |
22752 KB |
Output is correct |
23 |
Correct |
377 ms |
17280 KB |
Output is correct |
24 |
Correct |
260 ms |
18920 KB |
Output is correct |
25 |
Correct |
215 ms |
22748 KB |
Output is correct |
26 |
Correct |
285 ms |
23264 KB |
Output is correct |
27 |
Correct |
157 ms |
17680 KB |
Output is correct |
28 |
Correct |
281 ms |
18748 KB |
Output is correct |
29 |
Correct |
337 ms |
22516 KB |
Output is correct |
30 |
Correct |
165 ms |
18424 KB |
Output is correct |
31 |
Correct |
83 ms |
18796 KB |
Output is correct |
32 |
Correct |
168 ms |
26728 KB |
Output is correct |
33 |
Correct |
194 ms |
22748 KB |
Output is correct |
34 |
Correct |
45 ms |
16740 KB |
Output is correct |
35 |
Correct |
233 ms |
17192 KB |
Output is correct |