这Dijkstra为啥错
查看原帖
这Dijkstra为啥错
613228
sinnoaa楼主2024/1/20 18:09
#include<bits/stdc++.h>
using namespace std;
const int N = 55;
typedef pair<int ,int > PII;

int n, m;
vector<PII> g[N];
int st[N];
int dist[N];
int dijks(int start){
	memset(st,0,sizeof st);
	memset(dist, -0x3f,sizeof dist);
	
	dist[start] = 0;
	
	for(int i = 1; i <= n; i ++){
		int t = -1;
		for(int j = 1; j <= n; j ++){
			if(!st[j] && (t == -1 || dist[t] < dist[j])) t = j;
		}
		st[t] = 1;
		 
		for(auto [w, val] : g[t]){
			if(!st[val] && dist[val] < w + dist[t]){
				dist[val] = w + dist[t];
			}
		}
		
	}
	
	int ma1 = 0;
	
	for(int i = 1; i <= n; i ++) {
		ma1 = max(ma1, dist[i]);
	}

	return ma1;

}

int main(){
	cin >> n >> m;
	while(m --){
		int a, b, c;
		cin >> a >> b >> c;
		g[a].push_back({c, b});
		g[b].push_back({c, a});
	}
	
	int max1 = 0;
	for(int i = 1; i <= n; i ++){
		max1 = max(max1, dijks(i));
	}
	cout << max1;
	

	
	
}
2024/1/20 18:09
加载中...