-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ-1949-Chores-dp-easy.txt
132 lines (122 loc) · 1.84 KB
/
POJ-1949-Chores-dp-easy.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
POJ-1949-Chores
2015-11-12 20:12:41
xuchen
**/
/**
* Topological Sort
**/
#include "stdio.h"
#include <cmath>
#include <algorithm>
#include <queue>
#include <stdlib.h>
#include <cstring>
using namespace std;
const int N = 10005;
struct Edge{
int to, next;
};
struct Node{
int _id;
int e;
int pres, v;
int d, f;
bool visited;
};
Edge edges[N*100];
Node heads[N];
int sortedChores[N];
int preVs[N];
int cnt;
int _time;
int ans;
int cmp(const void *a, const void *b)
{
return ((Node*)b)->f - ((Node*)a)->f;
}
void addEdge(int u, int v)
{
cnt++;
edges[cnt].to = v;
edges[cnt].next = heads[u].e;
heads[v].pres++;
heads[u].e = cnt;
}
void DFS_FIRST(int cur)
{
heads[cur].visited = true;
_time++;
heads[cur].d = _time;
int to;
for(int i=heads[cur].e; i!=0; i=edges[i].next)
{
to = edges[i].to;
if(heads[to].visited!=true)
DFS_FIRST(to);
}
_time++;
heads[cur].f = _time;
}
void DFS(int n)
{
for(int i=1; i<=n; i++)
{
if(heads[i].visited!=true)
{
DFS_FIRST(i);
}
}
}
void printSorted(int s, int e)
{
for(int i=s; i<=e; i++)
printf("%d ", heads[i]._id);
printf("\n");
}
void printF(int s, int e)
{
for(int i=s; i<=e; i++)
printf("%d ", heads[i].f);
printf("\n");
}
void topologicalSort(int s, int n)
{
//DFS(s);
//qsort(heads+1, n, sizeof(Node), cmp);
int to;
for(int i=1; i<=n; i++)
{
for(int j=heads[i].e; j!=0; j=edges[j].next)
{
to = edges[j].to;
if(heads[i].v+preVs[i]>preVs[to])
preVs[to] = heads[i].v+preVs[i];
}
}
for(int i=1; i<=n; i++)
{
if(heads[i].v+preVs[i]>ans)
ans = heads[i].v+preVs[i];
}
}
int main()
{
int n, v, pi, p;
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
heads[i]._id = i;
scanf("%d", &v);
heads[i].v = v;
scanf("%d", &pi);
for(int j=0; j<pi; j++)
{
scanf("%d", &p);
addEdge(p, i);
}
}
topologicalSort(1, n);
printf("%d\n", ans);
return 0;
}