답안 #45078

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45078 2018-04-11T07:05:37 Z nibnalin Cities (BOI16_cities) C++17
74 / 100
4000 ms 47932 KB
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
using namespace std;
 
typedef long long int lli;
 
const int maxn = lli(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];
 
void dijkstra(int type, int node)
{
	for(int i = 0;i < n;i++) D[type][i] = inf;
	D[type][node] = 0;
 
	set<pair<lli, int>> Q;
	Q.insert({D[type][node], node});
 
	while(!Q.empty())
	{
		pair<lli, int> top = *Q.begin();
		Q.erase(Q.begin());
 
		for(auto it: graph[top.second])
		{
			if(D[type][it.first] > D[type][top.second]+it.second)
			{
				if(D[type][it.first] != inf) Q.erase({D[type][it.first], it.first});
				D[type][it.first] = D[type][top.second]+it.second;
				Q.insert({D[type][it.first], it.first});
			}
		}
	}
}
 
int main(void)
{
	int k, m, u, v;
	lli w;
	scanf("%d%d%d", &n, &k, &m);
	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 < k;i++) dijkstra(i, A[i]);
 
	for(int mask = 1;mask < (1<<k);mask++)
	{
		priority_queue<pair<lli, int>> Q;
		for(int i = 0;i < n;i++)
		{
			dp[mask][i] = inf;
			for(int j = 0;j < k;j++)
			{
				if((mask&(1<<j))) dp[mask][i] = min(dp[mask][i], dp[(mask^(1<<j))][i]+D[j][i]);
			}
			Q.push({-dp[mask][i], i});
		}
 
		while(!Q.empty())
		{
			pair<lli, int> top = Q.top();
			Q.pop();
 
			for(auto it: graph[top.second])
			{
				if(dp[mask][it.first] > dp[mask][top.second]+it.second)
				{
					dp[mask][it.first] = dp[mask][top.second]+it.second;
					Q.push({-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:46: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:49:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]);
   ~~~~~^~~~~~~~~~~~~
cities.cpp:55: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);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 5 ms 2792 KB Output is correct
3 Correct 5 ms 2792 KB Output is correct
4 Correct 4 ms 2988 KB Output is correct
5 Correct 4 ms 3052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1157 ms 27560 KB Output is correct
2 Correct 1112 ms 27560 KB Output is correct
3 Correct 643 ms 27560 KB Output is correct
4 Correct 125 ms 27560 KB Output is correct
5 Correct 626 ms 27560 KB Output is correct
6 Correct 118 ms 27560 KB Output is correct
7 Correct 7 ms 27560 KB Output is correct
8 Correct 6 ms 27560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 27560 KB Output is correct
2 Correct 10 ms 27560 KB Output is correct
3 Correct 8 ms 27560 KB Output is correct
4 Correct 8 ms 27560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2216 ms 34724 KB Output is correct
2 Correct 2160 ms 34724 KB Output is correct
3 Correct 1307 ms 34724 KB Output is correct
4 Correct 1222 ms 34724 KB Output is correct
5 Correct 347 ms 34724 KB Output is correct
6 Correct 133 ms 34724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4011 ms 47932 KB Time limit exceeded
2 Halted 0 ms 0 KB -