POJ 3249 DAG图最短路

Test for Job
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
一个有向无环图(DAG)图,我们可以已拓扑排序的访问顺序更新最大值,因为如果更新到x->y,说明所有k->x节点都已经更新完毕,所以可以更新x->y的值。如果toposort失败,则说明有原图有环,不是DAG图。
7

#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f,MAXN=1e5+8,mod=998244353;
typedef long long ll;
#define Init(arr,val) memset(arr,val,sizeof(arr))
int n,m;
struct E{int y,nt;}e[MAXN*10];
int head[MAXN],cnt;
inline void add(int x,int y){//x->y
    e[++cnt].y=y;
    e[cnt].nt=head[x];head[x]=cnt;
}
int pval[MAXN],ru[MAXN],chu[MAXN];//点值,入度、出度
int ans[MAXN];
int que[MAXN],num;
void toposort(){
    for(int i=1;i<=n;++i){
        ans[i]=-1<<25;
        if(ru[i]==0){
            que[num++]=i;
            ans[i]=pval[i];
        }
    }
    int x,y;
    while(num){
        x=que[--num];
        for(int i=head[x];i;i=e[i].nt){
            y=e[i].y;
            if(ans[y]<ans[x]+pval[y])ans[y]=ans[x]+pval[y];
            if(--ru[y]==0){
                que[num++]=y;
            }
        }
    }
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        Init(head,0);
        Init(ru,0);
        Init(chu,0);
        cnt=0;
        for(int i=1;i<=n;++i)scanf("%d",pval+i);
        int x,y;for(int i=0;i<m;++i){
            scanf("%d%d",&x,&y);
            add(x,y);
            ++chu[x];++ru[y];
        }
        toposort();
        int res=-1<<25;
        for(int i=1;i<=n;++i)if(!chu[i])res=max(res,ans[i]);
        //出度为0的点才能更新答案
        printf("%d\n",res);
    }
}
posted @ 2020-12-18 17:12  肆之月  阅读(61)  评论(0编辑  收藏  举报