求助
  • 板块P1807 最长路
  • 楼主Owenzjg
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/10/27 12:49
  • 上次更新2023/10/27 05:38:54
查看原帖
求助
515971
Owenzjg楼主2022/10/27 12:49
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct node {ll u,v;};
bool operator < (const node& a,const node& b){
	if(a.v==b.v) return a.u>b.u;
	return a.v<b.v;
	//从大到小排序
}
#define maxn 1000+10

priority_queue <node> q;
vector <node> g[maxn];
bool vis[maxn];
ll f[maxn];
void d(ll x){
	memset(f,-0x3f3f3f3f,sizeof(f));
	//cout<<f[0]<<endl;
	q.push( (node){x,0} );
	f[x]=0;
	
	while(!q.empty()){
		node t=q.top(); q.pop();
		ll u=t.u;
		
		if(vis[u]) continue;
		else vis[u]=1;
		
		for(int i=0;i<(int)g[u].size();i++){
			node to=g[u][i];
			if(f[to.u]<f[u]+g[u][i].v){
				f[to.u]=f[u]+to.v;
				q.push( (node){to.u,f[to.u]} );
			}
		}
		
	}
	
}

int main(){
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(ll i=0;i<m;i++){
		ll x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		g[x].push_back( (node){y,z} );
	}
	d(1);
	if(f[n]==-0x3f3f3f3f) printf("-1");
	else printf("%lld",f[n]);
	return 0;
}

2022/10/27 12:49
加载中...