APIO2009劫掠计划

2021-04-01 11:27

阅读:476

标签:pac   AC   void   namespace   set   ios   while   ret   FN   

/*
缩点加上最长路
应该用拓扑序列的  不想写  spfa跑一下吧



*/

#include
#include
#include
#include
#include#define M 500200
#define ll long long
using namespace std;
int be[M], ed[M], to[M], nxt[M], dft, head[M], cnt, belong[M], dfn[M], low[M], dis[M], ver[M], ver2[M], st[M], top, tot;
bool vis[M], isj[M], zz[M];

int read() {
    int nm = 0, f = 1;
    char c = getchar();
    for(; !isdigit(c); c = getchar()) if(c == -) f = -1;
    for(; isdigit(c); c = getchar()) nm = nm * 10 + c -0;
    return nm * f;
}

void push(int vi, int vj) {
    cnt++;
    to[cnt] = vj;
    nxt[cnt] = head[vi];
    head[vi] = cnt;
}

void tarjan(int x) {
    dfn[x] = low[x] = ++dft;
    st[++top] = x;
    vis[x] = true;
    for(int i = head[x]; i; i = nxt[i]) {
        int vj = to[i];
        if(!dfn[vj]) {
            tarjan(vj);
            low[x] = min(low[x], low[vj]);
        } else if(vis[vj]) low[x] = min(low[x], dfn[vj]);
    }
    if(dfn[x] == low[x]) {
        tot++;
        while(st[top] != x) {
            belong[st[top]] = tot;
            ver2[tot] += ver[st[top]];
            if(isj[st[top]]) zz[tot] = true;
            vis[st[top]] = false;
            top--;
        }

        belong[st[top]] = tot;
        ver2[tot] += ver[st[top]];
        if(isj[st[top]]) zz[tot] = true;
        vis[st[top]] = false;
        top--;
    }
}

void spfa(int x) {
    queueint>q;
    dis[x] = ver2[x];
    memset(vis, 0, sizeof(vis));
    q.push(x);
    while(!q.empty()) {
        int op = q.front();
        q.pop();
        vis[op] = false;
        for(int i = head[op]; i; i = nxt[i]) {
            int vj = to[i];
            if(dis[vj]  ver2[vj]) {
                dis[vj] = dis[op] + ver2[vj];
                if( !vis[vj])
                    q.push(vj), vis[vj] = true;
            }
        }
    }
}


int main() {
    int n = read(), m = read();
    for(int i = 1; i ) {
        be[i] = read(), ed[i] = read();
        push(be[i], ed[i]);
    }
    for(int i = 1; i  read();
    int s = read(), k = read();
    for(int i = 1; i true;
    tarjan(s);
    memset(head, 0, sizeof(head));
    cnt = 0;
    for(int i = 1; i ) {
        int vi = belong[be[i]], vj = belong[ed[i]];
        if(vi == vj) continue;
        push(vi, vj);
    }
    spfa(belong[s]);
    int ans=  0;
    for(int i = 1; i ) {
        if(zz[i]) ans = max(ans, dis[i]);
    }
    cout  ans;
    return 0;
}

 

APIO2009劫掠计划

标签:pac   AC   void   namespace   set   ios   while   ret   FN   

原文地址:https://www.cnblogs.com/luoyibujue/p/9240866.html


评论


亲,登录后才可以留言!