POJ 3249 Test for Job【SPFA】
2016-06-01 19:29
281 查看
Test for Job
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
POJ Monthly--2007.07.08,
落叶飞雪
题目大意:给出n个点,m条边,每个点都提供了相对的点权值,然后给出相连着的边,问最大利润值。
思路:
1、SPFA求最长路,正向建边TLE,反向建边4000msAC。。。。
2、这里源点一定是入度为0的点,因为题干中说了,没有其他任何点有边有有向边连接源点,那么起点的选取一定是要入度为0的点才行,相应的,终点也一定是出度为0的点才行。
3、卡vector、卡cin、习惯用这两种来实现代码的,请使用链式前向星和scanf。
AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[200005];
struct EdgeNode
{
int to;
int w;
int next;
}e[200005*20];
int degree[200005];
int val[200005];
int vis[200005];
int dis[200005];
int n,m;
int cont;
void add(int from,int to)
{
e[cont].to=to;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA()
{
queue<int >s;
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();
s.pop();vis[u]=0;
for(int k=head[u];k!=-1;k=e[k].next)
{
int v=e[k].to;
if(dis[v]<dis[u]+val[v])//求最长路
{
dis[v]=dis[u]+val[v];
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
int output=-0x3f3f3f3f;
for(int i=0;i<n;i++)
{
if(head[i]==-1)
output=max(output,dis[i]);
}
printf("%d\n",output);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cont=0;
memset(vis,0,sizeof(vis));
memset(degree,0,sizeof(degree));
memset(head,-1,sizeof(head));
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--;
add(y,x);
degree[x]++;
}
SPFA();
}
}
Time Limit: 5000MS | | Memory Limit: 65536K |
Total Submissions: 10381 | | Accepted: 2421 |
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
POJ Monthly--2007.07.08,
落叶飞雪
题目大意:给出n个点,m条边,每个点都提供了相对的点权值,然后给出相连着的边,问最大利润值。
思路:
1、SPFA求最长路,正向建边TLE,反向建边4000msAC。。。。
2、这里源点一定是入度为0的点,因为题干中说了,没有其他任何点有边有有向边连接源点,那么起点的选取一定是要入度为0的点才行,相应的,终点也一定是出度为0的点才行。
3、卡vector、卡cin、习惯用这两种来实现代码的,请使用链式前向星和scanf。
AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[200005];
struct EdgeNode
{
int to;
int w;
int next;
}e[200005*20];
int degree[200005];
int val[200005];
int vis[200005];
int dis[200005];
int n,m;
int cont;
void add(int from,int to)
{
e[cont].to=to;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA()
{
queue<int >s;
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();
s.pop();vis[u]=0;
for(int k=head[u];k!=-1;k=e[k].next)
{
int v=e[k].to;
if(dis[v]<dis[u]+val[v])//求最长路
{
dis[v]=dis[u]+val[v];
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
int output=-0x3f3f3f3f;
for(int i=0;i<n;i++)
{
if(head[i]==-1)
output=max(output,dis[i]);
}
printf("%d\n",output);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cont=0;
memset(vis,0,sizeof(vis));
memset(degree,0,sizeof(degree));
memset(head,-1,sizeof(head));
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--;
add(y,x);
degree[x]++;
}
SPFA();
}
}
相关文章推荐
- POJ 3249-Test for Job(拓扑排序&&DP)
- POJ 3249 Test for Job【DAG图上的最短路问题】
- NSString 常用方法汇总
- 一些不错的文章链接记录
- iOS导航控制器的注意事项
- Android 一个改善的okHttp封装库
- HDU1102(最小生成树Kruskal算法)
- 人口普查
- 【GDOI 2016 Day1】疯狂动物城
- lightoj 1294 Positive Negative Sign 【规律】
- 双缓冲的誉抄的过程
- 二叉树的分层打印(一)
- CodeForces 671A Recycling Bottles
- alert之后留在原页面
- SpringMVC
- 静态代理设计模式
- LeetCode OJ Remove Duplicates from Sorted Array II
- Codeforces Round #324 (Div. 2) A
- 显示Unicode 字节的对应字符的小技巧
- 第18课:Spark Streaming中空RDD处理及流处理程序优雅的停止