POJ 3249-Test for Job(拓撲排序&&DP)
Test for Job
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 10339 | Accepted: 2405 |
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 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
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
題目意思:
一個人去找工作,面試官給了他這道題:給出一些城市與城市之間的道路,每到達一個城市,就會獲得一些收益或是損失,求最大收益。
解題思路:
資料量巨大!用鏈式前向星方式儲存路徑,拓撲排序後,再使用用DAG單源最短路演算法。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define inf 2000000010
#define Maxn 100010
#define Maxm 1000010
using namespace std;
int n,i,j,e,num,Max;
int dist[Maxn],head[Maxn];//儲存儲存描述點vi邊資訊的鏈的起點在Edges陣列的位置
int value[Maxn],indegree[Maxn],outdegree[Maxn];//點值和入度出度
int ans[Maxn];//拓撲序列
struct Edge
{
//鏈式前向星 將新加入的節點鏈在對應鏈的最開始並修改head陣列的對應位置的值
int to;//終點
int val;//權值
int next;//指向下一條邊
} edge[Maxm];
void init()//初始化
{
memset(indegree,0,sizeof(indegree));
memset(outdegree,0,sizeof(outdegree));
memset(head,-1,sizeof(head));
memset(ans,0,sizeof(ans));
for(int i=0; i<=100010; i++)
dist[i]=-inf;
Max=-inf;
e=0;
num=1;
}
void addedge(int from,int to,int val)
{
edge[e].to=to;
edge[e].val=val;
edge[e].next=head[from];
head[from]=e++;
}
void Topsort()//拓撲排序
{
queue<int> q;
int now,adj;
for(i=1; i<=n; i++)
{
if(indegree[i]==0)
{
q.push(i);
dist[i]=value[i];
}
}
while(!q.empty())
{
now=q.front();
ans[num++]=now;
q.pop();
for(i=head[now]; i!=-1; i=edge[i].next)
{
adj=edge[i].to;
indegree[adj]--;
if(!indegree[adj])
q.push(adj);
}
}
}
void DAG()//DAG單源最短路
{
for(i=1; i<num; i++)
{
for(j=head[ans[i]]; j!=-1; j=edge[j].next)
{
dist[edge[j].to]=max(dist[edge[j].to],dist[ans[i]]+edge[j].val);
}
if(outdegree[ans[i]]==0)
{
if(dist[ans[i]]>Max)
Max=dist[ans[i]];
}
}
}
int main()
{
int m,i,a,b;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(i=1; i<=n; i++)
scanf("%d",&value[i]);
for(i=1; i<=m; i++)
{
scanf("%d%d",&a,&b);
addedge(a,b,value[b]);
indegree[b]++,outdegree[a]++;
}
Topsort();
DAG();
printf("%d\n",Max);
}
return 0;
}
相關文章
- 拓撲排序排序
- 【Tarjan 拓撲排序 dp】P3387 【模板】縮點排序
- 洛谷P3953 逛公園(dp 拓撲排序)排序
- 拓撲排序,YYDS排序
- 圖論——拓撲排序圖論排序
- 筆記:拓撲排序筆記排序
- 拓撲排序小結排序
- Leetcode 1691. 堆疊長方體的最大高度(拓撲排序 + DP)LeetCode排序
- Reward (圖論+拓撲排序)圖論排序
- AOV網與拓撲排序排序
- 【筆記/模板】拓撲排序筆記排序
- DFS實現拓撲排序排序
- 拓撲排序就這麼回事排序
- 演算法-圖論-拓撲排序演算法圖論排序
- 有向圖的拓撲排序——DFS排序
- (set+拓撲排序) CF1572A Book排序
- 圖解拓撲排序+程式碼實現圖解排序
- 圖的拓撲排序詳解與實現排序
- 拓撲排序 (BFS )DAG (有向無環圖)排序
- VOL.2 拓撲排序與關鍵路徑排序
- 拓撲排序詳解(梅開二度之dfs版按字典序輸出拓撲路徑+dfs版輸出全部拓撲路徑排序
- Noc拓撲
- 牛客 51011 可達性統計(拓撲排序,bitset)排序
- 【BZOJ-1565】植物大戰殭屍 拓撲排序 + 最小割排序
- POJ 3071 Football(概率DP)
- POJ 3267 The Cow Lexicon(dp)
- Day2 尤拉路,拓撲排序和差分約束排序
- StratoVirt 的 vCPU 拓撲(SMP)
- 網路拓撲結構
- 簡單dp -- Common Subsequence POJ - 1458
- POJ1390 Blocks (區間DP)BloC
- BZOJ2535: [Noi2010]Plane 航空管制2(拓撲排序 貪心)排序
- Android程式設計師會遇到的演算法(part 7 拓撲排序)Android程式設計師演算法排序
- 網路拓撲圖:網路拓撲圖介紹及線上製作
- 藍橋杯 卡勒沃夫之弱水路三千(提高型) 拓撲排序+Map排序
- 演算法資料結構 | 圖論基礎演算法——拓撲排序演算法資料結構圖論排序
- POJ3252Round Numbers(數位dp)
- Making the Grade POJ - 3666(離散化+dp)
- Istio全景監控與拓撲