제출 #230385

#제출 시각아이디문제언어결과실행 시간메모리
230385arnold518Cities (BOI16_cities)C++14
74 / 100
6069 ms171856 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const ll INF = 1e18;

int N, M, K;
int A[10];
vector<pii> adj[MAXN+10];

ll dist[MAXN+10][40];

struct Queue
{
	int v, mask; ll w;
	bool operator < (const Queue &p) const { return w>p.w; }
};

int main()
{
	int i, j;

	scanf("%d%d%d", &N, &K, &M);
	for(i=0; i<K; i++) scanf("%d", &A[i]);
	for(i=1; i<=M; i++)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
	}

	for(i=1; i<=N; i++) for(j=1; j<(1<<K); j++) dist[i][j]=INF;

	priority_queue<Queue> PQ;
	for(i=0; i<K; i++)
	{
		PQ.push({A[i], (1<<i), 0});
		dist[A[i]][(1<<i)]=0;
	}

	while(!PQ.empty())
	{
		Queue now=PQ.top(); PQ.pop();
		if(now.w>dist[now.v][now.mask]) continue;
		//printf("%d %d %lld\n", now.v, now.mask, now.w);

		for(auto &nxt : adj[now.v])
		{
			int p=(((1<<K)-1)^now.mask);
			for(i=p; ; i=p&(i-1))
			{
				if(dist[nxt.first][i|now.mask]>now.w+dist[nxt.first][i]+nxt.second)
				{
					dist[nxt.first][i|now.mask]=now.w+dist[nxt.first][i]+nxt.second;
					PQ.push({nxt.first, i|now.mask, dist[nxt.first][i|now.mask]});
				}
				if(i==0) break;
			}
		}
	}

	ll ans=INF;
	for(i=1; i<=N; i++) ans=min(ans, dist[i][(1<<K)-1]);
	printf("%lld\n", ans);
}

컴파일 시 표준 에러 (stderr) 메시지

cities.cpp: In function 'int main()':
cities.cpp:27: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:28:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=0; i<K; i++) scanf("%d", &A[i]);
                     ~~~~~^~~~~~~~~~~~~
cities.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &u, &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...