답안 #475469

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
475469 2021-09-22T15:02:34 Z fredbr Cities (BOI16_cities) C++17
51 / 100
6000 ms 35600 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define clr(x, c) memset((x), (c), sizeof((x)))

using namespace std;

template<class T> void DBG(T&& x) { cerr << x << " "; }
template<class T, class...Args> void DBG(T&& x, Args&&... args) { DBG(x); DBG(args...); }
#define DBG(...) do {cerr << "[" << #__VA_ARGS__ << "]: "; DBG(__VA_ARGS__); cerr << endl; } while (0) 

#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif

template<class num> inline void rd(num& x) {
	char c, neg = 0; while(isspace(c = getchar_unlocked()));
	if(!isdigit(c)) neg = (c == '-'), x = 0;
	else x = c - '0';
	while(isdigit(c = getchar_unlocked())) x = (x << 3) + (x << 1) + c - '0';
	x = neg ? -x : x; }
template <class Ty, class... Args> inline void rd(Ty& x, Args&... args) { rd(x); rd(args...); }


using ll = long long;
using ii = pair<ll, ll>;

using Gr = vector<vector<ii>>;

template<typename T = int>
constexpr T inf = 0x3f3f3f3f;

template<>
constexpr ll inf<ll> = 0x3f3f3f3f3f3f3f3f;

seed_seq seq {
    (uint64_t) chrono::duration_cast<chrono::nanoseconds>(
    	chrono::high_resolution_clock::now().
    	time_since_epoch()).count(),
    (uint64_t) __builtin_ia32_rdtsc(),
    (uint64_t) random_device{}(),
    (uint64_t) 17
};

mt19937 rng{seq};
template<class Ty> Ty randint(Ty a, Ty b) { return uniform_int_distribution<Ty>(a, b)(rng); }

vector<ll> sssp(Gr const& g, int src = 0) {
	int const n = g.size();
	vector<ll> dist(n, inf<ll>);
	vector<char> vis(n);

	priority_queue<ii, vector<ii>, greater<ii>> pq;
	pq.push({0, src});
	dist[src] = 0;

	while (!pq.empty()) {
		auto [d, u] = pq.top();
		pq.pop();

		if (vis[u]) continue;
		vis[u] = 1;

		for (auto [v, w] : g[u]) {
			auto cost = d + w;

			if (cost < dist[v]) {
				dist[v] = cost;
				pq.emplace(cost, v);
			}
		}
	}
	return dist;
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);

	int n, k, m;
	cin >> n >> k >> m;

	vector<int> cities(k);
	for (auto& i : cities) cin >> i;

	Gr g(n+1);
	for (int i = 0; i < m; i++) {
		int a, b, w;
		cin >> a >> b >> w;

		g[a].eb(b, w);
		g[b].eb(a, w);
	}

	if (k <= 2) {
		vector<vector<ll>> d;
		for (auto src : cities) d.push_back(sssp(g, src));

		ll ans = inf<ll>;

		for (int i = 1; i <= n; i++) {
			ll cost = d[0][i] + d[1][i] + (k == 3? d[2][i] : 0);

			ans = min(ans, cost);
		}

		cout << ans << "\n";
	}
	else {
		sort(all(cities));

		ll ans = inf<ll>;
		do {
			if (k == 5) {
				if (cities[0] > cities[1] or cities[3] > cities[4] or cities[0] > cities[3]) continue;
			}

			vector<vector<ll>> d(k);
			for (int i = 0; i < k; i++) d[i] = sssp(g, cities[i]);

			for (int i = 1; i < k; i++) {
				auto ng = g;
				for (int u = 1; u <= n; u++) ng[0].eb(u, d[i-1][u] + d[i][u]);
				d[i] = sssp(ng);
			}

			ans = min(ans, *min_element(d[k-1].begin()+1, d[k-1].end()));

		} while (next_permutation(all(cities)));

		cout << ans << "\n";
	}
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2139 ms 34356 KB Output is correct
2 Correct 2090 ms 34176 KB Output is correct
3 Correct 1142 ms 23780 KB Output is correct
4 Correct 144 ms 15504 KB Output is correct
5 Correct 230 ms 16464 KB Output is correct
6 Correct 75 ms 9340 KB Output is correct
7 Correct 9 ms 560 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 756 KB Output is correct
2 Correct 45 ms 692 KB Output is correct
3 Correct 26 ms 460 KB Output is correct
4 Correct 26 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6073 ms 34664 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6064 ms 35600 KB Time limit exceeded
2 Halted 0 ms 0 KB -