#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=100010, N=2005, M=4005;
typedef array<pair<ll, pii>, 5> DATA;
int n, m, T, L, k, u, v, x, y, t, a, b, ans;
int U[M], V[M], W[M], X[MAXN];
ll dist[M][M];
bool mark[M][M];
DATA F[N][N];
vector<int> G[N];
priority_queue<pll, vector<pll>, greater<pll>> pq;
DATA Merge(DATA X, DATA Y){
vector<pair<ll, pii>> vec;
for (int i=0; i<5; i++) for (int j=0; j<5; j++) if (X[i].first+Y[j].first<INF && X[i].second.second!=Y[j].second.first){
int a=X[i].second.first, b=Y[j].second.second;
ll w=X[i].first+Y[j].first;
vec.pb({w, {a, b}});
}
DATA Z=F[0][0];
sort(all(vec));
if (vec.empty()) return Z;
Z[0]=vec[0];
int sz=1, lastx=-1, lasty=-1, tedx=0, tedy=0;
int x=Z[0].second.first, y=Z[0].second.second;
for (int i=1; i<vec.size() && sz<5; i++){
int a=vec[i].second.first, b=vec[i].second.second;
bool okx=(a!=x && b!=lastx && tedx<2);
bool oky=(b!=y && a!=lasty && tedy<2);
if (okx){
Z[sz++]=vec[i];
tedx++;
lastx=b;
}
else if (oky){
Z[sz++]=vec[i];
tedy++;
lasty=a;
}
}
return Z;
}
void print(DATA X){
for (int i=0; i<5 && X[i].first<INF; i++) cerr<<"{"<<X[i].first<<", ("<<X[i].second.first<<", "<<X[i].second.second<<")} ";
cerr<<"\n";
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>m>>T>>L;
m<<=1;
assert(T==1); // 62 points
for (int i=0; i<m; ){
cin>>u>>v>>x;
U[i]=u, V[i]=v, W[i]=x, G[u].pb(i++);
U[i]=v, V[i]=u, W[i]=x, G[v].pb(i++);
}
memset(dist, 63, sizeof(dist));
for (int i=0; i<m; i++){
pq.push({dist[i][i]=0, i});
while (pq.size()){
int x=pq.top().second;
pq.pop();
if (mark[i][x]) continue ;
mark[i][x]=1;
for (int y:G[V[x]]) if (x^y^1 && dist[i][y]>dist[i][x]+W[y])
pq.push({dist[i][y]=dist[i][x]+W[y], y});
}
}
for (int i=0; i<5; i++) F[0][0][i]={INF, {-1, -1}};
for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) F[i][j]=F[0][0];
for (int u=1; u<=n; u++) for (int v=u+1; v<=n; v++){
for (int i:G[u]) for (int j:G[v]) F[u][v][0]=min(F[u][v][0], {W[i]+dist[i][j^1], {i>>1, j>>1}});
int a=F[u][v][0].second.first, b=F[u][v][0].second.second;
for (int i:G[u]) for (int j:G[v]){
if ((i>>1)!=a) F[u][v][1]=min(F[u][v][1], {W[i]+dist[i][j^1], {i>>1, j>>1}});
if ((j>>1)!=b) F[u][v][2]=min(F[u][v][2], {W[i]+dist[i][j^1], {i>>1, j>>1}});
}
int c=F[u][v][1].second.first, d=F[u][v][1].second.second;
int e=F[u][v][2].second.first, f=F[u][v][2].second.second;
for (int i:G[u]) for (int j:G[v]){
if ((i>>1)!=a && (j>>1)!=d) F[u][v][3]=min(F[u][v][3], {W[i]+dist[i][j^1], {i>>1, j>>1}});
if ((i>>1)!=e && (j>>1)!=b) F[u][v][4]=min(F[u][v][4], {W[i]+dist[i][j^1], {i>>1, j>>1}});
}
for (int i=0; i<5; i++) F[v][u][i]={F[u][v][i].first, {F[u][v][i].second.second, F[u][v][i].second.first}};
}
// cerr<<"F[1][2]: ";print(F[1][2]);
// cerr<<"F[1][3]: ";print(F[1][3]);
// cerr<<"F[2][3]: ";print(F[2][3]);
for (int i=1; i<=L; i++) cin>>X[i];
while (T--){
cin>>x>>y;
X[x]=y;
DATA ans=F[X[1]][X[2]];
for (int i=3; i<=L; i++) ans=Merge(ans, F[X[i-1]][X[i]]);
ll res=ans[0].first;
if (res>=INF) res=-1;
cout<<res<<"\n";
}
return 0;
}
/*
3 3 1 3
1 2 1
2 3 1
1 3 1
1
2
3
3 1
*/
Compilation message
wild_boar.cpp: In function 'DATA Merge(DATA, DATA)':
wild_boar.cpp:47:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i=1; i<vec.size() && sz<5; i++){
| ~^~~~~~~~~~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:102:7: warning: unused variable 'c' [-Wunused-variable]
102 | int c=F[u][v][1].second.first, d=F[u][v][1].second.second;
| ^
wild_boar.cpp:103:34: warning: unused variable 'f' [-Wunused-variable]
103 | int e=F[u][v][2].second.first, f=F[u][v][2].second.second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
126060 KB |
Output is correct |
2 |
Correct |
63 ms |
126060 KB |
Output is correct |
3 |
Correct |
62 ms |
126060 KB |
Output is correct |
4 |
Correct |
62 ms |
126060 KB |
Output is correct |
5 |
Correct |
62 ms |
126060 KB |
Output is correct |
6 |
Correct |
63 ms |
126060 KB |
Output is correct |
7 |
Correct |
64 ms |
126060 KB |
Output is correct |
8 |
Correct |
64 ms |
126060 KB |
Output is correct |
9 |
Correct |
62 ms |
126188 KB |
Output is correct |
10 |
Correct |
64 ms |
126188 KB |
Output is correct |
11 |
Correct |
62 ms |
126060 KB |
Output is correct |
12 |
Correct |
63 ms |
126060 KB |
Output is correct |
13 |
Correct |
71 ms |
126060 KB |
Output is correct |
14 |
Correct |
64 ms |
126060 KB |
Output is correct |
15 |
Correct |
64 ms |
126060 KB |
Output is correct |
16 |
Correct |
62 ms |
126060 KB |
Output is correct |
17 |
Correct |
62 ms |
126060 KB |
Output is correct |
18 |
Correct |
63 ms |
126060 KB |
Output is correct |
19 |
Correct |
62 ms |
126060 KB |
Output is correct |
20 |
Correct |
64 ms |
126060 KB |
Output is correct |
21 |
Correct |
62 ms |
126060 KB |
Output is correct |
22 |
Correct |
62 ms |
126084 KB |
Output is correct |
23 |
Correct |
61 ms |
126060 KB |
Output is correct |
24 |
Correct |
62 ms |
126060 KB |
Output is correct |
25 |
Correct |
63 ms |
126060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
126060 KB |
Output is correct |
2 |
Correct |
63 ms |
126060 KB |
Output is correct |
3 |
Correct |
62 ms |
126060 KB |
Output is correct |
4 |
Correct |
62 ms |
126060 KB |
Output is correct |
5 |
Correct |
62 ms |
126060 KB |
Output is correct |
6 |
Correct |
63 ms |
126060 KB |
Output is correct |
7 |
Correct |
64 ms |
126060 KB |
Output is correct |
8 |
Correct |
64 ms |
126060 KB |
Output is correct |
9 |
Correct |
62 ms |
126188 KB |
Output is correct |
10 |
Correct |
64 ms |
126188 KB |
Output is correct |
11 |
Correct |
62 ms |
126060 KB |
Output is correct |
12 |
Correct |
63 ms |
126060 KB |
Output is correct |
13 |
Correct |
71 ms |
126060 KB |
Output is correct |
14 |
Correct |
64 ms |
126060 KB |
Output is correct |
15 |
Correct |
64 ms |
126060 KB |
Output is correct |
16 |
Correct |
62 ms |
126060 KB |
Output is correct |
17 |
Correct |
62 ms |
126060 KB |
Output is correct |
18 |
Correct |
63 ms |
126060 KB |
Output is correct |
19 |
Correct |
62 ms |
126060 KB |
Output is correct |
20 |
Correct |
64 ms |
126060 KB |
Output is correct |
21 |
Correct |
62 ms |
126060 KB |
Output is correct |
22 |
Correct |
62 ms |
126084 KB |
Output is correct |
23 |
Correct |
61 ms |
126060 KB |
Output is correct |
24 |
Correct |
62 ms |
126060 KB |
Output is correct |
25 |
Correct |
63 ms |
126060 KB |
Output is correct |
26 |
Correct |
63 ms |
126444 KB |
Output is correct |
27 |
Correct |
80 ms |
128620 KB |
Output is correct |
28 |
Correct |
79 ms |
128620 KB |
Output is correct |
29 |
Correct |
201 ms |
130284 KB |
Output is correct |
30 |
Correct |
204 ms |
130188 KB |
Output is correct |
31 |
Correct |
183 ms |
130156 KB |
Output is correct |
32 |
Correct |
190 ms |
130156 KB |
Output is correct |
33 |
Correct |
194 ms |
131692 KB |
Output is correct |
34 |
Correct |
194 ms |
131564 KB |
Output is correct |
35 |
Correct |
165 ms |
131308 KB |
Output is correct |
36 |
Correct |
180 ms |
131436 KB |
Output is correct |
37 |
Correct |
195 ms |
131436 KB |
Output is correct |
38 |
Correct |
182 ms |
133100 KB |
Output is correct |
39 |
Correct |
186 ms |
133100 KB |
Output is correct |
40 |
Correct |
185 ms |
133100 KB |
Output is correct |
41 |
Correct |
182 ms |
133100 KB |
Output is correct |
42 |
Correct |
164 ms |
133996 KB |
Output is correct |
43 |
Correct |
170 ms |
135020 KB |
Output is correct |
44 |
Correct |
172 ms |
135020 KB |
Output is correct |
45 |
Correct |
112 ms |
137452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
126060 KB |
Output is correct |
2 |
Correct |
63 ms |
126060 KB |
Output is correct |
3 |
Correct |
62 ms |
126060 KB |
Output is correct |
4 |
Correct |
62 ms |
126060 KB |
Output is correct |
5 |
Correct |
62 ms |
126060 KB |
Output is correct |
6 |
Correct |
63 ms |
126060 KB |
Output is correct |
7 |
Correct |
64 ms |
126060 KB |
Output is correct |
8 |
Correct |
64 ms |
126060 KB |
Output is correct |
9 |
Correct |
62 ms |
126188 KB |
Output is correct |
10 |
Correct |
64 ms |
126188 KB |
Output is correct |
11 |
Correct |
62 ms |
126060 KB |
Output is correct |
12 |
Correct |
63 ms |
126060 KB |
Output is correct |
13 |
Correct |
71 ms |
126060 KB |
Output is correct |
14 |
Correct |
64 ms |
126060 KB |
Output is correct |
15 |
Correct |
64 ms |
126060 KB |
Output is correct |
16 |
Correct |
62 ms |
126060 KB |
Output is correct |
17 |
Correct |
62 ms |
126060 KB |
Output is correct |
18 |
Correct |
63 ms |
126060 KB |
Output is correct |
19 |
Correct |
62 ms |
126060 KB |
Output is correct |
20 |
Correct |
64 ms |
126060 KB |
Output is correct |
21 |
Correct |
62 ms |
126060 KB |
Output is correct |
22 |
Correct |
62 ms |
126084 KB |
Output is correct |
23 |
Correct |
61 ms |
126060 KB |
Output is correct |
24 |
Correct |
62 ms |
126060 KB |
Output is correct |
25 |
Correct |
63 ms |
126060 KB |
Output is correct |
26 |
Correct |
63 ms |
126444 KB |
Output is correct |
27 |
Correct |
80 ms |
128620 KB |
Output is correct |
28 |
Correct |
79 ms |
128620 KB |
Output is correct |
29 |
Correct |
201 ms |
130284 KB |
Output is correct |
30 |
Correct |
204 ms |
130188 KB |
Output is correct |
31 |
Correct |
183 ms |
130156 KB |
Output is correct |
32 |
Correct |
190 ms |
130156 KB |
Output is correct |
33 |
Correct |
194 ms |
131692 KB |
Output is correct |
34 |
Correct |
194 ms |
131564 KB |
Output is correct |
35 |
Correct |
165 ms |
131308 KB |
Output is correct |
36 |
Correct |
180 ms |
131436 KB |
Output is correct |
37 |
Correct |
195 ms |
131436 KB |
Output is correct |
38 |
Correct |
182 ms |
133100 KB |
Output is correct |
39 |
Correct |
186 ms |
133100 KB |
Output is correct |
40 |
Correct |
185 ms |
133100 KB |
Output is correct |
41 |
Correct |
182 ms |
133100 KB |
Output is correct |
42 |
Correct |
164 ms |
133996 KB |
Output is correct |
43 |
Correct |
170 ms |
135020 KB |
Output is correct |
44 |
Correct |
172 ms |
135020 KB |
Output is correct |
45 |
Correct |
112 ms |
137452 KB |
Output is correct |
46 |
Correct |
240 ms |
133996 KB |
Output is correct |
47 |
Correct |
4981 ms |
164376 KB |
Output is correct |
48 |
Correct |
4521 ms |
196004 KB |
Output is correct |
49 |
Correct |
3697 ms |
225024 KB |
Output is correct |
50 |
Correct |
3933 ms |
225068 KB |
Output is correct |
51 |
Correct |
4228 ms |
225016 KB |
Output is correct |
52 |
Correct |
2987 ms |
225260 KB |
Output is correct |
53 |
Correct |
2992 ms |
225136 KB |
Output is correct |
54 |
Correct |
3032 ms |
225392 KB |
Output is correct |
55 |
Correct |
3008 ms |
225260 KB |
Output is correct |
56 |
Correct |
2988 ms |
242028 KB |
Output is correct |
57 |
Correct |
2931 ms |
260332 KB |
Output is correct |
58 |
Correct |
2853 ms |
280428 KB |
Output is correct |
59 |
Correct |
2779 ms |
301844 KB |
Output is correct |
60 |
Correct |
2698 ms |
324940 KB |
Output is correct |
61 |
Correct |
2633 ms |
349692 KB |
Output is correct |
62 |
Correct |
2528 ms |
375904 KB |
Output is correct |
63 |
Correct |
2387 ms |
403632 KB |
Output is correct |
64 |
Incorrect |
855 ms |
456568 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
126060 KB |
Output is correct |
2 |
Correct |
63 ms |
126060 KB |
Output is correct |
3 |
Correct |
62 ms |
126060 KB |
Output is correct |
4 |
Correct |
62 ms |
126060 KB |
Output is correct |
5 |
Correct |
62 ms |
126060 KB |
Output is correct |
6 |
Correct |
63 ms |
126060 KB |
Output is correct |
7 |
Correct |
64 ms |
126060 KB |
Output is correct |
8 |
Correct |
64 ms |
126060 KB |
Output is correct |
9 |
Correct |
62 ms |
126188 KB |
Output is correct |
10 |
Correct |
64 ms |
126188 KB |
Output is correct |
11 |
Correct |
62 ms |
126060 KB |
Output is correct |
12 |
Correct |
63 ms |
126060 KB |
Output is correct |
13 |
Correct |
71 ms |
126060 KB |
Output is correct |
14 |
Correct |
64 ms |
126060 KB |
Output is correct |
15 |
Correct |
64 ms |
126060 KB |
Output is correct |
16 |
Correct |
62 ms |
126060 KB |
Output is correct |
17 |
Correct |
62 ms |
126060 KB |
Output is correct |
18 |
Correct |
63 ms |
126060 KB |
Output is correct |
19 |
Correct |
62 ms |
126060 KB |
Output is correct |
20 |
Correct |
64 ms |
126060 KB |
Output is correct |
21 |
Correct |
62 ms |
126060 KB |
Output is correct |
22 |
Correct |
62 ms |
126084 KB |
Output is correct |
23 |
Correct |
61 ms |
126060 KB |
Output is correct |
24 |
Correct |
62 ms |
126060 KB |
Output is correct |
25 |
Correct |
63 ms |
126060 KB |
Output is correct |
26 |
Correct |
63 ms |
126444 KB |
Output is correct |
27 |
Correct |
80 ms |
128620 KB |
Output is correct |
28 |
Correct |
79 ms |
128620 KB |
Output is correct |
29 |
Correct |
201 ms |
130284 KB |
Output is correct |
30 |
Correct |
204 ms |
130188 KB |
Output is correct |
31 |
Correct |
183 ms |
130156 KB |
Output is correct |
32 |
Correct |
190 ms |
130156 KB |
Output is correct |
33 |
Correct |
194 ms |
131692 KB |
Output is correct |
34 |
Correct |
194 ms |
131564 KB |
Output is correct |
35 |
Correct |
165 ms |
131308 KB |
Output is correct |
36 |
Correct |
180 ms |
131436 KB |
Output is correct |
37 |
Correct |
195 ms |
131436 KB |
Output is correct |
38 |
Correct |
182 ms |
133100 KB |
Output is correct |
39 |
Correct |
186 ms |
133100 KB |
Output is correct |
40 |
Correct |
185 ms |
133100 KB |
Output is correct |
41 |
Correct |
182 ms |
133100 KB |
Output is correct |
42 |
Correct |
164 ms |
133996 KB |
Output is correct |
43 |
Correct |
170 ms |
135020 KB |
Output is correct |
44 |
Correct |
172 ms |
135020 KB |
Output is correct |
45 |
Correct |
112 ms |
137452 KB |
Output is correct |
46 |
Correct |
240 ms |
133996 KB |
Output is correct |
47 |
Correct |
4981 ms |
164376 KB |
Output is correct |
48 |
Correct |
4521 ms |
196004 KB |
Output is correct |
49 |
Correct |
3697 ms |
225024 KB |
Output is correct |
50 |
Correct |
3933 ms |
225068 KB |
Output is correct |
51 |
Correct |
4228 ms |
225016 KB |
Output is correct |
52 |
Correct |
2987 ms |
225260 KB |
Output is correct |
53 |
Correct |
2992 ms |
225136 KB |
Output is correct |
54 |
Correct |
3032 ms |
225392 KB |
Output is correct |
55 |
Correct |
3008 ms |
225260 KB |
Output is correct |
56 |
Correct |
2988 ms |
242028 KB |
Output is correct |
57 |
Correct |
2931 ms |
260332 KB |
Output is correct |
58 |
Correct |
2853 ms |
280428 KB |
Output is correct |
59 |
Correct |
2779 ms |
301844 KB |
Output is correct |
60 |
Correct |
2698 ms |
324940 KB |
Output is correct |
61 |
Correct |
2633 ms |
349692 KB |
Output is correct |
62 |
Correct |
2528 ms |
375904 KB |
Output is correct |
63 |
Correct |
2387 ms |
403632 KB |
Output is correct |
64 |
Incorrect |
855 ms |
456568 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |