POJ 3249 Test for Job【DAG图上的最短路问题】

Test for Job

 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10381 Accepted: 2421

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 iVi (0 ≤ |Vi| ≤ 20000)
The next m lines each contain two integers xy 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

根据这样一句话：

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.可以判断出这个图保证是一个有向无环图（DAG图）。那么按照其拓扑排序得到的顺序来对点进行松弛就能解决DAG图上的最短路问题。

1、对于其多源点的问题，我们就不用多考虑了，所得到的拓扑排序的结果进行层次性的松弛，是相当于从树根一直dp到树叶的过程。

2、对于给出的点权值，我们在初始的时候给dis【s（起点）】赋值的时候，dis【s（起点）】=val【s（起点）】即可。这样我们dis【v】=dis【u】+val【v】就相当于在求边权值的松弛了。

AC代码：

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct EdgeNode
{
int to;
int w;
int next;
}e[200005*20];
int vis[200007];
int val[200007];
int degree[200007];
int dis[200007];
int ans[200007];
int n,m;
int cont;
{
e[cont].to=to;
}
void DAG()
{
int cont2=0;
queue<int >s;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)dis[i]=-0x3f3f3f3f;
for(int i=0;i<n;i++)
{
if(degree[i]==0)
{
dis[i]=val[i];
vis[i]=1;
s.push(i);
}
}
while(!s.empty())
{
int u=s.front();
ans[cont2++]=u;
s.pop();
{
int v=e[k].to;
if(degree[v]--)
{
if(degree[v]==0&&vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
for(int i=0;i<n;i++)
{
int u=ans[i];
{
int v=e[k].to;
if(dis[v]<dis[u]+val[v])
{
dis[v]=dis[u]+val[v];
}
}
}
int output=-0x3f3f3f3f;
for(int i=0;i<n;i++)
{
output=max(output,dis[i]);
}
printf("%d\n",output);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cont=0;
memset(degree,0,sizeof(degree));
for(int i=0;i<n;i++)
{
scanf("%d",&val[i]);
}
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x--;y--;
degree[y]++;
}
DAG();
}
}


• 本文已收录于以下专栏：

POJ 3249 Test for Job（拓扑排序+dp）

Description 给出一张有向图，每个点有点权，现要从一条入度为0的点到一个出度为0的点，问最大点权和 Input 第一行两个整数n和m分别表示点数和边数，之后n行每行一个整数表示该点点权...
• V5ZSQ
• 2016年03月15日 11:12
• 577

POJ 3249 Test for Job DAG图上的单源最短路径

• Dinivity123
• 2013年10月24日 11:16
• 825

poj 3249 Test for Job 图上dp（记忆化搜索）

• sepNINE
• 2014年12月15日 18:49
• 623

POJ 3249 Test for Job DAG图单源最短路

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10651   ...
• LuRiCheng
• 2016年11月13日 15:55
• 240

【DAG && 拓扑】POJ - 3249 Test for Job

Problem Description 说的是一个人去找工作遇到了一道面试题，面试官要求给出一些城市和城市之间的道路，没到达一个城市，可能会赚一些钱，但是也可能会有损失。最终面试者的总所得会决定他...
• bbbbswbq
• 2017年08月02日 19:29
• 66

DAG图上单源最短路径 poj 3249

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7475   A...
• yumao19921006
• 2013年01月24日 11:19
• 196

POJ 3249 Test for Job

• u011074149
• 2014年04月15日 16:22
• 489

POJ 3249 Test For Job

• scorpiocj
• 2011年02月14日 22:13
• 701

poj 3249 Test for Job

• 2014年11月30日 21:37
• 219

DAG最短路算法

#include #include #include #include #include using namespace std; #define INF 1000000 int d[100...
• qq_24667639
• 2015年07月22日 18:59
• 858

举报原因： 您举报文章：POJ 3249 Test for Job【DAG图上的最短路问题】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)