poj 3249 Test for Job (记忆化深搜)

http://poj.org/problem?id=3249

Test for Job
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8206   Accepted: 1831

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 fee, 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 profit Vi of all cities he may reach (a negative Vi indicates 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 includes several test cases.  The first line of each test case contains 2 integers n and m(1 ≤ n ≤ 100000, 0 ≤ m ≤ 1000000) 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 two integers x, y indicating that there is a road leads from city x to 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 5
1
2
2
3
3
4
1 2
1 3
2 4
3 4
5 6

Sample Output

7

Hint

Source

 
【题解】:记忆化深搜,广搜超时,必须记忆化
【code】:
 1 /**
 2 status:Accepted      memory:13936K
 3 time:2704MS     language:G++
 4 code length:1251B  author:cj
 5 */
 6 
 7 #include<iostream>
 8 #include<stdio.h>
 9 #include<algorithm>
10 #include<vector>
11 #include<queue>
12 #include<string.h>
13 
14 #define N  100010
15 #define INF 1000000000
16 using namespace std;
17 
18 vector<int> vct[N]; //保存图用的vector
19 
20 int v[N],in[N],rem[N];  // 城市价值v[N],节点入度in[N],记忆化数组rem[N]
21 
22 int dfs(int s)  //记忆化深搜
23 {
24     if(rem[s]!=-INF)
25         return rem[s];  //当搜索到曾经到过的点直接返回rem值就行了
26     rem[s] = v[s];
27     int i,ans=-INF;
28     for(i=0;i<vct[s].size();i++)
29     {
30         int temp = dfs(vct[s][i]);  //下一层的搜索
31         if(ans<temp)
32             ans = temp;
33     }
34     if(ans!=-INF)
35         rem[s]+=ans;  //回溯累计
36     return rem[s];
37 }
38 
39 int main()
40 {
41     int n,i,m;
42     while(~scanf("%d%d",&n,&m))
43     {
44         for(i=1;i<=n;i++)
45         {
46             scanf("%d",v+i);
47             vct[i].clear();  //初始化地图
48             rem[i] = -INF;
49         }
50         memset(in,0,sizeof(int)*(n+1));
51         for(i=1;i<=m;i++)
52         {
53             int a,b;
54             scanf("%d%d",&a,&b);
55             in[b]++;  //入度统计
56             vct[a].push_back(b);
57         }
58         int ans = -INF;
59         for(i=1;i<=n;i++)
60         {
61             if(in[i]==0)  //入度为0表示起始点
62             {
63                 int temp = dfs(i);
64                 if(ans<temp)    ans = temp;
65             }
66         }
67         printf("%d
",ans);
68     }
69     return 0;
70 }

附:TLE代码:(广搜的超时代码)

 1 /**
 2 status:Time Limit Exceeded            
 3 language:G++    codelength:1646B
 4 author:cj
 5 */
 6 
 7 #include<iostream>
 8 #include<stdio.h>
 9 #include<algorithm>
10 #include<vector>
11 #include<queue>
12 #include<string.h>
13 
14 #define N  100010
15 #define INF 1000000000
16 using namespace std;
17 
18 struct Nod
19 {
20     int id,sum;
21 }nd1,nd2;
22 vector<int> vct[N];
23 int v[N],in[N],out[N],visit[N];
24 
25 int bfs(int s)
26 {
27     queue<Nod> q;
28     nd1.id = s;
29     nd1.sum = v[nd1.id];

30     q.push(nd1);
31     visit[s]=1;
32     int maks = nd1.sum;
33     while(!q.empty())
34     {
35         nd2 = q.front();
36         q.pop();
37         if(!out[nd2.id]&&maks<nd2.sum)    maks = nd2.sum;
38         int i;
39         for(i=0;i<vct[nd2.id].size();i++)
40         {
41             nd1.id = vct[nd2.id][i];
42             nd1.sum = nd2.sum + v[nd1.id];
43             if(visit[nd1.id]<in[nd1.id])
44             {
45                 q.push(nd1);
46                 visit[nd1.id]++;
47             }
48         }
49     }
50     return maks;
51 }
52 
53 int main()
54 {
55     int n,i,m;
56     while(~scanf("%d%d",&n,&m))
57     {
58         for(i=1;i<=n;i++)
59         {
60             scanf("%d",v+i);
61             vct[i].clear();
62         }
63         memset(in,0,sizeof(int)*(n+1));
64         memset(out,0,sizeof(int)*(n+1));
65         for(i=1;i<=m;i++)
66         {
67             int a,b;
68             scanf("%d%d",&a,&b);
69             out[a]++;
70             in[b]++;
71             vct[a].push_back(b);
72         }
73         int ans = 0;
74         for(i=1;i<=n;i++)
75         {
76             if(in[i]==0)
77             {
78                 memset(visit,0,sizeof(int)*(n+1));
79                 int temp = bfs(i);
80                 if(ans<temp)    ans = temp;
81             }
82         }
83         printf("%d
",ans);
84     }
85     return 0;
86 }
View Code
原文地址:https://www.cnblogs.com/crazyapple/p/3243377.html