Instructions Arrangement

#include<bits/stdc++.h>
using namespace std;

const int Max=1001;
const int INF=INT_MAX;

struct Edge {
    int to;
    int length;
    Edge(int x,int y):to(x),length(y) {
    }
};

vector<Edge> graph[Max];
int indegree[Max]= {0};
int el[Max]= {0};


void CritialPath(int n) {
    vector<int> topology;
    queue<int> q;
    for(int i=0; i<n; i++) {
        if(indegree[i]==0) {
            q.emplace(i);
            el[i]=1;
        }
    }
    while(!q.empty()) {
        int u=q.front();
        q.pop();
        topology.emplace_back(u);
        for(int i=0; i<graph[u].size(); i++) {
            int v=graph[u][i].to;
            int l=graph[u][i].length;
            indegree[v]--;
            if(indegree[v]==0) {
                q.emplace(v);
            }
            el[v]=max(el[v],el[u]+l);
        }
    }
    return ;
}

int main() {
    int n,m;
    while(cin>>n>>m) {
        memset(graph,0,sizeof(graph));
        for(int i=0; i<m; i++) {
            int from,to,length;
            cin>>from>>to>>length;
            graph[from].emplace_back(Edge(to,length));
            indegree[to]++;
        }
        CritialPath(n);
        int answer=0;
        for(int i=0; i<n; i++) {
            answer=max(answer,el[i]);
        }
        cout<<answer<<endl;
    }
    return 0;
}
全部评论
学习新的东西很好
点赞 回复 分享
发布于 2022-10-15 16:12 河南

相关推荐

做个有文化的流氓:幸遇良师,幸遇好的hr
找工作中的小确幸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务