67分求助
  • 板块P1807 最长路
  • 楼主xueba1hao大聪明
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/8/24 12:08
  • 上次更新2023/11/3 01:33:51
查看原帖
67分求助
655072
xueba1hao大聪明楼主2023/8/24 12:08
#include <bits/stdc++.h>
using namespace std;

int ind[1505],maxn[1505];

struct k
{
  int a,num;
  k(int a1,int num1)
  {
    a=a1,num=num1;
  }
};

vector<k> p[1505];

int main()
{
  int n,m;
  cin>>n>>m;
  for(int i=1; i<=m; i++)
  {
    int u,v,w;
    cin>>u>>v>>w;
    p[u].push_back ({v,w});
    ind[v]++;
  }
  queue <int> q;
  for(int i=2; i<=n; i++)
    maxn[i]=-1e9;
  q.push(1);
  while(!q.empty())
  {
    int x=q.front();
    q.pop();
    for(int i=0; i<p[x].size(); i++)
    {
      int t=p[x][i].a,len=p[x][i].num;
      maxn[t]=max(maxn[t],maxn[x]+len);
      ind[t]--;
      if(!ind[t])
        q.push(t);
    }
  }
  if(maxn[n]!=-1e9)
    cout<<maxn[n]<<endl;
  else
    cout<<-1<<endl;
  return 0;
}

3,5,6测试点WA了

2023/8/24 12:08
加载中...