zoukankan      html  css  js  c++  java
  • 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
    ",res);
        }
    }
    
  • 相关阅读:
    【2018.05.05 C与C++基础】C++中的自动废料收集:概念与问题引入
    【2018.04.27 C与C++基础】关于switch-case及if-else的效率问题
    【2018.04.19 ROS机器人操作系统】机器人控制:运动规划、路径规划及轨迹规划简介之一
    March 11th, 2018 Week 11th Sunday
    March 10th, 2018 Week 10th Saturday
    March 09th, 2018 Week 10th Friday
    March 08th, 2018 Week 10th Thursday
    March 07th, 2018 Week 10th Wednesday
    ubantu之Git使用
    AMS分析 -- 启动过程
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14155935.html
Copyright © 2011-2022 走看看