Test for Job//DAG 拓扑 最短路

题目:

 
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 11620   Accepted: 2756

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

 

思路:

  因为求最长路,所以输入取负数(最后输出再取负就行了),变成最短路问题,但求DAG的最短路必须是单源的,所以加一个总源点(连起始点并且权取0),加一个总终点(方便最终结果计算,连终点并且权也取0),然后从总源点拓扑排序,再按该排序进行边松弛,得到最短路。
 

代码:

#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
const int size = 1e5+5;

vector<int> graph[size];//存图
int fee[size];//记录点权
bool marked[size];
stack<int> order;//存拓扑顺序
long long dis[size];
bool in[size];
bool out[size];

void init(int n)//初始化
{
    for(int i = 0; i <= n+1; i++)//要包含0和n+1!!!
    {
        graph[i].clear();
        marked[i] = false;
        dis[i] = 0x3f3f3f3f;
        in[i] = false;
        out[i] = false;
    }
    fee[0] = 0;
    fee[n+1] = 0;
}

void topo(int x)//用dfs来拓扑排序
{
    marked[x] = true;
    for(int i = 0; i < graph[x].size(); i++)
    {
        if(!marked[graph[x][i]])
            topo(graph[x][i]);
    }
    order.push(x);
}

void sp(int s)//找最短路
{
    dis[s] = fee[s];

    while(!order.empty())//按拓扑排序的顺序进行松弛
    {
        int c = order.top();
        order.pop();
        for(int i = 0; i < graph[c].size(); i++)//松弛
        {
            if(dis[c] + fee[graph[c][i]] < dis[graph[c][i]])
                dis[graph[c][i]] = dis[c] + fee[graph[c][i]];
        }
    }
}

int main()
{
    int n, m;
    int x, y;
    while(~scanf("%d%d",&n,&m))
    {
        //初始化
        init(n);
        //输入
        for(int i = 1; i <= n; i++)
        {
            int t;
            scanf("%d", &t);
            fee[i] = -t;//将权取负就变成最短路了,最后再输出负值
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d", &x, &y);
            graph[x].push_back(y);
            in[y] = true;//标记有入度的点
            out[x] = true;
        }
        //加总源点,总终点
        for(int i = 1; i <= n; i++)
        {
            if(!in[i])
                graph[0].push_back(i);
            if(!out[i])
                graph[i].push_back(n+1);
        }

        topo(0);
        sp(0);
        printf("%lld\n",-dis[n+1]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/w-j-c/p/9230882.html