Submission #261259

# Submission time Handle Problem Language Result Execution time Memory
261259 2020-08-11T15:34:57 Z Berted Relay Marathon (NOI20_relaymarathon) C++14
100 / 100
1686 ms 127816 KB
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <bitset>
#define pii pair<int, int>
#define ppi pair<pii, int>
#define ppp pair<pii, pii>
#define fst first
#define snd second
#define vi vector<int>
#define vpi vector<pii>
#define pub push_back
using namespace std;
vector<vpi> adj;
priority_queue<ppi, vector<ppi>, greater<ppi>> pq;
bitset<100001> special;

const int MX = 1000000000;

int n, m, k, visited[100001][2] = {};

//inline int getchar_unlocked() {return getchar();}
inline bool isNum(char c) {return '0' <= c && c <= '9';}
inline int fast_input()
{
	int c = 0, rs = 0;
	while (!isNum(c)) {c = getchar_unlocked();}
	while (isNum(c)) 
	{

		rs = 10 * rs + (c - '0');
		c = getchar_unlocked();
	}
	return rs;
}

inline ppp dijkstra(int s)
{
	int u, c;
	for (int i = 0; i < n; i++) {visited[i][0] = MX;}
	ppp tp = {{-1, -1}, {-1, -1}};
	pq.push({{0, s}, 0});
	while (!pq.empty())
	{
		u = pq.top().fst.snd, c = pq.top().fst.fst; pq.pop();
		if (c <= visited[u][0])
		{
			visited[u][0] = c;
			if (special[u]) 
			{
				if (tp.fst.fst == -1) {tp.fst = {u, c};}
				else if (tp.snd.fst == -1) 
				{
					tp.snd = {u, c}; 
					while (pq.size()) {pq.pop();}
					return tp;
				}
			}
			for (auto v : adj[u])
			{
				 if (c + v.snd < visited[v.fst][0]) 
				 {
				 	visited[v.fst][0] = c + v.snd;
				 	pq.push({{c + v.snd, v.fst}, 0});
				 }
			}
		}
	}
	return tp;
}

inline ppi findShortestPair()
{
	int u, c, p;
	for (int i = 0; i < n; i++) {visited[i][0] = MX; visited[i][1] = -1;}
	ppi rs = {{-1, -1}, MX};
	while (!pq.empty())
	{
		u = pq.top().fst.snd, c = pq.top().fst.fst, p = pq.top().snd; pq.pop(); 
		if (c <= visited[u][0])
		{
			visited[u][0] = c;
			visited[u][1] = p;
			for (auto v : adj[u])
			{
				if (c + v.snd < visited[v.fst][0])
				{
					if (visited[v.fst][0] + c + v.snd < rs.snd && visited[v.fst][1] != -1 && visited[v.fst][1] != p)
					{
						rs.fst.fst = visited[v.fst][1];
						rs.fst.snd = p;
						rs.snd = visited[v.fst][0] + c + v.snd;
					}
					visited[v.fst][0] = c + v.snd;
					visited[v.fst][1] = p;
					pq.push({{c + v.snd, v.fst}, p});
				}
				else if (visited[v.fst][0] + c + v.snd < rs.snd && visited[v.fst][1] != p)
				{
					rs.fst.fst = visited[v.fst][1];
					rs.fst.snd = p;
					rs.snd = visited[v.fst][0] + c + v.snd;
				}
			}
		}
	}
	return rs;
}

int main()
{
	n = fast_input();
	m = fast_input();
	k = fast_input();
	for (int i = 0; i < n; i++) {adj.pub(vpi());}
	for (int i = 0; i < m; i++)
	{
		int u, v, w;
		u = fast_input();
		v = fast_input();
		w = fast_input();
		adj[u - 1].pub({v - 1, w});
		adj[v - 1].pub({u - 1, w});
	}
	for (int i = 0; i < k; i++) 
	{
		int x;
		x = fast_input();
		special[x - 1] = 1;
		//mp[find(x - 1)].pub(x);
		pq.push({{0, x - 1}, x - 1});
	}
	int rs = 1e9;
	ppi rs1 = findShortestPair();
	special[rs1.fst.snd] = 0;
	special[rs1.fst.fst] = 0;
	//cout << rs1.fst.snd << " " << rs1.fst.fst << " " << rs1.snd << "\n";
	ppp o = dijkstra(rs1.fst.fst), s = dijkstra(rs1.fst.snd);
	//cout << o.fst.fst << " " << o.fst.snd << " " << o.snd.fst << " " << o.snd.snd << "\n";
	//cout << s.fst.fst << " " << s.fst.snd << " " << s.snd.fst << " " << s.snd.snd << "\n";
	if (o.fst.fst + 1 && s.fst.fst + 1)
	{
		if (o.fst.fst == s.fst.fst) 
		{
			if (o.snd.fst + 1) rs = min(rs, o.snd.snd + s.fst.snd);
			if (s.snd.fst + 1) rs = min(rs, o.fst.snd + s.snd.snd);
		}
		else {rs = o.fst.snd + s.fst.snd;}
	}
	for (int i = 0; i < n; i++)
	{
		if (special[i]) {pq.push({{0, i}, i});}
	}
	//cout << findShortestPair().fst.fst << " " <<findShortestPair().fst.snd << " " << findShortestPair().snd << "\n";
	rs = min(rs, rs1.snd + findShortestPair().snd);
	printf("%d\n", rs);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 372 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 372 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 404 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 2 ms 640 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 10 ms 3584 KB Output is correct
40 Correct 3 ms 1152 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 3 ms 1156 KB Output is correct
43 Correct 1 ms 512 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 512 KB Output is correct
46 Correct 10 ms 3840 KB Output is correct
47 Correct 2 ms 768 KB Output is correct
48 Correct 9 ms 3456 KB Output is correct
49 Correct 9 ms 3584 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 6 ms 2048 KB Output is correct
54 Correct 11 ms 3584 KB Output is correct
55 Correct 1 ms 384 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 13 ms 3968 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 7392 KB Output is correct
2 Correct 7 ms 4080 KB Output is correct
3 Correct 1232 ms 105380 KB Output is correct
4 Correct 638 ms 48904 KB Output is correct
5 Correct 217 ms 13088 KB Output is correct
6 Correct 164 ms 10724 KB Output is correct
7 Correct 192 ms 13924 KB Output is correct
8 Correct 60 ms 7540 KB Output is correct
9 Correct 116 ms 10080 KB Output is correct
10 Correct 99 ms 8416 KB Output is correct
11 Correct 1375 ms 109848 KB Output is correct
12 Correct 130 ms 8800 KB Output is correct
13 Correct 415 ms 33160 KB Output is correct
14 Correct 216 ms 14124 KB Output is correct
15 Correct 1486 ms 107860 KB Output is correct
16 Correct 65 ms 6880 KB Output is correct
17 Correct 783 ms 75184 KB Output is correct
18 Correct 8 ms 4080 KB Output is correct
19 Correct 1470 ms 111780 KB Output is correct
20 Correct 147 ms 13408 KB Output is correct
21 Correct 183 ms 12260 KB Output is correct
22 Correct 113 ms 8164 KB Output is correct
23 Correct 20 ms 6120 KB Output is correct
24 Correct 1120 ms 86048 KB Output is correct
25 Correct 168 ms 10208 KB Output is correct
26 Correct 96 ms 7528 KB Output is correct
27 Correct 240 ms 9188 KB Output is correct
28 Correct 18 ms 5096 KB Output is correct
29 Correct 246 ms 13652 KB Output is correct
30 Correct 425 ms 24680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 372 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 404 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 2 ms 640 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 10 ms 3584 KB Output is correct
40 Correct 3 ms 1152 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 3 ms 1156 KB Output is correct
43 Correct 1 ms 512 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 512 KB Output is correct
46 Correct 10 ms 3840 KB Output is correct
47 Correct 2 ms 768 KB Output is correct
48 Correct 9 ms 3456 KB Output is correct
49 Correct 9 ms 3584 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 6 ms 2048 KB Output is correct
54 Correct 11 ms 3584 KB Output is correct
55 Correct 1 ms 384 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 13 ms 3968 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 74 ms 7392 KB Output is correct
62 Correct 7 ms 4080 KB Output is correct
63 Correct 1232 ms 105380 KB Output is correct
64 Correct 638 ms 48904 KB Output is correct
65 Correct 217 ms 13088 KB Output is correct
66 Correct 164 ms 10724 KB Output is correct
67 Correct 192 ms 13924 KB Output is correct
68 Correct 60 ms 7540 KB Output is correct
69 Correct 116 ms 10080 KB Output is correct
70 Correct 99 ms 8416 KB Output is correct
71 Correct 1375 ms 109848 KB Output is correct
72 Correct 130 ms 8800 KB Output is correct
73 Correct 415 ms 33160 KB Output is correct
74 Correct 216 ms 14124 KB Output is correct
75 Correct 1486 ms 107860 KB Output is correct
76 Correct 65 ms 6880 KB Output is correct
77 Correct 783 ms 75184 KB Output is correct
78 Correct 8 ms 4080 KB Output is correct
79 Correct 1470 ms 111780 KB Output is correct
80 Correct 147 ms 13408 KB Output is correct
81 Correct 183 ms 12260 KB Output is correct
82 Correct 113 ms 8164 KB Output is correct
83 Correct 20 ms 6120 KB Output is correct
84 Correct 1120 ms 86048 KB Output is correct
85 Correct 168 ms 10208 KB Output is correct
86 Correct 96 ms 7528 KB Output is correct
87 Correct 240 ms 9188 KB Output is correct
88 Correct 18 ms 5096 KB Output is correct
89 Correct 246 ms 13652 KB Output is correct
90 Correct 425 ms 24680 KB Output is correct
91 Correct 212 ms 9688 KB Output is correct
92 Correct 1686 ms 118704 KB Output is correct
93 Correct 238 ms 13920 KB Output is correct
94 Correct 859 ms 70268 KB Output is correct
95 Correct 9 ms 4120 KB Output is correct
96 Correct 19 ms 4208 KB Output is correct
97 Correct 292 ms 18584 KB Output is correct
98 Correct 157 ms 9700 KB Output is correct
99 Correct 119 ms 10552 KB Output is correct
100 Correct 1547 ms 126704 KB Output is correct
101 Correct 691 ms 48328 KB Output is correct
102 Correct 697 ms 47596 KB Output is correct
103 Correct 1156 ms 88836 KB Output is correct
104 Correct 1295 ms 100064 KB Output is correct
105 Correct 92 ms 7664 KB Output is correct
106 Correct 1117 ms 89292 KB Output is correct
107 Correct 456 ms 28280 KB Output is correct
108 Correct 220 ms 12900 KB Output is correct
109 Correct 23 ms 5488 KB Output is correct
110 Correct 77 ms 7136 KB Output is correct
111 Correct 282 ms 13284 KB Output is correct
112 Correct 1662 ms 127064 KB Output is correct
113 Correct 1535 ms 127816 KB Output is correct
114 Correct 192 ms 12768 KB Output is correct
115 Correct 556 ms 47452 KB Output is correct
116 Correct 1151 ms 116240 KB Output is correct
117 Correct 924 ms 86880 KB Output is correct
118 Correct 926 ms 77920 KB Output is correct
119 Correct 1448 ms 103684 KB Output is correct
120 Correct 970 ms 62176 KB Output is correct