玄关求调
查看原帖
玄关求调
733791
Collapsar233楼主2024/11/22 19:57
#include <iostream>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<long long, int> pli;

template<typename T>
void read(T& x) {
	x = 0;
	bool sgn = false;
	char ch = getchar();
	while (ch < 48 || 57 < ch) {
		sgn ^= ch == '-';
		ch = getchar();
	}
	while (48 <= ch && ch <= 57) {
		x = x * 10 + (ch ^ 48);
		ch = getchar();
	}
	x = sgn ? -x : x;
	return;
}

const int maxn = 1.5e3+7, maxm = 3e5+7;
const ll inf = 1e13+7;

int n, m, x1, y1, x2, y2;
int t, tt, h[maxn], hh[maxn], ind[maxn];
ll dis1[maxn], dis2[maxn], dis3[maxn], dis4[maxn], ans[maxn];
bool vis[maxn];
priority_queue<pli, vector<pli>, greater<pli> > q;
queue<int> que;

struct Edge {
	int from, to, w, nxt;
}e[maxm << 1], ee[maxm << 1];

inline void Add(int u, int v, int w, Edge* edge, int* head, int& tot) {
	edge[++tot].from = u;
	edge[tot].to = v;
	edge[tot].w = w;
	edge[tot].nxt = head[u];
	head[u] = tot;
	return;
}

void Dijkstra(int s, ll* dis) {
	for (int i = 1; i <= n; ++i) {
		vis[i] = false;
		dis[i] = inf;
	}
	dis[s] = 0;
	q.emplace(make_pair(dis[s], s));
	while (!q.empty()) {
		int u = q.top().second;
		q.pop();
		if (vis[u])
			continue;
		vis[u] = true;
		for (int i = h[u]; i != 0; i = e[i].nxt) {
			int v = e[i].to;
			if (dis[v] > dis[u] + e[i].w) {
				dis[v] = dis[u] + e[i].w;
				q.emplace(make_pair(dis[v], v));
			}
		}
	}
}

void toposort() {
	for (int i = 1; i <= n; ++i)
		if (ind[i] == 0)
			que.emplace(i);
	while (!que.empty()) {
		int u = que.front();
		que.pop();
		for (int i = hh[u]; i != 0; i = ee[i].nxt) {
			int v = ee[i].to;
			if (ans[v] < ans[u] + ee[i].w) {
				ans[v] = ans[u] + ee[i].w;
				if (--ind[v] == 0)
					que.emplace(v);
			}
		}
	}
	return;
}

int main() {
	read(n), read(m);
	read(x1), read(y1), read(x2), read(y2);
	for (int i = 1; i <= n; ++i)
		ind[i] = -1;
	int u, v, w;
	for (int i = 1; i <= m; ++i) {
		read(u), read(v), read(w);
		Add(u, v, w, e, h, t);
		Add(v, u, w, e, h, t);
	}
	m <<= 1;
	Dijkstra(x1, dis1);
	Dijkstra(y1, dis2);
	Dijkstra(x2, dis3);
	Dijkstra(y2, dis4);
	ll sd1 = dis1[y1], sd2 = dis3[y2];
	for (int i = 1; i <= m; ++i) {
		int u = e[i].from, v = e[i].to;
		ll w = e[i].w;
		ll d1 = min(dis1[u], dis1[v]), d2 = min(dis2[u], dis2[v]), d3 = min(dis3[u], dis3[v]), d4 = min(dis4[u], dis4[v]);
		if (d1 + d2 + w == sd1 && d3 + d4 + w == sd2) {
			if (i & 1)
				++i;
			if (ind[u] == -1)
				ind[u] = 0;
			if (ind[v] == -1)
				ind[v] = 0;
			if (dis1[u] < dis1[v])
				Add(u, v, w, ee, hh, tt), ++ind[v];
			else
				Add(v, u, w, ee, hh, tt), ++ind[u];
		}
	}
	toposort();
	int mx = 0;
	for (int i = 1; i <= n; ++i) {
		if (ind[i] == 0 && ans[i] > mx)
			mx = ans[i];
	}
	cout << mx << "\n";
	return 0;
}
2024/11/22 19:57
加载中...