Test for Job
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 8990 |
|
Accepted: 2004 |
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 integersNandm(1≤N≤100000, 0≤m≤1000000) indicating the number of cities and roads. The nextNLines each contain a single integer. TheITh line describes the net profit of the cityI,Vi(0≤|Vi| ≤20000) The next m lines each contain the integersx,yIndicating that there are a road leads from the cityxTo Cityy. 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 Source field=source&key=poj+monthly--2007.07.08 "style=" Text-decoration:none ">poj Monthly--2007.07.08, deciduous snow |
Test instructions
Give you a picture of a starting point (in degrees 0) to the end of the road (out of 0). Satisfies the sum of all points of the Val max.
Ideas:
Start thinking is SPFA. Then the endless tle, later with the memory of the search.
One simple little deal is to add a source point to all of the starting points of the build edge.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define MAXN 100005# Define MAXN 2000005#define INF 0x3f3f3f3ftypedef Long Long ll;using namespace Std;ll n,m,ans,cnt,sx,oo;bool vis[maxn];ll D P[maxn],head[maxn];ll in[maxn],val[maxn];struct node{ll V,w,next;} Edge[maxn];void Addedge (ll u,ll v,ll W) {cnt++; Edge[cnt].v=v; Edge[cnt].w=w; Edge[cnt].next=head[u]; head[u]=cnt;} ll Dfs (ll u) {if (Dp[u]!=oo) return dp[u]; ll i,j,t,v,best=oo,flg=0; for (I=head[u];i;i=edge[i].next) {flg=1; V=EDGE[I].V; DFS (v); Best=max (Best,dp[v]); } if (FLG) dp[u]=best+val[u]; else dp[u]=val[u];} int main () {ll i,j,u,v,w; oo=-(1LL<<50); while (~SCANF ("%lld%lld", &n,&m)) {for (i=1;i<=n;i++) {scanf ("%lld", &val[i]); } cnt=0; for (i=0;i<=n;i++) head[i]=in[i]=0; for (i=1;i<=m;i++) {scanf ("%lld%lld", &u, &v); Addedge (U,v,val[u]); in[v]++; } for (i=1;i<=n;i++) {if (in[i]==0) Addedge (0,i,0); } for (i=0;i<=n;i++) Dp[i]=oo; DFS (0); printf ("%lld\n", dp[0]); } return 0;}
POJ 3249 Test for Job (Dag's longest-way memory search resolution)