码迷,mamicode.com
首页 > 编程语言 > 详细

计蒜课 16957 拓扑排序

时间:2018-06-13 00:59:33      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:ops   tps   拓扑   ems   push   sort   printf   pop   i++   

计蒜课
【代码】:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 10;
const int mod = 142857;
int t,n,m,k,x,u,v,w,num;
vector<pair<int,int> > G[maxn];
int inDeg[maxn], dp[maxn];
int virus[maxn];
queue<int> q;

void topSort()
{
    while(!q.empty()) q.pop();
    for(int i=1;i<=n;i++) if(!inDeg[i]) q.push(i);
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        for(int i=0;i<G[now].size();i++)
        {
            int nxt = G[now][i].first;
            if(--inDeg[nxt] == 0) q.push(nxt);
            dp[nxt] = max(dp[nxt], dp[now]+G[now][i].second);
        }
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(inDeg,0,sizeof(inDeg));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++) G[i].clear();
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            G[u].push_back(make_pair(v,w));
            inDeg[v]++;
        }
        topSort();
        int maxx = -1;
        for(int i=1; i<=n; i++) maxx = max(maxx,dp[i]);
        printf("%d\n",maxx);
    }
}

计蒜课 16957 拓扑排序

标签:ops   tps   拓扑   ems   push   sort   printf   pop   i++   

原文地址:https://www.cnblogs.com/Roni-i/p/9175743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!