HDU_4019 Instrction Arrangement

 Warmup for 2011 Asia Regional Chengdu (Alibaba精英赛题目重现,SJTU命题)——Problem 1009 

/*
比赛没做,周六下午做zoj的月赛做的想吐,周日一点心情也没有。
今天挺师兄说这题是dp的,就做了做。其实更像拓扑排序,大题思路就是把
所有入度为0的点入栈,然后出栈。本体多组数据,WA了一晚上!妹的!
*/

//My Code:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1010;

long long pre[N][N];
int in[N], sta[N];
long long dp[N];

int main() {
//freopen("data.in", "r", stdin);

int n, m, top = 0;
int x, y, z, i;
long long max;
while(~scanf("%d%d", &n, &m)) {

memset(pre, 0, sizeof(pre));
memset(dp, 0, sizeof(dp));
memset(in, 0, sizeof(in));
memset(sta, 0, sizeof(sta));

while(m--) {
scanf("%d%d%d", &x, &y, &z);
pre[y][x] = z; in[y]++;
}

for(i = 0; i < n; i++)
if(in[i] == 0) {
dp[i] = 1; sta[top++] = i;
}

while(top != 0) {
x = sta[--top];
for(i = 0; i < n; i++) {
if(pre[i][x] != 0) {
dp[i] = dp[i] > pre[i][x] + dp[x] ? dp[i] : pre[i][x] + dp[x];
in[i]--; pre[i][x] = 0;
if(in[i] == 0) sta[top++] = i;
}
}
}

for(max = 0, i = 0; i < n; i++) {
max = max > dp[i] ? max : dp[i];
}

printf("%lld\n", max);
}
return 0;
}



原文地址:https://www.cnblogs.com/vongang/p/2232277.html