标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586
题意:
#include <stdio.h> #include <queue> #include <math.h> #include <string.h> #include <algorithm> using namespace std; const int N = 1007; typedef struct Edge { int w; int to; int next; }edge; edge eg[N * 10]; int n, m, head[N], isVis[N], in[N], out[N], val[N]; //val记录每个状态的时间,记录到达当前状态最长的时间 queue <int> q; int criticalPath() { int max = -1; while (!q.empty()) q.pop(); memset(val, 0, sizeof(val)); for (int i = 0; i < n; i++) { if (!in[i]) { val[i] = 1; q.push(i); isVis[i] = 1; } } while (!q.empty()) { int temp = q.front(); q.pop(); if (val[temp] > max) max = val[temp]; int r = head[temp]; while (r != -1) { in[eg[r].to]--; if (val[eg[r].to] < val[temp] + eg[r].w - 1) val[eg[r].to] = val[temp] + eg[r].w - 1; if (!in[eg[r].to] && !isVis[eg[r].to]) { val[eg[r].to]++; isVis[eg[r].to] = 1; q.push(eg[r].to); } r = eg[r].next; } } return max; } int main() { while (scanf("%d%d", &n, &m) != EOF) { memset(isVis, 0, sizeof(isVis)); memset(head, -1, sizeof(head)); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &v, &u, &w); eg[i].to = v; eg[i].w = w; in[v]++; out[u]++; eg[i].next = head[u]; head[u] = i; } printf("%d\n", criticalPath()); } return 0; }
hdu Instrction Arrangement(关键路径 + 拓扑排序)
标签:
原文地址:http://www.cnblogs.com/burning-flame/p/5760356.html