28分求调玄关
  • 板块P1807 最长路
  • 楼主Kyleshao
  • 当前回复1
  • 已保存回复1
  • 发布时间2025/7/21 13:50
  • 上次更新2025/7/21 18:08:16
查看原帖
28分求调玄关
758513
Kyleshao楼主2025/7/21 13:50

link

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
struct node
{
    int v,w;
};
vector<node> a[100005];
queue<int> q;
int d[100005];
bool flag[100005];
int main()
{
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        a[u].push_back(node{v,w});
    }
    q.push(1);
    flag[1]=true;
    while (!q.empty())
    {
        int t;
        t=q.front();
        q.pop();
        for (int i=0;i<a[t].size();i++)
        {
            if (flag[a[t][i].v])    continue;
            q.push(a[t][i].v);
            d[a[t][i].v]=max(d[t]+a[t][i].w,d[a[t][i].v]);
            flag[a[t][i].v]=true;
        }
    }
    int maxn=0;
    for (int i=1;i<=n;i++)
    {
        maxn=max(maxn,d[i]);
    }
    cout<<maxn<<endl;
}


2025/7/21 13:50
加载中...