Submission #680939

# Submission time Handle Problem Language Result Execution time Memory
680939 2023-01-12T05:31:08 Z HaiHoang Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
5156 ms 195928 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 2 ms 2772 KB Output is correct
3 Correct 3 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 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 4 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 7 ms 2792 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 5 ms 2644 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 5 ms 2772 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 3 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 6 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 3 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2772 KB Output is correct
29 Correct 4 ms 2772 KB Output is correct
30 Correct 5 ms 2792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 3 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 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 4 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 7 ms 2792 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 5 ms 2644 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 5 ms 2772 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 3 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 6 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 3 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2772 KB Output is correct
29 Correct 4 ms 2772 KB Output is correct
30 Correct 5 ms 2792 KB Output is correct
31 Correct 3 ms 2644 KB Output is correct
32 Correct 3 ms 2644 KB Output is correct
33 Correct 4 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 8 ms 2900 KB Output is correct
37 Correct 10 ms 3080 KB Output is correct
38 Correct 5 ms 2644 KB Output is correct
39 Correct 54 ms 7384 KB Output is correct
40 Correct 13 ms 3984 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 12 ms 3988 KB Output is correct
43 Correct 6 ms 2900 KB Output is correct
44 Correct 6 ms 2772 KB Output is correct
45 Correct 2 ms 2688 KB Output is correct
46 Correct 63 ms 7464 KB Output is correct
47 Correct 11 ms 3412 KB Output is correct
48 Correct 36 ms 7396 KB Output is correct
49 Correct 37 ms 7456 KB Output is correct
50 Correct 3 ms 2644 KB Output is correct
51 Correct 6 ms 2696 KB Output is correct
52 Correct 4 ms 2644 KB Output is correct
53 Correct 24 ms 5204 KB Output is correct
54 Correct 46 ms 7424 KB Output is correct
55 Correct 6 ms 2644 KB Output is correct
56 Correct 4 ms 2644 KB Output is correct
57 Correct 6 ms 2644 KB Output is correct
58 Correct 52 ms 7464 KB Output is correct
59 Correct 2 ms 2684 KB Output is correct
60 Correct 5 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 531 ms 11072 KB Output is correct
2 Correct 8 ms 7124 KB Output is correct
3 Correct 4626 ms 129788 KB Output is correct
4 Correct 2990 ms 65836 KB Output is correct
5 Correct 1359 ms 16552 KB Output is correct
6 Correct 1232 ms 14452 KB Output is correct
7 Correct 1474 ms 17640 KB Output is correct
8 Correct 507 ms 11300 KB Output is correct
9 Correct 1109 ms 14052 KB Output is correct
10 Correct 746 ms 12232 KB Output is correct
11 Correct 4623 ms 134804 KB Output is correct
12 Correct 824 ms 12588 KB Output is correct
13 Correct 2588 ms 43668 KB Output is correct
14 Correct 1421 ms 20528 KB Output is correct
15 Correct 4487 ms 132412 KB Output is correct
16 Correct 256 ms 10656 KB Output is correct
17 Correct 3710 ms 96956 KB Output is correct
18 Correct 8 ms 7380 KB Output is correct
19 Correct 4772 ms 138420 KB Output is correct
20 Correct 1385 ms 19652 KB Output is correct
21 Correct 1267 ms 15608 KB Output is correct
22 Correct 661 ms 11880 KB Output is correct
23 Correct 21 ms 9644 KB Output is correct
24 Correct 3717 ms 114724 KB Output is correct
25 Correct 990 ms 13920 KB Output is correct
26 Correct 455 ms 11220 KB Output is correct
27 Correct 841 ms 13004 KB Output is correct
28 Correct 16 ms 8404 KB Output is correct
29 Correct 1377 ms 17172 KB Output is correct
30 Correct 2115 ms 30736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 3 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 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 4 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 7 ms 2792 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 5 ms 2644 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 5 ms 2772 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 3 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 6 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 3 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 5 ms 2772 KB Output is correct
29 Correct 4 ms 2772 KB Output is correct
30 Correct 5 ms 2792 KB Output is correct
31 Correct 3 ms 2644 KB Output is correct
32 Correct 3 ms 2644 KB Output is correct
33 Correct 4 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 8 ms 2900 KB Output is correct
37 Correct 10 ms 3080 KB Output is correct
38 Correct 5 ms 2644 KB Output is correct
39 Correct 54 ms 7384 KB Output is correct
40 Correct 13 ms 3984 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 12 ms 3988 KB Output is correct
43 Correct 6 ms 2900 KB Output is correct
44 Correct 6 ms 2772 KB Output is correct
45 Correct 2 ms 2688 KB Output is correct
46 Correct 63 ms 7464 KB Output is correct
47 Correct 11 ms 3412 KB Output is correct
48 Correct 36 ms 7396 KB Output is correct
49 Correct 37 ms 7456 KB Output is correct
50 Correct 3 ms 2644 KB Output is correct
51 Correct 6 ms 2696 KB Output is correct
52 Correct 4 ms 2644 KB Output is correct
53 Correct 24 ms 5204 KB Output is correct
54 Correct 46 ms 7424 KB Output is correct
55 Correct 6 ms 2644 KB Output is correct
56 Correct 4 ms 2644 KB Output is correct
57 Correct 6 ms 2644 KB Output is correct
58 Correct 52 ms 7464 KB Output is correct
59 Correct 2 ms 2684 KB Output is correct
60 Correct 5 ms 2772 KB Output is correct
61 Correct 531 ms 11072 KB Output is correct
62 Correct 8 ms 7124 KB Output is correct
63 Correct 4626 ms 129788 KB Output is correct
64 Correct 2990 ms 65836 KB Output is correct
65 Correct 1359 ms 16552 KB Output is correct
66 Correct 1232 ms 14452 KB Output is correct
67 Correct 1474 ms 17640 KB Output is correct
68 Correct 507 ms 11300 KB Output is correct
69 Correct 1109 ms 14052 KB Output is correct
70 Correct 746 ms 12232 KB Output is correct
71 Correct 4623 ms 134804 KB Output is correct
72 Correct 824 ms 12588 KB Output is correct
73 Correct 2588 ms 43668 KB Output is correct
74 Correct 1421 ms 20528 KB Output is correct
75 Correct 4487 ms 132412 KB Output is correct
76 Correct 256 ms 10656 KB Output is correct
77 Correct 3710 ms 96956 KB Output is correct
78 Correct 8 ms 7380 KB Output is correct
79 Correct 4772 ms 138420 KB Output is correct
80 Correct 1385 ms 19652 KB Output is correct
81 Correct 1267 ms 15608 KB Output is correct
82 Correct 661 ms 11880 KB Output is correct
83 Correct 21 ms 9644 KB Output is correct
84 Correct 3717 ms 114724 KB Output is correct
85 Correct 990 ms 13920 KB Output is correct
86 Correct 455 ms 11220 KB Output is correct
87 Correct 841 ms 13004 KB Output is correct
88 Correct 16 ms 8404 KB Output is correct
89 Correct 1377 ms 17172 KB Output is correct
90 Correct 2115 ms 30736 KB Output is correct
91 Correct 964 ms 13588 KB Output is correct
92 Correct 4737 ms 180420 KB Output is correct
93 Correct 1379 ms 20752 KB Output is correct
94 Correct 3604 ms 111496 KB Output is correct
95 Correct 9 ms 7380 KB Output is correct
96 Correct 10 ms 7556 KB Output is correct
97 Correct 1795 ms 28876 KB Output is correct
98 Correct 912 ms 14904 KB Output is correct
99 Correct 1079 ms 16384 KB Output is correct
100 Correct 5156 ms 195120 KB Output is correct
101 Correct 3059 ms 75596 KB Output is correct
102 Correct 2939 ms 75548 KB Output is correct
103 Correct 3733 ms 146908 KB Output is correct
104 Correct 4370 ms 157260 KB Output is correct
105 Correct 503 ms 12776 KB Output is correct
106 Correct 3843 ms 144560 KB Output is correct
107 Correct 2192 ms 41788 KB Output is correct
108 Correct 1279 ms 19048 KB Output is correct
109 Correct 17 ms 9172 KB Output is correct
110 Correct 333 ms 11908 KB Output is correct
111 Correct 1303 ms 19608 KB Output is correct
112 Correct 4909 ms 195928 KB Output is correct
113 Correct 4771 ms 193772 KB Output is correct
114 Correct 1204 ms 18852 KB Output is correct
115 Correct 2845 ms 78720 KB Output is correct
116 Correct 4464 ms 176712 KB Output is correct
117 Correct 3830 ms 144268 KB Output is correct
118 Correct 3636 ms 125860 KB Output is correct
119 Correct 4398 ms 161932 KB Output is correct
120 Correct 3410 ms 97912 KB Output is correct