Test for Job
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 9512 |
|
Accepted: 2178 |
Description
Mr.dog was fired by he company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job, since there is swelling numbers of the unemployed. So some companies often use hard tests for their recruitment.
The test is like this:starting from a source-city, and you could pass through some directed roads to reach another city. Each time your reach a city, you can earn some profit or pay some fee, let this process continue until you reach a target-c ity. The boss would compute the expense you spent for your trips and the profit you have just obtained. Finally, he'll decide whether you can be hired.
In order to get the job, Mr.dog managed to obtain the knowledge of the net profitVi of all cities he may reach (a Negative Vi indicates is spent rather than gained) and the connection between cities. A city with no roads leading to it are a source-city and a city with no roads leading to other cities are a target-city. The mission of Mr.dog is to start from a source-city and choose a route leading to a target-city through which he can get The maximum profit.
Input
The input file includes several test cases.
The first line of all test case contains 2 integers
n and
m(1≤
n ≤ 100000, 0≤
m ≤10 00000) indicating the number of cities and roads.
The next
n lines each contain a single integer. The
ith line describes the net profit of the city
I,
Vi (0≤|
Vi| ≤20000)
The next m lines each contain and integers
x,
y indicating that there are a road leads
from city X to City
y. It is guaranteed, the each road appears exactly once, and the there is no-a-to-return to a-previous city.
Output
The output file contains one line for each test cases, in which contains an integer indicating the maximum profit Dog is able to obtain (or the minimum expenditure to spend)
Sample Input
6 51223341 21 32 43 45 6
Sample Output
7
Hint
Title Link: http://poj.org/problem?id=3249
The main idea: to give more trees, the maximum value from root to leaf
Topic Analysis: Tears Ben, and South Mail 2015 school match d similar, also than that difficult, the topic must use the memory search to do, and not a tree, first with the IND array statistics root number and the degree, and then from each root memory search whole tree get a maximum value, note here Benquan may be negative, did not notice WA two hair
#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std; int const MAX = 1e5 + 5;int Const INF = 2147483640;int N, m;int Dp[max], Val[max], ind[max];vector <int> vt[max];int DFS (int x) {if (dp[x]) return dp[x]; int sz = Vt[x].size (); if (sz = = 0) return val[x]; int tmp =-inf; for (int i = 0; i < sz; i++) {int u = vt[x][i]; TMP = MAX (TMP, val[x] + DFS (u)); } return dp[x] = tmp;} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {for (int i = 1; I <= n; i++) vt[i].cl Ear (); memset (Ind, 0, sizeof (IND)); memset (DP, 0, sizeof (DP)); for (int i = 1; I <= n; i++) scanf ("%d", &val[i]); for (int i = 0; i < m; i++) {int u, v; scanf ("%d%d", &u, &v); Vt[u].push_back (v); ind[v]++; } int ans =-inf; for (int i = 1; I <= N;i++) if (!ind[i]) ans = max (ans, DFS (i)); printf ("%d\n", ans); }}
POJ 3249 Test for Job (memory search good title)