Submission #84241

# Submission time Handle Problem Language Result Execution time Memory
84241 2018-11-14T02:56:23 Z ToMoClone Cities (BOI16_cities) C++17
74 / 100
6000 ms 133352 KB
/*input
4 3 6
1 3 4
1 2 4
1 3 9
1 4 6
2 3 2
2 4 5
3 4 8
*/
#include <bits/stdc++.h>
using namespace std;
 
const int N = 100005;
const int64_t inf = 1e15;
 
struct State {
	int node; int64_t d; int mask;
	State(int _node = 0, int64_t _d = inf, int _mask = 0) :
		node(_node), d(_d), mask(_mask) {}
	bool operator<(const State &x) const {
		return d > x.d;
	}
};
 
int n, k, m, mark[N];
int64_t d[N][32];
priority_queue<State> q;
vector<pair<int, int> > G[N];
 
int32_t main(){
	memset(mark, -1, sizeof mark), memset(d, 127, sizeof d);
 
	scanf("%d%d%d", &n, &k, &m);
	for(int i = 0; i < k; ++ i) {
		int a; scanf("%d", &a); mark[a] = i;
		d[a][1 << i] = 0, q.push(State(a, 0, 1 << i));
	}
	for(int i = 1; i <= m; ++ i) {
		int u, v, c; scanf("%d%d%d", &u, &v, &c);
		G[u].push_back(make_pair(c, v));
		G[v].push_back(make_pair(c, u));
	}
 
	while(!q.empty()) {
		auto cur = q.top(); q.pop();
		if(cur.mask == (1 << k) - 1) return cout << cur.d << endl, 0;
		for(auto u : G[cur.node])
			if(d[u.second][cur.mask] > d[cur.node][cur.mask] + u.first) {
				d[u.second][cur.mask] = d[cur.node][cur.mask] + u.first;
				q.push(State(u.second, d[u.second][cur.mask], cur.mask));
			}
			
		int otherMask = ((1 << k) - 1) ^ cur.mask; 
		for(auto u : G[cur.node]) for(int i = 0; i < k; ++ i) if(otherMask & (1 << i)) {
			int mask = (1 << i);
			if(d[cur.node][mask | cur.mask] > cur.d + d[u.second][mask] + u.first) {
				d[cur.node][mask | cur.mask] = cur.d + d[u.second][mask] + u.first;
				q.push(State(cur.node, d[cur.node][mask | cur.mask], mask | cur.mask));
			}
			if(d[u.second][mask | cur.mask] > cur.d + d[u.second][mask] + u.first) {
				d[u.second][mask | cur.mask] = cur.d + d[u.second][mask] + u.first;
				q.push(State(u.second, d[u.second][mask | cur.mask], mask | cur.mask));
			}
		}
	}
}

Compilation message

cities.cpp: In function 'int32_t main()':
cities.cpp:34: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:36:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a; scanf("%d", &a); mark[a] = i;
          ~~~~~^~~~~~~~~~
cities.cpp:40:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v, c; scanf("%d%d%d", &u, &v, &c);
                ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 28024 KB Output is correct
2 Correct 25 ms 28148 KB Output is correct
3 Correct 25 ms 28224 KB Output is correct
4 Correct 24 ms 28300 KB Output is correct
5 Correct 25 ms 28300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 713 ms 59192 KB Output is correct
2 Correct 979 ms 59192 KB Output is correct
3 Correct 170 ms 59192 KB Output is correct
4 Correct 163 ms 59192 KB Output is correct
5 Correct 345 ms 59192 KB Output is correct
6 Correct 134 ms 59192 KB Output is correct
7 Correct 29 ms 59192 KB Output is correct
8 Correct 26 ms 59192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 59192 KB Output is correct
2 Correct 35 ms 59192 KB Output is correct
3 Correct 29 ms 59192 KB Output is correct
4 Correct 32 ms 59192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3291 ms 83924 KB Output is correct
2 Correct 3013 ms 83924 KB Output is correct
3 Correct 1245 ms 83924 KB Output is correct
4 Correct 2111 ms 83924 KB Output is correct
5 Correct 558 ms 83924 KB Output is correct
6 Correct 303 ms 83924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6056 ms 133352 KB Time limit exceeded
2 Halted 0 ms 0 KB -