답안 #392206

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
392206 2021-04-20T15:57:20 Z sinamhdv Cities (BOI16_cities) C++11
100 / 100
2987 ms 40832 KB
// BOI16_cities
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'

#define MAXN 100100
#define MAXK 6

int n, k, m;
vector<int> imp;
vector<pii> adj[MAXN];
ll dp[1 << MAXK][MAXN];

int32_t main(void)
{
	fast_io;
	cin >> n >> k >> m;
	FOR(i, 0, k)
	{
		int x;
		cin >> x;
		x--;
		imp.pb(x);
	}
	FOR(i, 0, m)
	{
		int x, y, w;
		cin >> x >> y >> w;
		x--; y--;
		adj[x].pb({y, w});
		adj[y].pb({x, w});
	}

	FOR(i, 0, 1 << k) fill(dp[i], dp[i] + MAXN, LINF);
	FOR(i, 0, k) dp[1 << i][imp[i]] = 0;
	FOR(mask, 1, 1 << k)
	{
		for (int sub = (mask - 1) & mask; sub; sub = (sub - 1) & mask)
			FOR(v, 0, n)
				dp[mask][v] = min(dp[mask][v], dp[sub][v] + dp[mask ^ sub][v]);

		priority_queue<pll, vector<pll>, greater<pll>> s;
		FOR(i, 0, n) s.push({dp[mask][i], i});

		while (!s.empty())
		{
			int v = s.top().sc;
			s.pop();
			for (pii p : adj[v])
			{
				int u = p.fr;
				int w = p.sc;
				if (dp[mask][v] + w >= dp[mask][u]) continue;
				dp[mask][u] = dp[mask][v] + w;
				s.push({dp[mask][u], u});
			}
		}
	}

	ll ans = LINF;
	FOR(i, 0, n) ans = min(ans, dp[(1 << k) - 1][i]);
	cout << ans << endl;
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 7 ms 8908 KB Output is correct
4 Correct 9 ms 15180 KB Output is correct
5 Correct 15 ms 27652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 856 ms 21672 KB Output is correct
2 Correct 792 ms 21756 KB Output is correct
3 Correct 489 ms 15772 KB Output is correct
4 Correct 86 ms 13636 KB Output is correct
5 Correct 395 ms 18596 KB Output is correct
6 Correct 79 ms 10472 KB Output is correct
7 Correct 8 ms 8992 KB Output is correct
8 Correct 5 ms 5836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 15308 KB Output is correct
2 Correct 14 ms 15276 KB Output is correct
3 Correct 12 ms 15180 KB Output is correct
4 Correct 12 ms 15224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1505 ms 28032 KB Output is correct
2 Correct 1413 ms 28056 KB Output is correct
3 Correct 917 ms 22168 KB Output is correct
4 Correct 802 ms 24536 KB Output is correct
5 Correct 205 ms 21512 KB Output is correct
6 Correct 103 ms 21632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2961 ms 40600 KB Output is correct
2 Correct 2987 ms 40760 KB Output is correct
3 Correct 2807 ms 40832 KB Output is correct
4 Correct 1839 ms 34820 KB Output is correct
5 Correct 1459 ms 37424 KB Output is correct
6 Correct 326 ms 34292 KB Output is correct
7 Correct 138 ms 34356 KB Output is correct