답안 #485511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485511 2021-11-08T03:13:57 Z hollwo_pelw Cities (BOI16_cities) C++17
22 / 100
1236 ms 65020 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 (int _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);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 54984 KB Output is correct
2 Correct 22 ms 55048 KB Output is correct
3 Correct 19 ms 55028 KB Output is correct
4 Correct 20 ms 54988 KB Output is correct
5 Correct 20 ms 55020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 234 ms 64928 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 55192 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 495 ms 65012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1236 ms 65020 KB Output isn't correct
2 Halted 0 ms 0 KB -