82pts,玄2观
查看原帖
82pts,玄2观
1433769
CCY20130127楼主2025/7/31 16:52
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1510;
int n, m, u, v, w, f[N], dis[N];
vector<pair<int, int> > adj[N];
queue<int> q;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
inline void write(int x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + '0');
}
void spfa(int s) {
	for (int i = 1; i <= n; i++) dis[i] = INT_MIN, f[i] = 0;
	dis[s] = 0;
	f[s] = 1;
	q.push(s);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (int i = 0; i < adj[x].size(); i++) {
			int y = adj[x][i].first, w = adj[x][i].second;
			if (dis[y] < dis[x] + w) {
				dis[y] = dis[x] + w;
				if (!f[y]) f[y] = 1, q.push(y);
			}
		}
		f[x] = 0;
	}
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		adj[u].push_back(make_pair(v, w));
	}
	spfa(1);
	cout << dis[n] << "\n";
	return 0;
}

spfa 不要改算法

2025/7/31 16:52
加载中...