답안 #676492

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676492 2022-12-31T03:39:59 Z penguin133 Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
1776 ms 203260 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second

priority_queue<pi, vector<pi>, greater<pi> > pq;
int n,m,k;
vector<pi>v[100005];
int A[100005], dist[100005], stor[100005], stor2[100005], dist2[100005], dist3[100005], dist4[100005];
vector<pi>v2;

main(){
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m >> k;
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin >> x >>y >> z;
		v[x].push_back({y, z});
		v[y].push_back({x, z});
	}
	for(int i=1;i<=n;i++)dist[i] = dist2[i] = dist3[i] = dist4[i] = 1e9;
	for(int i=1;i<=k;i++){
		cin >> A[i];
		pq.push({0, A[i]}), dist[A[i]] = 0, stor[A[i]] = i;
	}
	while(!pq.empty()){
		int x = pq.top().se, y = pq.top().fi;
		pq.pop();
		if(dist[x] < y)continue;
		for(auto a : v[x]){
			int i = a.fi, j = a.se;
			if(dist[i] > j + y)dist[i] = j + y, pq.push({j+y, i}), stor[i] = stor[x];
		}
	}
	int ans = 1e18;
	int in, in2;
	for(int i=1;i<=n;i++){
		for(auto j : v[i]){
			if(stor[i] != stor[j.fi]){
				if(ans > dist[i] + dist[j.fi] + j.se)ans = dist[i] + dist[j.fi] + j.se, in = stor[i], in2 = stor[j.fi];
			}
		}
	}
	for(int i=1;i<=k;i++){
		if(i == in || i == in2)continue;
		pq.push({0, A[i]}), dist2[A[i]] = 0, stor2[A[i]] = i;
	}
	
	
	while(!pq.empty()){
		int x = pq.top().se, y = pq.top().fi;
		pq.pop();
		if(dist2[x] < y)continue;
		for(auto a : v[x]){
			int i = a.fi, j = a.se;
			if(dist2[i] > j + y)dist2[i] = j + y, pq.push({j+y, i}), stor2[i] = stor2[x];
		}
	}
	int ans2 = 1e18;
	for(int i=1;i<=n;i++){
		for(auto j : v[i])if(stor2[i] != stor2[j.fi])ans2 = min(ans2, dist2[i] + dist2[j.fi] + j.se);
	}
	pq.push({0, A[in]});
	while(!pq.empty()){
		int x = pq.top().se, y = pq.top().fi;
		pq.pop();
		if(dist3[x] < y)continue;
		for(auto a : v[x]){
			int i = a.fi, j = a.se;
			if(dist3[i] > j + y)dist3[i] = j + y, pq.push({j+y, i});
		}
	}
	pq.push({0, A[in2]});
	while(!pq.empty()){
		int x = pq.top().se, y = pq.top().fi;
		pq.pop();
		if(dist4[x] < y)continue;
		for(auto a : v[x]){
			int i = a.fi, j = a.se;
			if(dist4[i] > j + y)dist4[i] = j + y, pq.push({j+y, i});
		}
	}
	int ans3 = 1e18;
	int in3, mini = 1e18;
	
	for(int i=1;i<=k;i++){
		if(i == in || i == in2)continue;
		v2.push_back({dist3[A[i]], A[i]});
	}
	sort(v2.begin(), v2.end());
	for(int i=1;i<=k;i++){
		if(i == in || i == in2)continue;
		if(A[i] == v2[0].se){
			ans3 = min(ans3, v2[1].fi + dist4[v2[0].se]);
		}
		else ans3 = min(ans3, v2[0].fi + dist4[A[i]]);
	}
	//cout << ans + ans2 << " " << ans3 << " " << in << " " << in2 << '\n';
	cout << min(ans + ans2, ans3);
}

Compilation message

RelayMarathon.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   16 | main(){
      | ^~~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:88:6: warning: unused variable 'in3' [-Wunused-variable]
   88 |  int in3, mini = 1e18;
      |      ^~~
RelayMarathon.cpp:88:11: warning: unused variable 'mini' [-Wunused-variable]
   88 |  int in3, mini = 1e18;
      |           ^~~~
RelayMarathon.cpp:49:19: warning: 'in2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |   if(i == in || i == in2)continue;
      |                 ~~^~~~~~
RelayMarathon.cpp:49:8: warning: 'in' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |   if(i == in || i == in2)continue;
      |      ~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2672 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2684 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2672 KB Output is correct
19 Correct 2 ms 2784 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2680 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2672 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2688 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2672 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2684 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2672 KB Output is correct
19 Correct 2 ms 2784 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2680 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2672 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2688 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2672 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 2940 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 22 ms 8120 KB Output is correct
40 Correct 7 ms 3924 KB Output is correct
41 Correct 3 ms 2900 KB Output is correct
42 Correct 7 ms 3932 KB Output is correct
43 Correct 3 ms 2900 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 32 ms 8492 KB Output is correct
47 Correct 5 ms 3412 KB Output is correct
48 Correct 20 ms 8124 KB Output is correct
49 Correct 23 ms 8188 KB Output is correct
50 Correct 2 ms 2772 KB Output is correct
51 Correct 2 ms 2808 KB Output is correct
52 Correct 2 ms 2772 KB Output is correct
53 Correct 17 ms 5592 KB Output is correct
54 Correct 23 ms 8264 KB Output is correct
55 Correct 2 ms 2772 KB Output is correct
56 Correct 2 ms 2640 KB Output is correct
57 Correct 2 ms 2772 KB Output is correct
58 Correct 27 ms 8580 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 3 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 12236 KB Output is correct
2 Correct 7 ms 6652 KB Output is correct
3 Correct 1268 ms 164164 KB Output is correct
4 Correct 602 ms 76804 KB Output is correct
5 Correct 174 ms 19792 KB Output is correct
6 Correct 127 ms 16868 KB Output is correct
7 Correct 183 ms 21436 KB Output is correct
8 Correct 64 ms 12568 KB Output is correct
9 Correct 114 ms 15912 KB Output is correct
10 Correct 81 ms 13640 KB Output is correct
11 Correct 1350 ms 170176 KB Output is correct
12 Correct 97 ms 14292 KB Output is correct
13 Correct 386 ms 51088 KB Output is correct
14 Correct 187 ms 21948 KB Output is correct
15 Correct 1354 ms 166980 KB Output is correct
16 Correct 48 ms 11552 KB Output is correct
17 Correct 953 ms 119736 KB Output is correct
18 Correct 7 ms 6932 KB Output is correct
19 Correct 1432 ms 176348 KB Output is correct
20 Correct 210 ms 20760 KB Output is correct
21 Correct 168 ms 18652 KB Output is correct
22 Correct 94 ms 13368 KB Output is correct
23 Correct 22 ms 10216 KB Output is correct
24 Correct 950 ms 135472 KB Output is correct
25 Correct 125 ms 16196 KB Output is correct
26 Correct 64 ms 12408 KB Output is correct
27 Correct 98 ms 14748 KB Output is correct
28 Correct 13 ms 8276 KB Output is correct
29 Correct 179 ms 20796 KB Output is correct
30 Correct 324 ms 37836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2672 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2684 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2672 KB Output is correct
19 Correct 2 ms 2784 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2680 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2672 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2688 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2672 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 2940 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 22 ms 8120 KB Output is correct
40 Correct 7 ms 3924 KB Output is correct
41 Correct 3 ms 2900 KB Output is correct
42 Correct 7 ms 3932 KB Output is correct
43 Correct 3 ms 2900 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 32 ms 8492 KB Output is correct
47 Correct 5 ms 3412 KB Output is correct
48 Correct 20 ms 8124 KB Output is correct
49 Correct 23 ms 8188 KB Output is correct
50 Correct 2 ms 2772 KB Output is correct
51 Correct 2 ms 2808 KB Output is correct
52 Correct 2 ms 2772 KB Output is correct
53 Correct 17 ms 5592 KB Output is correct
54 Correct 23 ms 8264 KB Output is correct
55 Correct 2 ms 2772 KB Output is correct
56 Correct 2 ms 2640 KB Output is correct
57 Correct 2 ms 2772 KB Output is correct
58 Correct 27 ms 8580 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 3 ms 2772 KB Output is correct
61 Correct 64 ms 12236 KB Output is correct
62 Correct 7 ms 6652 KB Output is correct
63 Correct 1268 ms 164164 KB Output is correct
64 Correct 602 ms 76804 KB Output is correct
65 Correct 174 ms 19792 KB Output is correct
66 Correct 127 ms 16868 KB Output is correct
67 Correct 183 ms 21436 KB Output is correct
68 Correct 64 ms 12568 KB Output is correct
69 Correct 114 ms 15912 KB Output is correct
70 Correct 81 ms 13640 KB Output is correct
71 Correct 1350 ms 170176 KB Output is correct
72 Correct 97 ms 14292 KB Output is correct
73 Correct 386 ms 51088 KB Output is correct
74 Correct 187 ms 21948 KB Output is correct
75 Correct 1354 ms 166980 KB Output is correct
76 Correct 48 ms 11552 KB Output is correct
77 Correct 953 ms 119736 KB Output is correct
78 Correct 7 ms 6932 KB Output is correct
79 Correct 1432 ms 176348 KB Output is correct
80 Correct 210 ms 20760 KB Output is correct
81 Correct 168 ms 18652 KB Output is correct
82 Correct 94 ms 13368 KB Output is correct
83 Correct 22 ms 10216 KB Output is correct
84 Correct 950 ms 135472 KB Output is correct
85 Correct 125 ms 16196 KB Output is correct
86 Correct 64 ms 12408 KB Output is correct
87 Correct 98 ms 14748 KB Output is correct
88 Correct 13 ms 8276 KB Output is correct
89 Correct 179 ms 20796 KB Output is correct
90 Correct 324 ms 37836 KB Output is correct
91 Correct 170 ms 15472 KB Output is correct
92 Correct 1652 ms 188688 KB Output is correct
93 Correct 267 ms 21616 KB Output is correct
94 Correct 1044 ms 110640 KB Output is correct
95 Correct 7 ms 6612 KB Output is correct
96 Correct 8 ms 6996 KB Output is correct
97 Correct 327 ms 28664 KB Output is correct
98 Correct 154 ms 15308 KB Output is correct
99 Correct 125 ms 16704 KB Output is correct
100 Correct 1732 ms 202408 KB Output is correct
101 Correct 736 ms 75056 KB Output is correct
102 Correct 749 ms 74420 KB Output is correct
103 Correct 1178 ms 142496 KB Output is correct
104 Correct 1445 ms 159440 KB Output is correct
105 Correct 127 ms 12684 KB Output is correct
106 Correct 1239 ms 141996 KB Output is correct
107 Correct 466 ms 41364 KB Output is correct
108 Correct 236 ms 19636 KB Output is correct
109 Correct 16 ms 8708 KB Output is correct
110 Correct 73 ms 12024 KB Output is correct
111 Correct 251 ms 20340 KB Output is correct
112 Correct 1776 ms 200972 KB Output is correct
113 Correct 1687 ms 203260 KB Output is correct
114 Correct 237 ms 19484 KB Output is correct
115 Correct 661 ms 74472 KB Output is correct
116 Correct 1690 ms 183948 KB Output is correct
117 Correct 1158 ms 139596 KB Output is correct
118 Correct 1064 ms 122820 KB Output is correct
119 Correct 1554 ms 163332 KB Output is correct
120 Correct 998 ms 96964 KB Output is correct