Test for Job (poj 3249 memory-based search)

Source: Internet
Author: User

Test for Job (poj 3249 memory-based search)

 

Language:DefaultTest for Job
Time Limit:5000 MS   Memory Limit:65536 K
Total Submissions:9733   Accepted:2245

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 integersNAndM(1 ≤N≤ 100000, 0 ≤M≤ 1000000) indicating the number of cities and roads.
The nextNLines each contain a single integer.ITh line describes the net profit of the cityI,Vi(0 ≤ |Vi| ≤ 20000)
The next m lines each contain two integersX,YIndicating that there is a road leads from cityXTo cityY. 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, fallen leaf snow


 

A directed graph with n vertices and m edges. Each vertex has the right value. Now, the directed graph is sent from a vertex with zero inbound degree to a vertex with zero outbound degree, calculates the maximum sum of weights on a path.

Idea: if there are many points, you must not use a matrix to store a graph. you need to use an adjacent table. When creating a graph, count the points with zero input, and start dfs from this point, find the maximum value that can be obtained from this point.

Code:

 

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     #include 
     
      #include
      #include 
       
        #include 
        
         #include 
         
          #include 
          
           #pragma comment (linker,/STACK:102400000,102400000)#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf(%d, &n)#define sff(a,b) scanf(%d %d, &a, &b)#define sfff(a,b,c) scanf(%d %d %d, &a, &b, &c)#define pf printf#define DBG pf(Hi)typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 100000+10;const int MAXN = 1000000+10;const int N = 1005;struct Edge{ int u,v,next;}edge[MAXN];int num,head[maxn];int weight[maxn],in[maxn];int n,m;int vis[maxn];void init(){ num=0; mem(head,-1); mem(vis,0); mem(in,0);}void addedge(int u,int v){ edge[num].u=u; edge[num].v=v; edge[num].next=head[u]; head[u]=num++;}int dfs(int u){ if (vis[u]) return vis[u]; int Max=-INF; for (int i=head[u];~i;i=edge[i].next) { int v=edge[i].v; Max=max(Max,dfs(v)); } if (Max==-INF) Max=0; vis[u]=Max+weight[u]; return vis[u];}int main(){#ifndef ONLINE_JUDGE freopen(C:/Users/lyf/Desktop/IN.txt,r,stdin);#endif int i,j,u,v; while (~sff(n,m)) { init(); for (i=1;i<=n;i++) sf(weight[i]); for (i=0;i
           
            

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.