#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
struct Edge{
int s, e, i; ll v;
Edge(){}
Edge(int s, int e, int i, ll v): s(s), e(e), i(i), v(v){}
};
struct dat{
int x; ll y;
dat(){}
dat(int x, ll y): x(x), y(y){}
bool operator<(const dat &r)const{
return y>r.y;
}
};
int n, m, k, l;
vector<int> link[2002];
vector<int> revLink[2002];
Edge edge[4002];
int arr[100002];
ll dist[4002][4002];
pair<ll, int> distVec[2002][4002][3];
void init(){
for(int i=0; i<m*2; i++){
for(int j=0; j<m*2; j++) dist[i][j] = 1e18;
priority_queue<dat> pq;
pq.push(dat(i, 0));
while(!pq.empty()){
dat tmp = pq.top(); pq.pop();
if(dist[i][tmp.x] < 1e18) continue;
int xe = tmp.x, x = edge[xe].e;
dist[i][xe] = tmp.y;
for(auto ye: link[x]){
if((xe^ye) == 1) continue;
pq.push(dat(ye, tmp.y + edge[ye].v));
}
}
}
/// ����
for(int i=1; i<=n; i++){
for(int j=0; j<m*2; j++){
distVec[i][j][0] = distVec[i][j][1] = make_pair(1e18, -1);
for(auto c: link[i]){ /// c: i���� ������ ����
distVec[i][j][2] = make_pair(dist[c][j]+edge[c].v, c);
for(int pnt=2; pnt>0; pnt--){
if(distVec[i][j][pnt-1] > distVec[i][j][pnt]){
swap(distVec[i][j][pnt-1], distVec[i][j][pnt]);
}
else break;
}
}
}
}
}
ll DP[2][4002];
void solve(){
for(int i=0; i<m*2; i++) DP[0][i] = DP[1][i] = 1e18;
for(int i=0; i<m*2; i++){
if(edge[i].e != arr[2]) continue;
for(auto s: link[arr[1]]){
DP[0][i] = min(DP[0][i], dist[s][i]+edge[s].v);
}
}
for(int d=3; d<=l; d++){
int b = d%2;
for(int i=0; i<m*2; i++) DP[b][i] = 1e18;
pair<ll, int> vec[3];
vec[0]=vec[1]=vec[2]=make_pair(1e18, -1);
for(int c=0; c<m*2; c++){
if(edge[c].e == arr[d-1]){
vec[2] = make_pair(DP[!b][c], c);
for(int pnt=2; pnt>0; pnt--){
if(vec[pnt-1] > vec[pnt]){
swap(vec[pnt-1], vec[pnt]);
}
else break;
}
}
}
for(int c=0; c<m*2; c++){ /// d�� ���ϴ� ���� ���� ��ȣ
if(edge[c].e != arr[d]) continue;
for(int pp=0; pp < 2; pp++){ /// ��� ������ ���� �����
pair<ll, int> p1 = vec[pp];
if(p1.second<0) continue;
for(int qq = 0; qq < 2; qq++){ /// d�� ���ϴ� �����
pair<ll, int> p2 = distVec[arr[d-1]][c][qq];
if(p2.second<0) continue;
if((p1.second ^ p2.second) == 1) continue;
DP[b][c] = min(DP[b][c], p1.first + p2.first);
}
}
}
}
ll ans = 1e18;
for(int i=0; i<m*2; i++) if(edge[i].e == arr[l]) ans = min(ans, DP[l%2][i]);
printf("%lld\n", ans == 1e18 ? -1 : ans);
}
int main(){
scanf("%d %d %d %d", &n, &m, &k, &l);
for(int i=0; i<m; i++){
scanf("%d %d %lld", &edge[i*2].s, &edge[i*2].e, &edge[i*2].v);
edge[i*2+1] = edge[i*2];
swap(edge[i*2+1].s, edge[i*2+1].e);
edge[i*2].i = i*2, edge[i*2+1].i = i*2+1;
link[edge[i*2].s].push_back(i*2);
link[edge[i*2+1].s].push_back(i*2+1);
revLink[edge[i*2].e].push_back(i*2);
revLink[edge[i*2+1].e].push_back(i*2+1);
}
init();
for(int i=1; i<=l; i++) scanf("%d", &arr[i]);
for(int i=1; i<=k; i++){
int x, y;
scanf("%d %d", &x, &y);
arr[x] = y;
solve();
}
}
Compilation message
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:113:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | scanf("%d %d %d %d", &n, &m, &k, &l);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:115:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d %d %lld", &edge[i*2].s, &edge[i*2].e, &edge[i*2].v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:127:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | for(int i=1; i<=l; i++) scanf("%d", &arr[i]);
| ~~~~~^~~~~~~~~~~~~~~
wild_boar.cpp:130:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
852 KB |
Output is correct |
27 |
Correct |
41 ms |
3296 KB |
Output is correct |
28 |
Correct |
40 ms |
3284 KB |
Output is correct |
29 |
Correct |
317 ms |
9440 KB |
Output is correct |
30 |
Correct |
551 ms |
9416 KB |
Output is correct |
31 |
Correct |
1234 ms |
9528 KB |
Output is correct |
32 |
Correct |
1186 ms |
9412 KB |
Output is correct |
33 |
Correct |
227 ms |
10956 KB |
Output is correct |
34 |
Correct |
230 ms |
11064 KB |
Output is correct |
35 |
Correct |
994 ms |
10968 KB |
Output is correct |
36 |
Correct |
686 ms |
11228 KB |
Output is correct |
37 |
Correct |
229 ms |
10956 KB |
Output is correct |
38 |
Correct |
180 ms |
12564 KB |
Output is correct |
39 |
Correct |
379 ms |
12592 KB |
Output is correct |
40 |
Correct |
183 ms |
12528 KB |
Output is correct |
41 |
Correct |
193 ms |
12492 KB |
Output is correct |
42 |
Correct |
306 ms |
13504 KB |
Output is correct |
43 |
Correct |
154 ms |
14180 KB |
Output is correct |
44 |
Correct |
154 ms |
14176 KB |
Output is correct |
45 |
Correct |
115 ms |
15812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
852 KB |
Output is correct |
27 |
Correct |
41 ms |
3296 KB |
Output is correct |
28 |
Correct |
40 ms |
3284 KB |
Output is correct |
29 |
Correct |
317 ms |
9440 KB |
Output is correct |
30 |
Correct |
551 ms |
9416 KB |
Output is correct |
31 |
Correct |
1234 ms |
9528 KB |
Output is correct |
32 |
Correct |
1186 ms |
9412 KB |
Output is correct |
33 |
Correct |
227 ms |
10956 KB |
Output is correct |
34 |
Correct |
230 ms |
11064 KB |
Output is correct |
35 |
Correct |
994 ms |
10968 KB |
Output is correct |
36 |
Correct |
686 ms |
11228 KB |
Output is correct |
37 |
Correct |
229 ms |
10956 KB |
Output is correct |
38 |
Correct |
180 ms |
12564 KB |
Output is correct |
39 |
Correct |
379 ms |
12592 KB |
Output is correct |
40 |
Correct |
183 ms |
12528 KB |
Output is correct |
41 |
Correct |
193 ms |
12492 KB |
Output is correct |
42 |
Correct |
306 ms |
13504 KB |
Output is correct |
43 |
Correct |
154 ms |
14180 KB |
Output is correct |
44 |
Correct |
154 ms |
14176 KB |
Output is correct |
45 |
Correct |
115 ms |
15812 KB |
Output is correct |
46 |
Correct |
498 ms |
22544 KB |
Output is correct |
47 |
Execution timed out |
18020 ms |
14496 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
852 KB |
Output is correct |
27 |
Correct |
41 ms |
3296 KB |
Output is correct |
28 |
Correct |
40 ms |
3284 KB |
Output is correct |
29 |
Correct |
317 ms |
9440 KB |
Output is correct |
30 |
Correct |
551 ms |
9416 KB |
Output is correct |
31 |
Correct |
1234 ms |
9528 KB |
Output is correct |
32 |
Correct |
1186 ms |
9412 KB |
Output is correct |
33 |
Correct |
227 ms |
10956 KB |
Output is correct |
34 |
Correct |
230 ms |
11064 KB |
Output is correct |
35 |
Correct |
994 ms |
10968 KB |
Output is correct |
36 |
Correct |
686 ms |
11228 KB |
Output is correct |
37 |
Correct |
229 ms |
10956 KB |
Output is correct |
38 |
Correct |
180 ms |
12564 KB |
Output is correct |
39 |
Correct |
379 ms |
12592 KB |
Output is correct |
40 |
Correct |
183 ms |
12528 KB |
Output is correct |
41 |
Correct |
193 ms |
12492 KB |
Output is correct |
42 |
Correct |
306 ms |
13504 KB |
Output is correct |
43 |
Correct |
154 ms |
14180 KB |
Output is correct |
44 |
Correct |
154 ms |
14176 KB |
Output is correct |
45 |
Correct |
115 ms |
15812 KB |
Output is correct |
46 |
Correct |
498 ms |
22544 KB |
Output is correct |
47 |
Execution timed out |
18020 ms |
14496 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |