/home/toolbox/public_html/solutions/123/12376/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <stdint.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 /* fprintf(stderr, "functionName: message", varslist); */
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2016-03-15
20 * Purpose: fun
21 * Problem: 12376
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define NODE_MAX 101
29
30 int numberOfTimes;
31 int n; /* number of nodes */
32 int m; /* number of edges */
33 int experience[NODE_MAX];
34 int tbl[NODE_MAX][NODE_MAX];
35
36 void init()
37 {
38 /* FUNCTION init */
39 scanf("%d ", &numberOfTimes);
40 } /* FUNCTION init */
41
42 void dump()
43 {
44 /* FUNCTION dump */
45 int i;
46 int j;
47
48 printf("\n");
49 for (i=0; i<n; i++)
50 {
51 /* for */
52 printf("%d :", experience[i]);
53 for (j=0; j<n; j++)
54 {
55 /* for */
56 printf("%3d", tbl[i][j]);
57 } /* for */
58 printf("\n");
59 } /* for */
60 } /* FUNCTION dump */
61
62 void getInput()
63 {
64 /* FUNCTION getInput */
65 int i;
66 int j;
67 int x;
68 int y;
69
70 scanf(" %d %d ", &n, &m);
71 for (i=0; i<n; i++)
72 {
73 /* get each experience and set each row to 0 */
74 scanf(" %d ", &experience[i]);
75 for (j=0; j<n; j++)
76 {
77 /* do all columns */
78 tbl[i][j] = 0;
79 } /* do all columns */
80 } /* get each experience and set each row to 0 */
81 /* now read in edges */
82 for (j=0; j<m; j++)
83 {
84 /* for each edge */
85 scanf(" %d %d ", &x, &y);
86 tbl[x][y] = 1;
87 } /* for each edge */
88 } /* FUNCTION getInput */
89
90 void process()
91 {
92 /* FUNCTION process */
93 int node = 0;
94 int gained = TRUE;
95 int i;
96 int mx;
97 int nxt = 0;
98 int tot = 0;
99
100 DEBUG dump();
101 while (gained)
102 {
103 /* while still gaining experience */
104 gained = FALSE;
105 mx = 0;
106 for (i=0; i<n; i++)
107 {
108 /* check each possible destination */
109 if ( 1 == tbl[node][i])
110 {
111 /* found a possible move */
112 gained = TRUE;
113 if (experience[i] > mx)
114 {
115 /* better choice */
116 nxt = i;
117 mx = experience[i];
118 } /* better choice */
119 } /* found a possible move */
120 } /* check each possible destination */
121 if (gained)
122 {
123 /* add up our experience */
124 tot = tot + mx;
125 node = nxt;
126 } /* add up our experience */
127 } /* while still gaining experience */
128 printf("%d %d\n", tot, node);
129 } /* FUNCTION process */
130
131 int main()
132 {
133 /* main */
134 int i;
135
136 init();
137 for (i=1; i<=numberOfTimes; i++)
138 {
139 /* while */
140 getInput();
141 printf("Case %d: ", i);
142 process();
143 } /* while */
144
145 return EXIT_SUCCESS;
146 } /* main */
147
148