Submission #743699

# Submission time Handle Problem Language Result Execution time Memory
743699 2023-05-17T17:57:15 Z fanwen Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
5546 ms 209804 KB
/**
*   NAME : PHAM VAN SAM
*   DATE : 12.01.2023 08:03:57
*  ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
*  │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
*  └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
*  ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
*  │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │Num│ / │ * │ - │
*  ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
*  │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
*  ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
*  │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
*  ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
*  │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
*  ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
*  │ Ctrl│ Win│ Alt│         Space         │ Alt│ Win│Menu│Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
*  └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
**/
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define int long long
#define MASK(x) (1LL << (x))
#define BIT(x, k) (((x) >> (k)) & 1LL)
#define all(x) (x).begin(),(x).end()
#define For(i, a, b) for (int i = (a); i <= (b); ++i)
#define Fod(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Fore(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(TASK) if(fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); }
 
template <class U, class V> bool maximize(U &A, const V &B){ return (A < B) ? (A = B, true) : false;}
template <class U, class V> bool minimize(U &A, const V &B){ return (A > B) ? (A = B, true) : false;}
 
using namespace std;
 
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) { return l + rnd() % (r - l + 1); }
const int MAXN = 1e5 + 5;
int N, M, K, d[MAXN];
vector <pair <int, int>> ke[MAXN];
vector <int> special;
bool dd[MAXN];
namespace sub1 {
	int dp[55][55];
	void dijkstra (int u) {
		priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> heap;
		fill(dp[u] + 1, dp[u] + N + 1, 2e9);
		dp[u][u] = 0;
		heap.push(make_pair(dp[u][u], u));
		while(heap.size()) {
			int x = heap.top().second;
			int w1 = heap.top().first;
			heap.pop();
			if(dp[u][x] != w1) continue;
			for (auto [v, w] : ke[x]) if(minimize(dp[u][v], dp[u][x] + w)) {
				heap.push(make_pair(dp[u][v], v));
			}
		}
	}
	void Main() {
		int ans = 2e9;
		for (auto u : special) dijkstra(u);
		rep (i, (int) special.size()) {
			rep (j, (int) special.size()) {
				For (u, i + 1, (int) special.size() - 1) {
					if(dp[special[i]][special[u]] == 2e9) continue;
					For (v, j + 1, (int) special.size() - 1){
						if(dp[special[j]][special[v]] == 2e9) continue;
						if(i != v && i != j && u != j && u != v) {
							minimize(ans, dp[special[i]][special[u]] + dp[special[j]][special[v]]);
						}
					}
				}
			}
		}
		cout << ans << '\n';
	}
}
namespace LieuAnNhieu{
	int trace[MAXN];
	struct SegmentTree {
		vector <int> it;
		SegmentTree() = default;
		SegmentTree(int n) : it(4 * n + 5, 2e9) {}
		void update(int idx, int l, int r, int u, int x) {
			if(l > u || r < u) return;
			if(l == r) return void(minimize(it[idx], x));
			int mid = (l + r) >> 1;
			update(idx << 1, l, mid, u, x);
			update(idx << 1 | 1, mid + 1, r, u, x);
			it[idx] = min(it[idx << 1], it[idx << 1 | 1]);
		}
		int get(int idx, int l, int r, int u, int v) {
			if(l > v || r < u) return 2e9;
			if(l >= u && r <= v) return it[idx];
			int mid = (l + r) >> 1;
			return min(get(idx << 1, l, mid, u, v), get(idx << 1 | 1, mid + 1, r, u, v));
		}
	};
	struct Data {
		int u, v, w;
		Data(int u, int v, int w) {
			this-> u = u;
			this-> v = v;
			this-> w = w;
		}
		friend bool operator < (const Data &A, const Data &B) {
			return (A.w < B.w || (A.w == B.w && A.u < B.u));
		}
		friend bool operator == (const Data &A, const Data &B) {
			return (A.w == B.w && A.u == B.u && A.v == B.v);
		}
	};
	vector <Data> val;
	void dijkstra(int bit, int type) {
		fill(d + 1, d + N + 1, 1e9);
		priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> heap;
		for (auto x : special) if(BIT(x, bit) == type) {
			d[x] = 0;
			trace[x] = x;
			heap.push(make_pair(0, x));
		}
		while(heap.size()) {
			int u = heap.top().second;
			int w1 = heap.top().first;
			heap.pop();
			if(d[u] != w1) continue;
			for (auto [v, w] : ke[u]) if(minimize(d[v], d[u] + w)) {
				heap.push(make_pair(d[v], v));
				trace[v] = trace[u];
			}
		}
		for (auto x : special) {
			if(BIT(x, bit) == type || d[x] == 1e9) continue;
			val.push_back(Data(min(x, trace[x]), max(x, trace[x]), d[x]));
		}
		sort (val.begin(), val.end());
		val.erase(unique(val.begin(), val.end()), val.end());
		while(val.size() > K) val.pop_back();
	}
	void lieu_thi_an_nhieu() {
		For (i, 0, 16) {
			dijkstra(i, 0);
			dijkstra(i, 1);
		}
		// return;
		SegmentTree IT(N);
		int ans = 2e9;
		int j = 0;
		sort (val.begin(), val.end(), [](Data A, Data B) {
			return A.u < B.u;
		});
		For (i, 0, (int) val.size()) {
			while(j < i && val[j].u < val[i].u) {
				IT.update(1, 1, N, val[j].v, val[j].w);
				j++;
			}
			minimize(ans, val[i].w + min({IT.get(1, 1, N, 1, val[i].u - 1), 
										  IT.get(1, 1, N, val[i].u + 1, val[i].v - 1), 
										  IT.get(1, 1, N, val[i].v + 1, N)}));
		}
		cout << ans << '\n';
	}
}
void solve(void) {
    cin >> N >> M >> K;
    while(M--) {
    	int u, v, w; cin >> u >> v >> w;
    	ke[u].push_back(make_pair(v, w));
    	ke[v].push_back(make_pair(u, w));
    }
    For (i, 1, K) {
    	int x; cin >> x; special.push_back(x);
    }
    if(N <= 50) sub1::Main();
    else LieuAnNhieu::lieu_thi_an_nhieu();
}
signed main() {
    file("4SPECIAL");
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int test; test = 1; 
    // cin >> test;
    while(test--) { 
        solve();
        cout << '\n';
    }
    // cerr << "Time elapsed: " << TIME << " s.\n";
    return (0 ^ 0);
}

Compilation message

RelayMarathon.cpp: In function 'void LieuAnNhieu::dijkstra(long long int, long long int)':
RelayMarathon.cpp:141:20: warning: comparison of integer expressions of different signedness: 'std::vector<LieuAnNhieu::Data>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  141 |   while(val.size() > K) val.pop_back();
      |         ~~~~~~~~~~~^~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:31:56: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 | #define file(TASK) if(fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); }
      |                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:181:5: note: in expansion of macro 'file'
  181 |     file("4SPECIAL");
      |     ^~~~
RelayMarathon.cpp:31:89: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 | #define file(TASK) if(fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); }
      |                                                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:181:5: note: in expansion of macro 'file'
  181 |     file("4SPECIAL");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2692 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 2684 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2692 KB Output is correct
8 Correct 3 ms 2700 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 2 ms 2696 KB Output is correct
15 Correct 7 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 7 ms 2772 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 6 ms 2812 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 5 ms 2688 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2616 KB Output is correct
26 Correct 2 ms 2684 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2692 KB Output is correct
29 Correct 3 ms 2696 KB Output is correct
30 Correct 4 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2692 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 2684 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2692 KB Output is correct
8 Correct 3 ms 2700 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 2 ms 2696 KB Output is correct
15 Correct 7 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 7 ms 2772 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 6 ms 2812 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 5 ms 2688 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2616 KB Output is correct
26 Correct 2 ms 2684 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2692 KB Output is correct
29 Correct 3 ms 2696 KB Output is correct
30 Correct 4 ms 2900 KB Output is correct
31 Correct 3 ms 2724 KB Output is correct
32 Correct 3 ms 2644 KB Output is correct
33 Correct 4 ms 2644 KB Output is correct
34 Correct 3 ms 2644 KB Output is correct
35 Correct 3 ms 2688 KB Output is correct
36 Correct 7 ms 3024 KB Output is correct
37 Correct 9 ms 3164 KB Output is correct
38 Correct 4 ms 2772 KB Output is correct
39 Correct 38 ms 8060 KB Output is correct
40 Correct 13 ms 3924 KB Output is correct
41 Correct 4 ms 2772 KB Output is correct
42 Correct 12 ms 3924 KB Output is correct
43 Correct 6 ms 2824 KB Output is correct
44 Correct 4 ms 2772 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 38 ms 8476 KB Output is correct
47 Correct 11 ms 3572 KB Output is correct
48 Correct 29 ms 8072 KB Output is correct
49 Correct 30 ms 8136 KB Output is correct
50 Correct 3 ms 2700 KB Output is correct
51 Correct 4 ms 2772 KB Output is correct
52 Correct 4 ms 2644 KB Output is correct
53 Correct 22 ms 5528 KB Output is correct
54 Correct 34 ms 8176 KB Output is correct
55 Correct 3 ms 2644 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 3 ms 2644 KB Output is correct
58 Correct 42 ms 8584 KB Output is correct
59 Correct 3 ms 2644 KB Output is correct
60 Correct 6 ms 2828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 464 ms 12164 KB Output is correct
2 Correct 8 ms 7252 KB Output is correct
3 Correct 4750 ms 167964 KB Output is correct
4 Correct 3113 ms 80696 KB Output is correct
5 Correct 1465 ms 19144 KB Output is correct
6 Correct 1169 ms 16464 KB Output is correct
7 Correct 1516 ms 20684 KB Output is correct
8 Correct 538 ms 12524 KB Output is correct
9 Correct 1057 ms 15640 KB Output is correct
10 Correct 806 ms 13568 KB Output is correct
11 Correct 4734 ms 174916 KB Output is correct
12 Correct 893 ms 14144 KB Output is correct
13 Correct 2678 ms 53020 KB Output is correct
14 Correct 1505 ms 23552 KB Output is correct
15 Correct 4554 ms 171800 KB Output is correct
16 Correct 328 ms 11608 KB Output is correct
17 Correct 3909 ms 121176 KB Output is correct
18 Correct 11 ms 7508 KB Output is correct
19 Correct 5318 ms 179176 KB Output is correct
20 Correct 1576 ms 22408 KB Output is correct
21 Correct 1325 ms 18044 KB Output is correct
22 Correct 787 ms 13188 KB Output is correct
23 Correct 31 ms 10320 KB Output is correct
24 Correct 4361 ms 141880 KB Output is correct
25 Correct 1145 ms 15744 KB Output is correct
26 Correct 611 ms 12312 KB Output is correct
27 Correct 1000 ms 14312 KB Output is correct
28 Correct 16 ms 8660 KB Output is correct
29 Correct 1516 ms 20052 KB Output is correct
30 Correct 2244 ms 37048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2692 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 2684 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2692 KB Output is correct
8 Correct 3 ms 2700 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 2 ms 2696 KB Output is correct
15 Correct 7 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 7 ms 2772 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 6 ms 2812 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 5 ms 2688 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2616 KB Output is correct
26 Correct 2 ms 2684 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2692 KB Output is correct
29 Correct 3 ms 2696 KB Output is correct
30 Correct 4 ms 2900 KB Output is correct
31 Correct 3 ms 2724 KB Output is correct
32 Correct 3 ms 2644 KB Output is correct
33 Correct 4 ms 2644 KB Output is correct
34 Correct 3 ms 2644 KB Output is correct
35 Correct 3 ms 2688 KB Output is correct
36 Correct 7 ms 3024 KB Output is correct
37 Correct 9 ms 3164 KB Output is correct
38 Correct 4 ms 2772 KB Output is correct
39 Correct 38 ms 8060 KB Output is correct
40 Correct 13 ms 3924 KB Output is correct
41 Correct 4 ms 2772 KB Output is correct
42 Correct 12 ms 3924 KB Output is correct
43 Correct 6 ms 2824 KB Output is correct
44 Correct 4 ms 2772 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 38 ms 8476 KB Output is correct
47 Correct 11 ms 3572 KB Output is correct
48 Correct 29 ms 8072 KB Output is correct
49 Correct 30 ms 8136 KB Output is correct
50 Correct 3 ms 2700 KB Output is correct
51 Correct 4 ms 2772 KB Output is correct
52 Correct 4 ms 2644 KB Output is correct
53 Correct 22 ms 5528 KB Output is correct
54 Correct 34 ms 8176 KB Output is correct
55 Correct 3 ms 2644 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 3 ms 2644 KB Output is correct
58 Correct 42 ms 8584 KB Output is correct
59 Correct 3 ms 2644 KB Output is correct
60 Correct 6 ms 2828 KB Output is correct
61 Correct 464 ms 12164 KB Output is correct
62 Correct 8 ms 7252 KB Output is correct
63 Correct 4750 ms 167964 KB Output is correct
64 Correct 3113 ms 80696 KB Output is correct
65 Correct 1465 ms 19144 KB Output is correct
66 Correct 1169 ms 16464 KB Output is correct
67 Correct 1516 ms 20684 KB Output is correct
68 Correct 538 ms 12524 KB Output is correct
69 Correct 1057 ms 15640 KB Output is correct
70 Correct 806 ms 13568 KB Output is correct
71 Correct 4734 ms 174916 KB Output is correct
72 Correct 893 ms 14144 KB Output is correct
73 Correct 2678 ms 53020 KB Output is correct
74 Correct 1505 ms 23552 KB Output is correct
75 Correct 4554 ms 171800 KB Output is correct
76 Correct 328 ms 11608 KB Output is correct
77 Correct 3909 ms 121176 KB Output is correct
78 Correct 11 ms 7508 KB Output is correct
79 Correct 5318 ms 179176 KB Output is correct
80 Correct 1576 ms 22408 KB Output is correct
81 Correct 1325 ms 18044 KB Output is correct
82 Correct 787 ms 13188 KB Output is correct
83 Correct 31 ms 10320 KB Output is correct
84 Correct 4361 ms 141880 KB Output is correct
85 Correct 1145 ms 15744 KB Output is correct
86 Correct 611 ms 12312 KB Output is correct
87 Correct 1000 ms 14312 KB Output is correct
88 Correct 16 ms 8660 KB Output is correct
89 Correct 1516 ms 20052 KB Output is correct
90 Correct 2244 ms 37048 KB Output is correct
91 Correct 950 ms 15244 KB Output is correct
92 Correct 4766 ms 189476 KB Output is correct
93 Correct 1500 ms 20680 KB Output is correct
94 Correct 3893 ms 111652 KB Output is correct
95 Correct 8 ms 7252 KB Output is correct
96 Correct 9 ms 7636 KB Output is correct
97 Correct 1974 ms 28892 KB Output is correct
98 Correct 1045 ms 15052 KB Output is correct
99 Correct 1180 ms 16328 KB Output is correct
100 Correct 5546 ms 205928 KB Output is correct
101 Correct 3182 ms 75424 KB Output is correct
102 Correct 3212 ms 75684 KB Output is correct
103 Correct 3890 ms 147252 KB Output is correct
104 Correct 4579 ms 161780 KB Output is correct
105 Correct 639 ms 12600 KB Output is correct
106 Correct 4140 ms 145016 KB Output is correct
107 Correct 2409 ms 41652 KB Output is correct
108 Correct 1423 ms 19040 KB Output is correct
109 Correct 17 ms 9172 KB Output is correct
110 Correct 366 ms 12052 KB Output is correct
111 Correct 1484 ms 19868 KB Output is correct
112 Correct 5208 ms 208424 KB Output is correct
113 Correct 5197 ms 209804 KB Output is correct
114 Correct 1325 ms 18872 KB Output is correct
115 Correct 3065 ms 78804 KB Output is correct
116 Correct 4887 ms 185372 KB Output is correct
117 Correct 4027 ms 144272 KB Output is correct
118 Correct 3974 ms 126036 KB Output is correct
119 Correct 4765 ms 167696 KB Output is correct
120 Correct 4122 ms 97912 KB Output is correct