Luogu P1073 Optimal Trade--(tarjan shrink point+DAG+dp+topological sort)

There seem to be 10,000 solutions to this problem. There are two-way expansion of spfa, hierarchical map transfer, and bfs. . . . .

Title description:

There are n points and m edges. Each point has a weight representing the price of buying or selling the crystal ball at this point.
A person walks from 1 to n and asks what is the difference you can make the most (buy and sell only once)

Question idea:

This is a directed graph with a ring.
First, you can shrink the ring into a point, and record the highest and lowest prices of the transaction at this point
(because for all points in a ring, these points can reach each other),
then the graph changes Becomes a DAG
satisfying the current point u

dp[u] = max(max(dp[u],dp[v]),val_max[u]-minn[v]);

Among them, max(dp[u],dp[v])record the maximum difference on the path that can reach n points

At first it was pushed to dp[v] =val_max[v]-minn[v];
and then swept one side to take the maximum value.
It is possible that the point reached at the end is not point n

Code:

int head[maxn],cnt,n,m,val[maxn],vis[maxn],dfn[maxn],low[maxn],indexx,minn[maxn];
int block,id[maxn],cnt1,head1[maxn],in[maxn],valma[maxn],valmi[maxn],dp[maxn];
stack<int>st;
struct node {
    
    
    int u,v,next;
} e[maxn],zan[maxn];
void add1(int u,int v ) {
    
    
    zan[cnt1].u=u;
    zan[cnt1].v=v;
    zan[cnt1].next=head1[u];
    head1[u]=cnt1++;
}
void add(int u,int v ) {
    
    
    e[cnt].u=u;
    e[cnt].v=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void tarjan(int u) {
    
    
    dfn[u] = low[u] = ++indexx;
    vis[u] = 1;
    st.push(u);
    for(int i=head1[u]; ~i; i=zan[i].next) {
    
    
        int v  = zan[i].v;
        if(dfn[v]==0) tarjan(v),  low [u] = min(low[u],low[v]);
        else if(vis[v])    low[u] = min(low[u],dfn[v]);
    }
    int ma = -inf,mi = inf;
    if(low[u] == dfn[u]) {
    
    
        int yy;
        block++;
        do {
    
    
            yy = st.top();
            st.pop();
            vis[yy] =0 ;
            ma =max(ma,val[yy]);
            mi =min(mi,val[yy]);
            id[yy]  = block;
        } while(yy!=u);
        valmi[block] = mi;
        valma[block] = ma;
    }
}
void top_sort() {
    
    
    queue<int>q;

    q.push(id[1]);

    while(q.size()) {
    
    
        int u = q.front();
        q.pop();
        for(int i =head[u]; ~i; i=e[i].next) {
    
    
            int v = e[i].v;
            minn[v] = min (minn[u],valmi[v]);
            dp[v] = max(max(dp[u],dp[v]),valma[v]-minn[v]);
            in[v]--;
            if(in[v]==0) q.push(v);
        }
    }
}
int main() {
    
    
    mst(head1,-1);
    n=read(),m=read(),mst(head,-1);
    rep(i,1,n) val[i] = read(),minn[i] = inf;
    for(int i=1 ; i<=m ; i++) {
    
    
        int op,u,v;
        u=read(),v=read(),op=read();
        add1(u,v);
        if(op==2) add1(v,u);
    }
    tarjan(1);
    for(int i = 0 ; i<cnt1 ; i++) {
    
    
        int u = zan[i].u;
        int v = zan[i].v;
        if(id[u]!=id[v]) add(id[u],id[v]),in[id[v]]++;
    }
    top_sort();
    out(dp[id[n]]);
    return 0;
}

Guess you like

Origin blog.csdn.net/wmy0536/article/details/110527771