Submission #45077

# Submission time Handle Problem Language Result Execution time Memory
45077 2018-04-11T06:59:58 Z nibnalin Cities (BOI16_cities) C++17
14 / 100
4000 ms 48108 KB
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
using namespace std;

typedef long long int lli;

const int maxn = int(1e5)+5, maxk = 6;
const lli inf = lli(1e17)+5;

int n, A[maxk];
lli D[maxk][maxn], dp[(1<<maxk)][maxn];
vector<pair<int, lli>> graph[maxn];

int main(void)
{
	int k, m, u, v;
	lli w;
	scanf("%d%d%d", &n, &k, &m);

	for(int i = 0;i < k;i++)
	{
		for(int j = 0;j < n;j++) D[i][j] = inf;
	}

	for(int i = 0;i < k;i++)
	{
		scanf("%d", &A[i]);
		A[i]--;
	}

	for(int i = 0;i < m;i++)
	{
		scanf("%d%d%lld", &u, &v, &w);
		u--, v--;
		graph[u].push_back({v, w}), graph[v].push_back({u, w});
	}

	for(int i = 0;i < (1<<k);i++)
	{
		for(int j = 0;j < n;j++) dp[i][j] = inf;
	}
	for(int i = 0;i < k;i++) dp[(1<<i)][A[i]] = 0;

	for(int mask = 1;mask < (1<<k);mask++)
	{
		set<pair<lli, int>> Q;
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < k;j++)
			{
				if((mask&(1<<j)))
				{
					dp[mask][i] = min(dp[mask][i], dp[(mask^(1<<j))][i]+dp[(1<<j)][i]);
					break;
				}
			}
			Q.insert({dp[mask][i], i});
		}

		while(!Q.empty())
		{
			pair<lli, int> top = *Q.begin();
			Q.erase(Q.begin());

			for(auto it: graph[top.second])
			{
				if(dp[mask][it.first] > dp[mask][top.second]+it.second)
				{
					Q.erase({dp[mask][it.first], it.first});
					dp[mask][it.first] = dp[mask][top.second]+it.second;
					Q.insert({dp[mask][it.first], it.first});
				}
			}
		}
	}

	lli res = inf;
	for(int i = 0;i < n;i++) res = min(res, dp[(1<<k)-1][i]);
	printf("%lld\n", res);
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:20:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &n, &k, &m);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]);
   ~~~~~^~~~~~~~~~~~~
cities.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%lld", &u, &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 3 ms 2792 KB Output is correct
3 Correct 3 ms 2996 KB Output is correct
4 Correct 4 ms 2996 KB Output is correct
5 Incorrect 4 ms 3232 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 2423 ms 27744 KB Output is correct
2 Correct 2361 ms 27744 KB Output is correct
3 Correct 1189 ms 27744 KB Output is correct
4 Correct 135 ms 27744 KB Output is correct
5 Correct 1016 ms 27744 KB Output is correct
6 Correct 126 ms 27744 KB Output is correct
7 Correct 10 ms 27744 KB Output is correct
8 Correct 7 ms 27744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 27744 KB Output is correct
2 Incorrect 14 ms 27744 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4062 ms 34796 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4046 ms 48108 KB Time limit exceeded
2 Halted 0 ms 0 KB -