SPTA TLE 40分求调(玄关)
查看原帖
SPTA TLE 40分求调(玄关)
780514
N1tr0us_Acid楼主2024/7/9 15:22
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int M = 5e5 + 5, N = 1e4 + 10;

int n, m, s;
int h[M], w[M], e[M], ne[M], idx;
int dist[N];
bool st[N];

void add(int a, int b, int c) {
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

int SPFA(int en) {
	memset(dist, 0x3f, sizeof(dist));
	memset(st, 0, sizeof(st));
	dist[s] = 0;
	
	queue <int> q;
	q.push(s);
	
	st[s] = 1;
	
	while (!q.empty()) {
		int t = q.front();
		q.pop();
		
		st[t] = 0;
		
		for (int i = h[t]; i != -1; i = ne[i]) {
			int j = e[i];
			
			if(dist[j] > dist[t] + w[i]) {
			    dist[j] = dist[t] + w[i];
			    
				if(!st[j]) {
					q.push(j);
					st[j] = 1;
				}
			}
		}
	}
	
	if(dist[en] == 0x3f3f3f3f) return 2147483647;
	return dist[en];
}

int main (void) {
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin >> n >> m >> s;
	
	memset(h, -1, sizeof(h));
	
	while(m --) {
		int a, b, c;
		
		cin >> a >> b >> c;
		add(a, b, c);
	}
	
	for (int i = 1; i <= n; i ++) {
	    int t = SPFA(i);
	
    	cout << t << ' ';
	}
	
	
	return 0;
}
2024/7/9 15:22
加载中...