Submission #30924

# Submission time Handle Problem Language Result Execution time Memory
30924 2017-08-01T06:24:24 Z Navick Cities (BOI16_cities) C++14
22 / 100
3369 ms 38848 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1e5 + 10, K = 5;
const ll INF = 1e18;

ll dp[N][1 << K];
vector<pii> adj[N];
int imp[K];
priority_queue<pair<int,int>,vector<pair<int,int> >, less<pair<int,int> > > pq;
bool mark[N];

int main(){
	int n, k, m; scanf("%d %d %d", &n, &k, &m);
	for(int i=0; i<k; i++){
		int v; scanf("%d", &v);
		imp[i] = v;
	}

	for(int i=0; i<m; i++){
		int u, v, w; scanf("%d %d %d", &u, &v, &w);
		adj[u].pb({v, w});
		adj[v].pb({u, w});
	}
	
	for(int mask=1; mask<(1 << k); mask++){
		while(!pq.empty())
			pq.pop();

		memset(mark, 0, sizeof mark);
		for(int i=1; i<=n; i++)
			dp[i][mask] = INF;
		
		if(__builtin_popcount(mask) == 1){
			for(int j=0; j<k; j++)
				if(mask & (1 << j))
					dp[imp[j]][mask] = 0;
		}
		
		for(int i=1; i<=n; i++){
			for(int t=mask; t; t=mask & (t - 1))
				dp[i][mask] = min(dp[i][mask], dp[i][t] + dp[i][mask ^ t]);

			pq.push({dp[i][mask], i});
		}
		
		while(!pq.empty()){
			int v = pq.top().S;
			pq.pop();
			if(mark[v])continue;
			mark[v] = true;
			for(auto X : adj[v]){
				int u = X.F, w = X.S;
				if(dp[u][mask] > dp[v][mask] + w){
					dp[u][mask] = dp[v][mask] + w;
					pq.push({dp[u][mask], u});
				}
			}
		}
	}
	ll res = INF;
	for(int i=1; i<=n; i++)
		res = min(res, dp[i][(1 << k) - 1]);
	printf("%lld\n", res);
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:22:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, k, m; scanf("%d %d %d", &n, &k, &m);
                                            ^
cities.cpp:24:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int v; scanf("%d", &v);
                         ^
cities.cpp:29:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v, w; scanf("%d %d %d", &u, &v, &w);
                                             ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 29464 KB Output is correct
2 Correct 0 ms 29464 KB Output is correct
3 Correct 0 ms 29464 KB Output is correct
4 Correct 0 ms 29464 KB Output is correct
5 Correct 0 ms 29464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 866 ms 38848 KB Output is correct
2 Incorrect 963 ms 38776 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 29596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1953 ms 38840 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3369 ms 38844 KB Output isn't correct
2 Halted 0 ms 0 KB -