QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#352718#7991. 最小环zzy0922RE 11ms49816kbC++142.3kb2024-03-13 16:03:312024-03-13 16:03:31

Judging History

你现在查看的是最新测评结果

  • [2024-03-13 16:03:31]
  • 评测
  • 测评结果:RE
  • 用时:11ms
  • 内存:49816kb
  • [2024-03-13 16:03:31]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
constexpr int N = 300005;

int n, m, tot;
int u[N << 1], v[N << 1];
i64 w[N << 1];
bool del[N << 1];
std::unordered_set<int> eS[N]; 

inline void add_e(int uu, int vv, i64 ww) {
	++tot;
	u[tot] = uu;
	v[tot] = vv;
	w[tot] = ww;
	eS[uu].insert(tot);
	eS[vv].insert(tot);
}

std::vector<std::pair<int, i64> > g[N];
std::unordered_map<int, i64> dis[N];

std::unordered_set<int> vis;

inline void dij(int s) {
	dis[s][s] = 0;
	vis.clear();
	std::priority_queue <std::pair<i64, int>, std::vector<std::pair<i64, int> >, std::greater<std::pair<i64, int> > > pq;
	pq.emplace(0, s);
	while (!pq.empty()) {
		int u = pq.top().second;
		pq.pop();
		if (vis.count(u)) continue;
		vis.insert(u);
		for (const auto &[v, w] : g[u]) {
			if (!dis[s].count(v) || dis[s][v] > dis[s][u] + w) {
				dis[s][v] = dis[s][u] + w;
				pq.emplace(dis[s][v], v);
			}
		}
	}
}

i64 ans = 0x7fffffffffffffffll;

int main() {
	std::cin >> n >> m;
	for (int i = 1, u, v, w; i <= m; i++) {
		std::cin >> u >> v >> w;
		add_e(u, v, w);
		if (u == v) ans = std::min(ans, 1ll * w);
	}

	auto del_e = [&](int id) {
		eS[u[id]].erase(id);
		eS[v[id]].erase(id);
		del[id] = 1;
	};

	std::queue<int> q;
	auto chk = [&](int u) {
		if (!eS[u].empty() && eS[u].size() <= 2) q.push(u);
	};

	for (int i = 1; i <= n; i++) chk(i);

	while (!q.empty()) {
		int x = q.front();
		q.pop();
		if (eS[x].size() == 1) {
			int e = *eS[x].begin();
			del_e(e);
			chk(u[e]);
			chk(v[e]);
		}
		else {
			int e1 = *eS[x].begin(), e2 = *std::next(eS[x].begin()); 
			del_e(e1);
			del_e(e2);
			if (v[e2] == u[e1]) std::swap(e1, e2);
			if (v[e1] == u[e2]) {
				add_e(u[e1], v[e2], w[e1] + w[e2]);
				chk(u[e1]);
				chk(v[e2]);
			} else if (u[e1] == u[e2]){
				chk(v[e1]);
				chk(v[e2]);
			} else {
				chk(u[e1]);
				chk(u[e2]);
			}
		}
	}
	for (int i = 1; i <= tot; i++) 
		if (!del[i]) g[u[i]].emplace_back(v[i], w[i]);// std::cout << "ADD: " << u[i] << ' ' << v[i] << ' ' << w[i] << '\n';
	for (int i = 1; i <= n; i++) dij(i);
	for (int u = 1; u <= n; u++) 
		for (const auto &[v, w] : dis[u]) {
			if (v != u && dis[v].count(u)) ans = std::min(ans, dis[v][u] + w);
			// std::cout << u << ' ' << v << ' ' << w << '\n';
		}
	std::cout << (ans == 0x7fffffffffffffffll ? -1 : ans) << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 49816kb

input:

4 6
1 2 1
4 3 3
4 1 9
2 4 1
3 1 2
3 2 6

output:

7

result:

ok single line: '7'

Test #2:

score: 0
Accepted
time: 7ms
memory: 44564kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 11ms
memory: 48372kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Runtime Error

input:

258420 258419
33061 33062 767169384
212916 212917 1741339
229881 229882 896760805
173467 173468 273055172
233189 233190 800433307
10157 10158 126766550
174605 174606 552176083
224030 224031 886617880
229102 229103 783848581
67588 67589 510826095
233648 233649 879695751
214453 214454 867104578
153140...

output:


result: