poj 3249 Test for Job (记忆化深搜)
http://poj.org/problem?id=3249
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
Output
Sample Input
6 5 1 2 2 3 3 4 1 2 1 3 2 4 3 4 5 6
Sample Output
7
Hint
![](http://poj.org/images/3249_1.gif)
Source
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\n",ans); 68 } 69 return 0; 70 }
附:TLE代码:(广搜的超时代码)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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\n",ans); 84 } 85 return 0; 86 }