Submission #14145227


Source Code Expand

/* cmd
	g++ "$F"
*/
#include <algorithm>
#include <iostream>
#include <iterator>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

int main()
{
// Inputs
	int N,M; cin >> N >> M;

	vector<vector<int>> E(N+1);
	for (int i = 0; i < M; ++i) {
		int u,v; cin >> u >> v;
		E[u].push_back(v);
		E[v].push_back(u);
	}

	int S,K; cin >> S >> K;

	vector<int> T(K+1);
	for (int i = 0; i < K; ++i) {
		cin >> T[i];
	}
	T[K] = S;

// Prepare
	vector<vector<pair<int,int>>> E2(K+1);
	auto C = [&](int s, int t){
		int cost = 0;
		vector<bool> visited(N+1,false);
		for (int u: T) {
			visited[u] = true;
		}
		visited[t] = false;
		vector<int> q = {s};
		while (! q.empty() && ! visited[t]) {
			vector<int> q2;
			for (int u: q) {
				for (int v: E[u]) {
					if (! visited[v]) {
						visited[v] = true;
						q2.push_back(v);
					}
				}
			}
			q.swap(q2);
			cost += 1;
		}
		return visited[t] ? cost : 1<<30;
	};
	for (int i = 0; i < K; ++i) {
		for (int j = i+1; j < K+1; ++j) {
			int c = C(T[i],T[j]);
			E2[i].emplace_back(j,c);
			E2[j].emplace_back(i,c);
		}
	}

// Run
	const unsigned WOU = (1U<<K+1)-1;
	vector<bool> Visited(1<<K+6,false);
	priority_queue<
		pair<int,unsigned>,
	        vector<pair<int,unsigned>>,
	        greater<pair<int,unsigned>>
	> q;
	q.emplace(0,K<<K+1|1<<K); // S(=T[K])
	while (! q.empty()) {
		if (Visited[q.top().second]) {
			q.pop();
			continue;
		}
		Visited[q.top().second] = true;
		int      c = q.top().first;
		int      u = q.top().second>>K+1;
		unsigned v = q.top().second&WOU;
		q.pop();

		if (v == WOU) {
			cout << c << endl;
			break;
		}

		for (auto p: E2[u]) {
			unsigned v2 = v|p.first<<K+1|1<<p.first;
			if (! Visited[v2]) {
				q.emplace(c+p.second,v2);
			}
		}
	}
	return 0;
}

Submission Info

Submission Time
Task M - Real Traveling Salesman
User ds14050
Language C++ (GCC 9.2.1)
Score 6
Code Size 1859 Byte
Status AC
Exec Time 1695 ms
Memory 74616 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:66:29: warning: suggest parentheses around ‘+’ inside ‘<<’ [-Wparentheses]
   66 |  const unsigned WOU = (1U<<K+1)-1;
      |                            ~^~
./Main.cpp:67:27: warning: suggest parentheses around ‘+’ inside ‘<<’ [-Wparentheses]
   67 |  vector<bool> Visited(1<<K+6,false);
      |                          ~^~
./Main.cpp:73:18: warning: suggest parentheses around ‘+’ inside ‘<<’ [-Wparentheses]
   73 |  q.emplace(0,K<<K+1|1<<K); // S(=T[K])
      |                 ~^~
./Main.cpp:81:33: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
   81 |   int      u = q.top().second>>K+1;
      |                                ~^~
./Main.cpp:91:30: warning: suggest parentheses around ‘+’ inside ‘<<’ [-Wparentheses]
   91 |    unsigned v2 = v|p.first<<K+1|1<<p.first;
      |                             ~^~

Judge Result

Set Name All Sample
Score / Max Score 6 / 6 0 / 0
Status
AC × 37
AC × 2
Set Name Test Cases
All sample_01.txt, sample_02.txt, testcase_1.txt, testcase_10.txt, testcase_11.txt, testcase_12.txt, testcase_13.txt, testcase_14.txt, testcase_15.txt, testcase_16.txt, testcase_17.txt, testcase_18.txt, testcase_19.txt, testcase_2.txt, testcase_20.txt, testcase_21.txt, testcase_22.txt, testcase_23.txt, testcase_24.txt, testcase_25.txt, testcase_26.txt, testcase_27.txt, testcase_28.txt, testcase_29.txt, testcase_3.txt, testcase_30.txt, testcase_31.txt, testcase_32.txt, testcase_33.txt, testcase_34.txt, testcase_35.txt, testcase_4.txt, testcase_5.txt, testcase_6.txt, testcase_7.txt, testcase_8.txt, testcase_9.txt
Sample sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
sample_01.txt AC 2 ms 3464 KiB
sample_02.txt AC 2 ms 3412 KiB
testcase_1.txt AC 66 ms 9228 KiB
testcase_10.txt AC 43 ms 4956 KiB
testcase_11.txt AC 50 ms 4848 KiB
testcase_12.txt AC 6 ms 3560 KiB
testcase_13.txt AC 481 ms 12452 KiB
testcase_14.txt AC 207 ms 7940 KiB
testcase_15.txt AC 33 ms 4632 KiB
testcase_16.txt AC 492 ms 11784 KiB
testcase_17.txt AC 27 ms 4292 KiB
testcase_18.txt AC 32 ms 4704 KiB
testcase_19.txt AC 84 ms 8424 KiB
testcase_2.txt AC 437 ms 74616 KiB
testcase_20.txt AC 121 ms 8488 KiB
testcase_21.txt AC 401 ms 12540 KiB
testcase_22.txt AC 123 ms 8424 KiB
testcase_23.txt AC 693 ms 16800 KiB
testcase_24.txt AC 110 ms 8488 KiB
testcase_25.txt AC 180 ms 9084 KiB
testcase_26.txt AC 111 ms 8620 KiB
testcase_27.txt AC 103 ms 8296 KiB
testcase_28.txt AC 768 ms 16704 KiB
testcase_29.txt AC 60 ms 8436 KiB
testcase_3.txt AC 1197 ms 20736 KiB
testcase_30.txt AC 70 ms 8540 KiB
testcase_31.txt AC 1695 ms 25276 KiB
testcase_32.txt AC 793 ms 16820 KiB
testcase_33.txt AC 408 ms 12328 KiB
testcase_34.txt AC 2 ms 3588 KiB
testcase_35.txt AC 2 ms 3416 KiB
testcase_4.txt AC 472 ms 11908 KiB
testcase_5.txt AC 43 ms 4940 KiB
testcase_6.txt AC 46 ms 4844 KiB
testcase_7.txt AC 34 ms 4644 KiB
testcase_8.txt AC 41 ms 4528 KiB
testcase_9.txt AC 103 ms 6244 KiB