求助8分但是代码感觉不出来问题,spfa星存图
查看原帖
求助8分但是代码感觉不出来问题,spfa星存图
312605
Megumimwf楼主2023/2/1 23:48
#include<bits/stdc++.h>
#define ll long long
using namespace std;

inline ll read() {
	ll 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 * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}

struct edge {
	ll to, next, dis;
};
edge a[1000086];
ll n, w, tot, head[1086], dis[100086], vis[1086];
queue<ll> q;

inline void add(ll u, ll v, ll w) {
	tot++;
	a[tot].next = head[u];
	a[tot].to = v;
	a[tot].dis = w;
	head[u] = tot;
}

void spfa(ll s) {
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		for(int i=head[u]; i; i=a[i].next) {
			int v = a[i].to;
			if(dis[v] > dis[u] + a[i].dis) {
				dis[v] = dis[u] + a[i].dis;
				if(!vis[v]){
					q.push(v);
					vis[v] = 1;
				}
			}
		}
		q.pop();
		vis[u] = false;
	}
}

int main() {
	n = read();
	for(ll i=1; i<=n; ++i) {
		for(ll j=i+1; j<=n; ++j) {
			w = read();
			add(i, j, w);
		}
	}
	memset(dis, 0x3f3f, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	spfa(1);
	cout<<dis[n];
	return 0;
}
2023/2/1 23:48
加载中...