Test for Job
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 10137 |
|
Accepted: 2348 |
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 profit Vi 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
Nand
m(1≤
N≤100000, 0≤
m≤1000000) indicating the number of cities and roads.
The next
NLines 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 the integers
x,
yIndicating that there are a road leads from the city
xTo 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 a ble to obtain (or the minimum expenditure to spend)
Sample Input
6 51223341 21 32 43 45 6
Sample Output
7
Hint
Topic Link: Click to open the link
Given the weights of n points and the M-path, ask what is the maximum weight of a road.
The starting point is 0, the point with all degrees of 0 is assigned to the DP array, the topology is sorted, and finally the point of 0 is traversed, and the maximum value is obtained.
AC Code:
#include "iostream" #include "Cstdio" #include "CString" #include "algorithm" #include "queue" #include "stack" #include " Cmath "#include" utility "#include" map "#include" set "#include" vector "#include" list "#include" string "using namespace Std;typedef Long Long ll;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const int MAXN = 1e5 + 5;int N, m, Num;int Cos T[MAXN], IN[MAXN], OUT[MAXN], HEAD[MAXN], dp[maxn];bool vis[maxn];struct node{/* Data */int fr, to, NXT;} E[MAXN * 10];void Add (int x, int y) {e[num].fr = X;e[num].to = Y;E[NUM].NXT = head[x];head[x] = num++;} void Toposort () {int cnt = 1;while (CNT < n) {for (int i = 1; I <= n; ++i) if (in[i] = = 0 &&!vis[i]) {Vis[i] = t rue;cnt++;for (int j = head[i]; J! =-1; j = e[j].nxt) {int x = e[j].to;in[x]--;if (Dp[i] + cost[x] > Dp[x]) dp[x] = Dp[i ] + cost[x];}}} int main (int argc, char const *argv[]) {while (scanf ("%d%d", &n, &m)! = EOF) {memset (in, 0, sizeof (in)); Memset (out, 0, sizeof (out)); Memset (Head,-1, sizeof (head)); Memset (vis, false, sizeof (VIS)), num = 1;for (int i = 1; I <= n; ++i) scanf ("%d", &cost[i]); for (int i = 1; I <= m; ++i) {int X, y;scanf ("%d%d", &x, &y); Add (x, y); in[y]++;out[x]++;} for (int i = 1; I <= n; ++i) if (in[i] = = 0) dp[i] = Cost[i];else Dp[i] =-inf;toposort (); int ans =-inf;for (int i = 1; i <= N; ++i) if (out[i] = = 0 && dp[i] > ans) ans = dp[i];p rintf ("%d\n", ans);} return 0;}
POJ3249 Test for Job (topology sort +DP)