【luogu P3627 [APIO2009]抢掠计划】 题解

题目链接:https://www.luogu.org/problemnew/show/P3627
把点权转化到边权上去。

#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 500000 + 10;
const int inf = 0x7fffffff;
struct edge{
    int from, to, next, len;
}e1[maxn<<2], e2[maxn<<2];
int head1[maxn], head2[maxn], cnt1, cnt2;
bool vis1[maxn], vis2[maxn];
int dfn[maxn], low[maxn], tim, color[maxn], num, dis[maxn];
int n, m, val1[maxn], val2[maxn], end, S, p, x[maxn], y[maxn], ans = -1;
stack<int> s;
queue<int> q;
void add1(int u, int v)
{
    e1[++cnt1].from = u;
    e1[cnt1].to = v;
    e1[cnt1].next = head1[u];
    head1[u] = cnt1;
}
void add2(int u, int v, int w)
{
    e2[++cnt2].from = u;
    e2[cnt2].to = v;
    e2[cnt2].len = w;
    e2[cnt2].next = head2[u];
    head2[u] = cnt2;
}
void tarjan(int x)
{
    dfn[x] = low[x] = ++tim;
    s.push(x); vis1[x] = 1;
    for(int i = head1[x]; i != -1; i = e1[i].next)
    {
        int v = e1[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[x] = min(low[x], low[v]);
        }
        else if(vis1[v])
        {
            low[x] = min(low[x], low[v]);
        }
    }
    if(dfn[x] == low[x])
    {
        color[x] = ++num;
        vis1[x] = 0;
        val2[num] += val1[x];
        while(s.top() != x)
        {
            color[s.top()] = num;
            vis1[s.top()] = 0;
            val2[num] += val1[s.top()];
            s.pop();
        }
        s.pop();
    }
}
void SPFA()
{
    for(int i = 1; i <= num; i++) dis[i] = inf;	
    dis[color[S]] = -1*val2[color[S]]; vis2[color[S]] = 1; q.push(color[S]); 
    while(!q.empty())
    {
        int now = q.front(); q.pop();
        vis2[now] = 0;
        for(int i = head2[now]; i != -1; i = e2[i].next)
        {
            if(dis[e2[i].to] > dis[now] + e2[i].len)
            {
                dis[e2[i].to] = dis[now] + e2[i].len;
                if(!vis2[e2[i].to])
                {
                    q.push(e2[i].to);
                    vis2[e2[i].to] = 1;
                }
            }
        }
    }
}
int main()
{
    memset(head1, -1, sizeof(head1));
    memset(head2, -1, sizeof(head2));
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d",&x[i],&y[i]);
        add1(x[i], y[i]);
    }
    for(int i = 1; i <= n; i++)	scanf("%d",&val1[i]);
    for(int i = 1; i <= n; i++)
        if(!dfn[i]) tarjan(i);
    for(int i = 1; i <= m; i++)
    {
        if(color[x[i]] != color[y[i]])
        add2(color[x[i]], color[y[i]], val2[color[y[i]]]*(-1));
    }
    scanf("%d%d",&S,&p);
    SPFA();
    for(int i = 1; i <= p; i++)
    {
        scanf("%d",&end);
        ans = max(ans, -1*dis[color[end]]);
    }
    //for(int i = 1; i <= n; i++) cout<<dis[i]<<" ";
    printf("%d",ans);
    return 0;
}
posted @ 2018-07-27 21:14  Misaka_Azusa  阅读(123)  评论(0编辑  收藏  举报
Live2D