27分求助
查看原帖
27分求助
819216
csh7306jjj楼主2024/8/27 14:48
#include<bits/stdc++.h>
#define int long long
#define D(x) cout<<#x<<": "<<x<<endl;
using namespace std;
inline int R() {int x = 0;bool f = 1;char ch = getchar();while (!isdigit(ch)) f = ch == '-' ? 0 : 1, ch = getchar();while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();return f ? x : -x;}
const int N=4e4+10;
int T,n,m,k,vis[N],d[N],cnt[N];
struct node{
	int v,w;
};
vector<node> g[N];
queue<int> q;
bool spfa(int s){
	memset(d,0x4f,sizeof(d));
	memset(vis,0,sizeof(vis));
	d[s]=0;
	vis[s]=1;
	while(q.size()){
		int u=q.front();
		q.pop(); 
		vis[u]=0;
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i].v;
			int w=g[u][i].w;
			if(d[v]>d[u]+w){
				d[v]=d[u]+w;
				if(!vis[v]){ 
					q.push(v);
					vis[v]=1;
					cnt[v]=cnt[u]+1;
					if(cnt[v]>=n+1) return false;
				}		
			}
		}
	}
	return true;
}
signed main(){
	n=R(),m=R();
	for(int i=1;i<=m;i++){
		int u=R(),v=R(),w=R();
		g[u].push_back({v,w});
	}
	for(int i=1;i<=n;i++){
		q.push(i);
		cnt[i]=vis[i]=1;
	}
	if(!spfa(1)){
		cout<<"NO"<<endl;
		return 0;
	}
	for(int i=1;i<=n;i++) cout<<d[i]<<" "; 
	return 0;
}

请问这题27分怎么办

2024/8/27 14:48
加载中...