求大佬江湖救急
  • 板块P1807 最长路
  • 楼主qlo_olp
  • 当前回复11
  • 已保存回复11
  • 发布时间2023/8/31 19:47
  • 上次更新2023/11/3 00:09:29
查看原帖
求大佬江湖救急
1001833
qlo_olp楼主2023/8/31 19:47

拓扑 67分 过不了3、5、6点

#include<bits/stdc++.h>
using namespace std;
const int N=1505;
const int M=50005;
long long way[N];
int n,m,cnt,head[N],in[N];
struct node{
	int to,W,next;
}e[M];
queue<int>q;

inline void add(int x,int y,int w)
    {
    	e[++cnt]=(node){y,w,head[x]};
    	head[x]=cnt;
	}
	
inline void topsort()
   {
   	q.push(1);
   	
   	while(!q.empty())
   	     {  
   	     	int v=q.front();
   	     	q.pop();
   	     	for(int i=head[v];i;i=e[i].next)
   	     	   { 
   	     	     int u=e[i].to;
   	     	   	 in[u]--;
				 way[u]=max(way[u],way[v]+e[i].W);
   	     	     if(in[u]==0) q.push(u);
				}
			}
   }
   
int main()
   {
   	cin>>n>>m;
   	int a,b,w;
   	for(int i=1;i<=m;i++)
   	   {
   	   	cin>>a>>b>>w;
   	   	add(a,b,w);
   	   	in[b]++;
		  }
	topsort();
	if(!way[n]) cout<<-1;
	else cout<<way[n];
	return 0;
   }
2023/8/31 19:47
加载中...