Submission #14144878


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);
		visited[s] = true;
		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 cost;
	};
	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])
	Visited[K<<K+1|1<<K] = true;
	while (! q.empty()) {
		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]) {
				Visited[v2] = true;
				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 0
Code Size 1744 Byte
Status WA
Exec Time 455 ms
Memory 13276 KiB

Compile Error

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

Judge Result

Set Name All Sample
Score / Max Score 0 / 6 0 / 0
Status
AC × 24
WA × 13
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 3 ms 3404 KiB
sample_02.txt AC 2 ms 3592 KiB
testcase_1.txt AC 172 ms 13276 KiB
testcase_10.txt AC 34 ms 4724 KiB
testcase_11.txt AC 42 ms 4740 KiB
testcase_12.txt AC 7 ms 3584 KiB
testcase_13.txt AC 72 ms 5072 KiB
testcase_14.txt AC 50 ms 4560 KiB
testcase_15.txt AC 34 ms 4644 KiB
testcase_16.txt WA 55 ms 4624 KiB
testcase_17.txt AC 28 ms 4392 KiB
testcase_18.txt AC 34 ms 4628 KiB
testcase_19.txt WA 94 ms 8608 KiB
testcase_2.txt AC 455 ms 11040 KiB
testcase_20.txt WA 128 ms 8592 KiB
testcase_21.txt WA 228 ms 8880 KiB
testcase_22.txt WA 129 ms 8608 KiB
testcase_23.txt WA 257 ms 9464 KiB
testcase_24.txt AC 118 ms 8580 KiB
testcase_25.txt WA 168 ms 8376 KiB
testcase_26.txt WA 115 ms 8604 KiB
testcase_27.txt WA 112 ms 8528 KiB
testcase_28.txt WA 312 ms 9528 KiB
testcase_29.txt AC 65 ms 8500 KiB
testcase_3.txt AC 118 ms 6524 KiB
testcase_30.txt AC 79 ms 8480 KiB
testcase_31.txt WA 356 ms 10732 KiB
testcase_32.txt WA 308 ms 9508 KiB
testcase_33.txt WA 218 ms 8928 KiB
testcase_34.txt AC 3 ms 3592 KiB
testcase_35.txt AC 2 ms 3628 KiB
testcase_4.txt AC 62 ms 4708 KiB
testcase_5.txt AC 33 ms 4792 KiB
testcase_6.txt AC 37 ms 4828 KiB
testcase_7.txt AC 36 ms 4764 KiB
testcase_8.txt AC 17 ms 3976 KiB
testcase_9.txt AC 40 ms 4652 KiB