0105 总结

发布时间 2024-01-06 13:35:00作者: 固态H2O

我是什么煞笔玩意儿

A - 过关斩将

多维最短路,记录一下到这个节点是什么状态,如需改变直接 \(+ x\) 即可

煞笔代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int n, m, s, t, x;
typedef pair<int, int> pi;
typedef long long ll;
vector<pi> G[N];
void addedge (int u, int v, int w) {
	G[u].push_back({v, w});
	G[v].push_back({u, w});
}
char a[N];
int type[N];
bool vis[N][3];
ll dis[N][3];
void dijkstra (int s, int t) {
	priority_queue< pair<int, pi> , vector< pair<int, pi> >, greater< pair<int, pi> > > q;
	if(type[s]) q.push({0, {s, type[s]}}), dis[s][type[s]] = 0;
	else q.push({0, {s, 1}}), q.push({0, {s, 2}}), dis[s][1] = dis[s][2] = 0;
	while(!q.empty()) {
		int u = q.top().second.first, ty = q.top().second.second;
		q.pop();
		if(vis[u][ty]) continue;
		vis[u][ty] = 1;
		for (auto e : G[u]) {
			int v = e.first, w = e.second, toty = ty;
			if(type[v] && ty != type[v]) w += x, toty = type[v];
			if(dis[u][ty] + w < dis[v][toty]) {
				dis[v][toty] = dis[u][ty] + w;
				q.push({dis[v][toty], {v, toty}});
			}
		}
	}
}

signed main() {
	ios::sync_with_stdio(0);
	int T;
	cin >> T;
	while(T --) {
		cin >> n >> m >> s >> t >> x;
		for (int i = 1; i <= n; i ++) {
			dis[i][1] = dis[i][2] = 1e18;
			G[i].clear();
			vis[i][1] = vis[i][2] = 0;
		}
		cin >> (a + 1);
		for (int i = 1; i <= n; i ++) {
			if(a[i] == 'L') type[i] = 1;
			else if(a[i] == 'R') type[i] = 2;
			else type[i] = 0;
		}
		while(m --) {
			int u, v, w;
			cin >> u >> v >> w;
			addedge(u, v, w);
		}
		dijkstra(s, t);
		cout << min(dis[t][1], dis[t][2]) << endl;

	}
	return 0;
}

B - 翻转游戏

这个 *** 赛时连 NO 的情况都没想出来
赛后听 LHY 讲,半懂不懂的。