結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-30 23:35:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,632 bytes
コンパイル時間 2,706 ms
コンパイル使用メモリ 212,344 KB
実行使用メモリ 7,080 KB
最終ジャッジ日時 2024-04-16 09:43:11
合計ジャッジ時間 175,908 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 4,901 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4,901 ms
5,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 4,901 ms
5,416 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//焼き鈍し

#include <bits/stdc++.h>
#include <ctime>
#include <cstdlib>
#define rep(i, l, n) for (int i = (l); i < (n); i++)
using namespace std;
using Pair = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T> >;
const int inf = 2000000000;

using chrono::duration_cast;
using chrono::milliseconds;
using chrono::system_clock;
inline int getnow() { return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); }


VV<int> dijkstra(int n, VV<int>& edge, V<int>& R, VV<Pair>& route) {
	VV<int> dist(n, V<int>(0));
	V<int> st = { 0,n - 1 };
	rep(i, 0, R.size()) {
		st.push_back(edge[R[i]][0]);
		st.push_back(edge[R[i]][1]);
	}
	rep(i, 0, st.size()) {
		int k = st[i];
		V<int> di(n, -1);
		priority_queue<Pair, V<Pair>, greater<Pair> > pq;
		pq.push({ 0,k });
		int cnt = 0;
		while (pq.size() && cnt < n) {
			Pair p = pq.top();	pq.pop();
			int d = p.first;	int v = p.second;
			if (di[v] == -1) {
				cnt++;
				di[v] = d;
				rep(j, 0, route[v].size()) {
					Pair p = { d + route[v][j].second,route[v][j].first };
					pq.push(p);
				}
			}
		}
		dist[k] = di;
	}
	return dist;
}


int main(void) {
	srand(time(NULL));
	int start = getnow();
	int N, M, K;	cin >> N >> M >> K;

	V<int> R(K);
	rep(i, 0, K) {
		cin >> R[i];	R[i] -= 1;
	}

	VV<int> edge(M, V<int>(3));
	VV<Pair> route(N, V<Pair>(0));
	rep(i, 0, M) {
		rep(j, 0, 3) {
			cin >> edge[i][j];
		}
		edge[i][0] -= 1;	edge[i][1] -= 1;
		route[edge[i][0]].push_back({ edge[i][1],edge[i][2] });
		route[edge[i][1]].push_back({ edge[i][0],edge[i][2] });
	}

	int rsum = 0;
	rep(i, 0, R.size()) {
		rsum += edge[R[i]][2];
	}
	VV<int> dist = dijkstra(N, edge, R, route);


	V<int> p = {};
	rep(i, 0, K) {
		p.push_back(i);
	}
	VV<int> idp(K, V<int>(2, inf));
	rep(i, 0, K) {
		rep(j, 0, 2) {
			idp[i][j] = rsum + dist[0][edge[R[i]][j]];
		}
	}
	int ans = inf;	int tans = inf;
	while (getnow() - start < 4900) {
		int x = rand() % K;	int y = rand() % K;
		int tmp = p[x];
		p[x] = p[y];
		p[y] = tmp;

		V<int> q = p;	VV<int> dp = idp;
		rep(k, 0, K - 1) {
			int s = p[k];	int t = p[k + 1];
			rep(i, 0, 2) {
				rep(j, 0, 2) {
					int d = dp[s][i ^ 1] + dist[edge[R[s]][i]][edge[R[t]][j]];
					if (dp[t][j] > d) {
						dp[t][j] = d;
					}
				}
			}
		}
		int a = inf;
		rep(j, 0, 2) {
			int d = dp[K - 1][j ^ 1] + dist[edge[R[K - 1]][j]][N - 1];
			if (a > d) {
				a = d;
			}
		}
		if (ans >= a) {
			ans = a;
		}
		else if (rand() % 100 != 99) {
			rep(i, 0, K) {
				p[i] = q[i];
			}
			tans = ans;
			ans = a;
		}
	}

	if (ans > tans) {
		ans = tans;
	}
	cout << ans << endl;

	return 0;
}
0