性感 90pts SPFA 代码在线求调
查看原帖
性感 90pts SPFA 代码在线求调
382825
GDSYZX楼主2023/1/13 10:23

rt,忽略标题。

INF 设了 2147483647,可还是 WA on #3

#include <iostream>
#include <iomanip> 
#include <cmath> 
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define IL inline
using namespace std;
const int N = 1e4 + 10;
const int INF = 0x3f3f3f3f;

struct node
{
	long long u, w;
};

vector <node> g[N];
queue <long long> q;
long long dp[N];
bool vis[N];

IL int read() 
{
    int x = 0,f = 1;
    char c = getchar();
    while(c <'0'|| c >'9'){if(c == '-') f = -1;c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar();
    return x * f;
}

void write(int x) 
{
    if(x < 0) putchar('-'),x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
	long long n, m, s;
	cin >> n >> m >> s;
	for(long long i = 1;i <= m;i++)
	{
		long long a, b, c;
		cin >> a >> b >> c;
		node t;
		t.u = b;
		t.w = c;
		g[a].push_back(t);
	}
	for(long long i = 1;i <= n;i++)
	{
		dp[i] = 2137483647ll;
		vis[i] = false;
	}
	vis[s] = true;
	q.push(s);
	dp[s] = 0ll;
	while(!q.empty())
	{
		long long u = q.front();
		q.pop();
		vis[u] = false;
		for(long long i = 0;i < g[u].size();i++)
		{
			node v = g[u][i];
			if(dp[v.u] > dp[u] + v.w)
			{
				dp[v.u] = dp[u] + v.w;
				if(!vis[v.u])
				{
					q.push(v.u);
					vis[v.u] = true;
				}
			}
		}
	}
	for(long long i = 1;i <= n;i++)
	{
		cout << dp[i] << " ";
	}
	return 0;
}
2023/1/13 10:23
加载中...