[CF845G]Shortest Path Problem?

题目大意:这道题,只是把最大值变成了最小值

题解:

卡点:

C++ Code:

#include <cstdio>
#define maxn 100010
#define maxm 100010
int head[maxn], cnt;
struct Edge {
	int to, nxt;
	long long w;
} e[maxm << 1];
void addE(int a, int b, long long c) {
	e[++cnt] = (Edge) {b, head[a], c}; head[a] = cnt;
}
 
long long p[64];
inline void add(long long x) {
	for (int i = 62; ~i; i--) if (x & 1ll << i) {
		if (p[i]) x ^= p[i];
		else {p[i] = x; break;}
	}
}
inline long long ask(long long x) {
	long long ans = x;
	for (int i = 62; ~i; i--) if (ans > (ans ^ p[i])) ans = ans ^ p[i];
	return ans;
}
 
long long d[maxn];
bool vis[maxn];
void dfs(int rt, long long now) {
	d[rt] = now;
	vis[rt] = true;
	for (int i = head[rt]; i; i = e[i].nxt) {
		int v = e[i].to;
		if (!vis[v]) dfs(v, d[rt] ^ e[i].w);
		else add(d[rt] ^ d[v] ^ e[i].w);
	}
}
int n, m;
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++) {
		int a, b;
		long long c;
		scanf("%d%d%lld", &a, &b, &c);
		addE(a, b, c);
		addE(b, a, c);
	}
	dfs(1, 0);
	printf("%lld\n", ask(d[n]));
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Memory-of-winter/p/9777719.html