dfs得91pts求调
查看原帖
dfs得91pts求调
690127
Xuzhenan520楼主2024/9/1 22:45

测试点信息 https://www.luogu.com.cn/record/175641966

#include<bits/stdc++.h>

using namespace std;

const int MAXN = 1e5;

int f[MAXN+5];

int n,m,maxn=-(1<<30);

struct N0DE{
	
	int v,w;
	
};

vector<N0DE> u[5*MAXN+50];

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

void dfs(int x,int ans){
	
	if(x==n){
		
		maxn=max(maxn,ans);
		
		return ;
		
	}
	
	for(int i=0;i<u[x].size();i++){
		
		if(f[u[x][i].v]<ans+u[x][i].w){
			
			f[u[x][i].v]=ans+u[x][i].w;
			
			dfs(u[x][i].v,ans+u[x][i].w);
			
		}
		
	}
	
	return ;
	
}

int main(){
	
	n=read(); m=read();
	
	for(int i=1;i<=m;i++){
		
		int uu;N0DE k;
		
		uu=read(); k.v=read(); k.w=read();
		
		u[uu].push_back(k);
		
	}
	
	dfs(1,0);
	
	if(maxn==-(1<<30)) cout<<-1;
	
	else cout<<maxn;
	
}
2024/9/1 22:45
加载中...