Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 10651 | Accepted: 2504 |
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
明显 图是有向无环图
用拓扑排序一遍,然后按拓扑顺序对点的出边进行松弛操作,就能得到最短路
此题可以有多个起点 在构图的时候记录入度出度 入度为0的就是起点 出度为0的是终点
要注意的是最后ans只取出度为0的,dist初值要初始化为-INF,因为每个点的权值可正可负
拓扑排序用bfs会超时
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
char readT;
bool readIsNegative;
inline bool read(int&x){
readIsNegative=x=0;
while(!isdigit(readT=getchar()))
if(readT==EOF)
return false;
else
if(readT=='-'){
readIsNegative=true;
readT='0';
break;
}
x=readT-'0';
while(isdigit(readT=getchar()))
x=x*10+readT-'0';
if(readIsNegative)
x=-x;
return true;
}
const int N=100000+5;
const int M=1000000+5;
int head[N];
struct Edge{
int to,next;
}edge[M];
inline void addEdge(int k,int u,int v){
edge[k].to=v;
edge[k].next=head[u];
head[u]=k;
}
int w[N];
int dist[N];
int inDegree[N];
int outDegree[N];
bool used[N];
int TPO[N];
int TPOindex;
/*BFS TLE
void TopologicalOrder(int n,int index){
if(n==index)
return;
for(int i=1;i<=n;++i){
if(!used[i]&&inDegree[i]==0){
for(int j=head[i];j!=-1;j=edge[j].next){
--inDegree[edge[j].to];
}
used[i]=true;
TPO[index]=i;
TopologicalOrder(n,index+1);
return;
}
}
}
*/
void TopologicalOrder(int u){
used[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next){
if(used[edge[i].to]==false)
TopologicalOrder(edge[i].to);
}
TPO[TPOindex++]=u;
}
void relax(int n){
TPOindex=0;
for(int i=1;i<=n;++i){
dist[i]=inDegree[i]==0?w[i]:-INF;
if(used[i]==false)
TopologicalOrder(i);
}
for(int i=n-1;i>=0;--i){
int u=TPO[i];
for(int j=head[u];j!=-1;j=edge[j].next){
int v=edge[j].to;
dist[v]=max(dist[u]+w[v],dist[v]);
}
}
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
int n,m;
//while(~scanf("%d%d",&n,&m)){
while(read(n)){
read(m);
for(int i=1;i<=n;++i){
read(w[i]);
//scanf("%d",w+i);
head[i]=-1;
outDegree[i]=inDegree[i]=0;
used[i]=false;
}
for(int i=0,u,v;i<m;++i){
//scanf("%d%d",&u,&v);
read(u);
read(v);
addEdge(i,u,v);
++inDegree[v];
++outDegree[u];
}
relax(n);
int ans=-INF;
for(int i=1;i<=n;++i){
if(outDegree[i]==0)
ans=max(ans,dist[i]);
}
printf("%d\n",ans);
}
return 0;
}