Submission #485514

# Submission time Handle Problem Language Result Execution time Memory
485514 2021-11-08T03:18:11 Z hollwo_pelw Cities (BOI16_cities) C++17
100 / 100
2622 ms 67988 KB
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
	if (fopen(filein.c_str(), "r")){
		freopen(filein.c_str(), "r", stdin);
		freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
		freopen(fileerr.c_str(), "w", stderr);
#endif
	}
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}

void Hollwo_Pelw();

signed main(){
#ifdef hollwo_pelw_local
	FAST_IO("input.inp", "output.out", "error.err");
	auto start = chrono::steady_clock::now();
#else
	FAST_IO("BOI16_cities.inp", "BOI16_cities.out");
#endif
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
	return 0;
}

const int N = 2e5 + 5, K = 5;

int n, k, m, node[K];
int64_t dp[1 << K][N];

vector<pair<int, int>> adj[N];  

struct dist_t {
	int64_t d; int u;
	dist_t (int64_t _d = 0, int _u = 0)
		: d(_d), u(_u) {}
	bool operator < (const dist_t &o) const {
		return d > o.d;
	}
};

inline void dijkstra(int64_t * dist) {
	priority_queue<dist_t> pq;
	for (int i = 1; i <= n; i++)
		pq.emplace(dist[i], i);
	while (!pq.empty()) {
		auto [d, u] = pq.top(); pq.pop();
		if (d != dist[u]) continue ;
		for (auto [v, w] : adj[u]) {
			if (dist[v] > d + w)
				pq.emplace(dist[v] = d + w, v);
		}
	}
}

void Hollwo_Pelw() {
	cin >> n >> k >> m;
	for (int i = 0; i < k; i++)
		cin >> node[i];
	for (int u, v, w; m --; )
		cin >> u >> v >> w, adj[u].emplace_back(v, w), adj[v].emplace_back(u, w);
	memset(dp, 0x3f, sizeof dp);

	for (int i = 0; i < k; i++)
		dp[1 << i][node[i]] = 0, dijkstra(dp[1 << i]); 

	for (int mask = 1; mask < 1 << k; mask++) {
		if ((mask & -mask) == mask) continue ;
		for (int sub = mask; sub; sub = (sub - 1) & mask) {
			
			for (int u = 1; u <= n; u++) for (auto [v, w] : adj[u])
				dp[mask][u] = min(dp[mask][u], dp[sub][u] + dp[mask ^ sub][v] + w);
		}
		dijkstra(dp[mask]);
	}
	cout << dp[(1 << k) - 1][node[0]];
}

Compilation message

cities.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
cities.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 54988 KB Output is correct
2 Correct 20 ms 54988 KB Output is correct
3 Correct 20 ms 55076 KB Output is correct
4 Correct 20 ms 55080 KB Output is correct
5 Correct 20 ms 55056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 551 ms 67852 KB Output is correct
2 Correct 524 ms 67876 KB Output is correct
3 Correct 307 ms 61840 KB Output is correct
4 Correct 82 ms 59792 KB Output is correct
5 Correct 276 ms 67764 KB Output is correct
6 Correct 71 ms 59756 KB Output is correct
7 Correct 22 ms 55116 KB Output is correct
8 Correct 21 ms 55244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 55204 KB Output is correct
2 Correct 25 ms 55232 KB Output is correct
3 Correct 23 ms 55176 KB Output is correct
4 Correct 22 ms 55116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1168 ms 67904 KB Output is correct
2 Correct 1054 ms 67836 KB Output is correct
3 Correct 626 ms 61912 KB Output is correct
4 Correct 628 ms 64504 KB Output is correct
5 Correct 202 ms 61468 KB Output is correct
6 Correct 121 ms 61520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2622 ms 67816 KB Output is correct
2 Correct 2589 ms 67860 KB Output is correct
3 Correct 2332 ms 67988 KB Output is correct
4 Correct 1343 ms 62060 KB Output is correct
5 Correct 1410 ms 64496 KB Output is correct
6 Correct 373 ms 61408 KB Output is correct
7 Correct 226 ms 61460 KB Output is correct