Test for job
Time limit:5000 Ms |
|
Memory limit:65536 K |
Total submissions:9201 |
|
Accepted:2080 |
Description
Mr. dog was fired by his 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 are 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, you may pass through some directed roads to reach another city. each time you reach a city, you can earn some profit or pay some traffic, let this process continue until you reach a target-city. the boss will compute the expense you spent for your trip and the profit you have just obtained. finally, he will decide whether you can be hired.
In order to get the job, mr. Dog managed to obtain the knowledge of the net profitVIOf all cities he may reach (a negativeVIIndicates that money is spent rather than gained) and the connection between cities. a city with no roads leading to it is a source-city and a city with no roads leading to other cities is 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 provided des several test cases.
The first line of each 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.
ITh line describes the net profit of the city
I,
VI(0 ≤ |
VI| ≤ 20000)
The next M lines each contain two integers
X,
YIndicating that there is a road leads from City
XTo City
Y. It is guaranteed that each road appears exactly once, and there is no way 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
Source
Poj monthly -- 2007.07.08: the greatest point right from a certain origin point to the destination point.
Dag:
#include <stdio.h>#include <string.h>#define inf 0x7fffffff#define maxn 100002#define maxm 1000002int id, cost[maxn], sta[maxn];struct Node2{ int v, first;} head[maxn];struct Node{ int to, next;} E[maxm];bool vis[maxn], in[maxn], out[maxn];void addEdge(int u, int v){ E[id].to = v; E[id].next = head[u].first; head[u].first = id++;}void getMap(int n, int m){ int i, u, v; for(i = 1; i <= n; ++i){ scanf("%d", &head[i].v); head[i].v = -head[i].v; in[i] = 0; head[i].first = -1; out[i] = 0; vis[i] = 0; cost[i] = inf; } for(i = id = 0; i < m; ++i){ scanf("%d%d", &u, &v); out[v] = 1; addEdge(v, u); in[u] = 1; }}void DAG(int n){ int i, u, v, id2 = 0, ans = inf, tmp; for(i = 1; i <= n; ++i) if(!in[i]){ sta[id2++] = i; vis[i] = 1; cost[i] = head[i].v; if(!out[i] && cost[i] < ans) ans = cost[i]; } while(id2){ u = sta[--id2]; vis[u] = 0; for(i = head[u].first; i != -1; i = E[i].next){ v = E[i].to; tmp = cost[u] + head[v].v; if(tmp < cost[v]){ cost[v] = tmp; if(!vis[v]){ vis[v] = 1; sta[id2++] = v; } if(!out[v] && tmp < ans) ans = tmp; } } } //if(ans < 0) ans = - ans; printf("%d\n", -ans);}int main(){ int n, m, i, u, v; while(scanf("%d%d", &n, &m) == 2){ getMap(n, m); DAG(n); } return 0;}
Put another re in the memory-based search... :
#include <stdio.h>#include <string.h>#define inf 0x7fffffff#define maxn 100002#define maxm 1000002int dp[maxn], id;struct Node2{ int first, v;} head[maxn];struct Node{ int to, next;} E[maxm];bool in[maxn], out[maxn];void addEdge(int u, int v){ E[id].to = v; E[id].next = head[u].first; head[u].first = id++;}void getMap(int n, int m){ int i, u, v; for(i = 1; i <= n; ++i){ scanf("%d", &head[i].v); head[i].v = -head[i].v; in[i] = out[i] = 0; dp[i] = inf; head[i].first = -1; } for(i = 0; i < m; ++i){ scanf("%d%d", &u, &v); addEdge(u, v); in[v] = 1; out[u] = 1; }}int DFS(int k){ if(dp[k] != inf) return dp[k]; int i, u, v, ans = inf, tmp; for(i = head[k].first; i != -1; i = E[i].next){ tmp = head[k].v + DFS(E[i].to); if(tmp < ans) ans = tmp; } return dp[k] = ans;}void solve(int n){ int i, u, v, ans = inf, tmp; for(i = 1; i <= n; ++i) if(!out[i]) dp[i] = head[i].v; for(i = 1; i <= n; ++i){ if(!in[i]){ tmp = DFS(i); if(tmp < ans) ans = tmp; } } printf("%d\n", -ans);}int main(){ int n, m; while(scanf("%d%d", &n, &m) == 2){ getMap(n, m); solve(n); } return 0;}