dijkstra求条
  • 板块灌水区
  • 楼主monodev
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/9/8 15:20
  • 上次更新2024/9/8 18:43:36
查看原帖
dijkstra求条
533102
monodev楼主2024/9/8 15:20
#include <iostream>
#include <vector>
using namespace std;

const int MAXN = 2500;
const long long INF = 1ll << 62;

int n, m, s, t;

struct edge {
	int to, w;
	edge(int to, int w) {
		this -> to = to;
		this -> w = w;
	}
};
vector<edge> graph[MAXN + 1];

long long dist[MAXN + 1];
bool vis[MAXN + 1];
void dijkstra(int root) {
	fill(dist + 1, dist + n + 1, INF);
	dist[root] = 0;
	
	for(int i = 0; i < n; ++ i) {
		int x = -1;
		for(int j = 1; j <= n; ++ j) {
			if(!vis[j] && (x==-1||dist[j]<dist[x])) {
				x = j;
			}
		}
		
		vis[x] = true;
		
		for(size_t j = 0; j < graph[x].size(); ++ j) {
			int y = graph[x][j].to;
			dist[y] = min(dist[y], dist[x] + graph[x][j].w);
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> m >> s >> t;
	for(int i = 0; i < m; ++ i) {
		int a, b, w;
		cin >> a >> b >> w;
		graph[a].push_back(edge(b, w));
	}
	
	dijkstra(s);
	cout << dist[t] << endl;
	
	return 0;
}
2024/9/8 15:20
加载中...