-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1005_1.c
109 lines (89 loc) · 1.95 KB
/
1005_1.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define BLD_NUM 100
int search(int (*mptr)[BLD_NUM], int bptr[], int dp[], int node, int desti)
{
int stack[1000], rear=0, i=0, max[1000]={0}, next = desti;
while(1)
{
while(i < node)
{
/* if(dp[next] != -1)
{
if(max[stack[--rear]] < dp[next])
max[stack[rear]] = dp[next];
i = next + 1;
next = stack[rear];
}*/
if(mptr[i][next])
{
if(dp[i] != -1)
{
if(dp[i] > max[next])
max[next] = dp[i];
i++;
continue;
}
stack[rear++] = next;
next = i;
i=0;
continue;
}
i++;
}
// if(dp[next] < bptr[next] + max[next])
dp[next] = bptr[next] + max[next];
if(!rear)
return dp[desti];
if(max[stack[--rear]] < dp[next])
max[stack[rear]] = dp[next];
i = next+1;
next = stack[rear];
}
/*
int i, max = 0, result;
if(dp[desti])
return dp[desti];
for(i=0; i<node; i++)
if(mptr[i][desti])
{
result = search(mptr, bptr, dp, node, i);
if(result > max)
max = result;
}
if(dp[desti] < bptr[desti] + max)
dp[desti] = bptr[desti] + max;
return dp[desti];
*/
}
int main(void)
{
int i, j, cnt, node, way, desti, x, y;
int bld[BLD_NUM];
int mat[BLD_NUM][BLD_NUM];
int dp[BLD_NUM];
scanf("%d", &cnt);
while(cnt--)
{
// memset(met, 0, sizeof(met)); more than needs
scanf("%d %d", &node, &way);
for(i=0; i<node; i++)
for(j=0; j<node; j++)
mat[i][j] = 0;
for(i=0; i<node; i++)
dp[i] = -1;
for(i=0; i<node; i++)
scanf("%d", &bld[i]);
for(i=0; i<way; i++)
{
scanf("%d %d", &x, &y);
mat[x-1][y-1] = 1;
}
scanf("%d", &desti);
printf("%d\n", search(mat, bld, dp, node, desti-1));
}
return 0;
}