Submission #718517

# Submission time Handle Problem Language Result Execution time Memory
718517 2023-04-04T09:15:32 Z lukameladze Cities (BOI16_cities) C++14
100 / 100
2162 ms 52164 KB
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
 #define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 2e5 + 5;
int t,n,k,m,sp[N],a[N],dp[33][N],idx[N],inf = 1e15;
vector < pii > v[N];
main() {
	std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>k>>m;
	for (int i = 0; i < k; i++) {
		cin>>sp[i];
		sp[i]--;
	}
	sort(sp, sp + k);
	k = unique(sp, sp + k) - sp;
	for (int i = 0; i < n; i++) idx[i] = -1;
	for (int i = 0; i < k; i++) {
		idx[sp[i]] = i;
	}
	for (int i = 1; i <= m; i++) {
		int a, b, c;
		cin>>a>>b>>c; 
		a--; b--;
		v[a].pb({b, c});
		v[b].pb({a, c});
	}
	for (int i = 0; i < (1<<k); i++) {
		for (int j = 0;  j < n ;j++) {
			dp[i][j] = inf;
		}
	}
	int ans = inf;
	for (int i = 0; i < k; i++) {
		dp[(1<<i)][sp[i]] = 0; // dp[mask][i] --- > minimum spanning tree rooted on vertex i, visited (mask) special vertices 
	}
	for (int mask = 1; mask < (1<<k); mask++) {
		priority_queue <pii> q;
		for (int i = 0; i < n; i++) {
			if (idx[i] != -1 && mask&(1<<idx[i])) dp[mask][i] = min(dp[mask][i], dp[mask ^ (1<<idx[i])][i]);
			for (int sub = (mask - 1); sub >= 1; sub = (sub - 1) & mask) {
				dp[mask][i] = min(dp[mask][i], dp[sub][i] + dp[mask ^ sub][i]);
			} 
			q.push({-dp[mask][i], i});
		}
		while (!q.empty()) {
			int x = -q.top().f;
			int y = q.top().s;
			q.pop();
			if (x > dp[mask][y]) continue;
			for (pii sth : v[y]) {
				int to = sth.f; int c = sth.s;
				if (dp[mask][to] > x + c) {
					dp[mask][to] = x + c;
					q.push({-dp[mask][to], to});
				}
			}
		}
		if (mask == (1<<k) - 1) for (int x = 0; x < n; x++) ans = min(ans, dp[mask][x]);
	}
	cout<<ans<<"\n";
}

Compilation message

cities.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   11 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 4 ms 5156 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 584 ms 28976 KB Output is correct
2 Correct 681 ms 32760 KB Output is correct
3 Correct 426 ms 23092 KB Output is correct
4 Correct 71 ms 17648 KB Output is correct
5 Correct 305 ms 30064 KB Output is correct
6 Correct 89 ms 17568 KB Output is correct
7 Correct 6 ms 5300 KB Output is correct
8 Correct 6 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5332 KB Output is correct
2 Correct 10 ms 5460 KB Output is correct
3 Correct 6 ms 5332 KB Output is correct
4 Correct 6 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1179 ms 35280 KB Output is correct
2 Correct 1143 ms 39072 KB Output is correct
3 Correct 668 ms 29592 KB Output is correct
4 Correct 607 ms 30180 KB Output is correct
5 Correct 164 ms 20792 KB Output is correct
6 Correct 75 ms 19920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2139 ms 48048 KB Output is correct
2 Correct 2162 ms 52164 KB Output is correct
3 Correct 2081 ms 51752 KB Output is correct
4 Correct 1322 ms 42140 KB Output is correct
5 Correct 1105 ms 36632 KB Output is correct
6 Correct 268 ms 22180 KB Output is correct
7 Correct 96 ms 20364 KB Output is correct