SOJ 12261 Milk Scheduling

题意:求拓扑结构中的最长路径

用删除源点法,出队的时候更新邻接点的最大值。

// Problem#: 12261
// Submission#: 3236983
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i, s, t) for(int (i)=(s);(i)>=(t);--(i))

typedef long long LL;

const int maxn = 10005;

struct Edge {
    int from, to;
};
vector<Edge> E;
vector<int> g[maxn];

int n, m;
int t[maxn];
int in[maxn];
int d[maxn];

int main() {
   // freopen("input.in", "r", stdin);
    while (cin >> n >> m) {
        memset(in, 0, sizeof(in));
        REP(i, 1, n) g[i].clear();
        REP(i, 1, n) {
            cin >> t[i];
        }
        int u, v;
        REP(i, 1, m) {
            cin >> u >> v;
            //cout << u << ' ' << v << endl;
            E.push_back((Edge){u, v});
            in[v]++;
            g[u].push_back(E.size()-1);
        }
        queue<int> Q;
        REP(i, 1, n)
            if (in[i] == 0)
                Q.push(i);
        memset(d, 0, sizeof(d));
        int ans = 0;
        while (Q.empty() == false) {
            int fr = Q.front();Q.pop();
            d[fr] += t[fr];
            ans = max(ans, d[fr]);
            int sz = g[fr].size();
            REP(i, 0, sz-1) {
                int id = g[fr][i];
                int v = E[id].to;
                d[v] = max(d[v], d[fr]);
                if (--in[v] == 0) Q.push(v);
            }
        }
        cout << ans << endl;
    }
    return 0;
}                                 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值