Trade optimal solution to a problem NOIP 2009

One of the most short-circuit problem, find a difference between the buy and sell to the highest point, we first ran spfa as a starting point to 1, d1 [x] no longer represents the distance can be expressed through the right weight minimum value of the node that is

if(d1[y]>min(d1[x],price[y])){
     d1[y]=min(d1[x],price[y]);
     if(!v[y]) q.push(y),v[y]=1;
}

We have built anti diagram for n-point run again spfa, the maximum value that is calculated

if(d2[y]<max(d2[x],price[y])){
    d2[y]=max(d2[x],price[y]);
    if(!v[y]) q.push(y),v[y]=1;
}

Each node of the last enumerated by d2 [x] -d1 [x] to update the maximum value

Code

#include<bits/stdc++.h>
using namespace std;
const int N=100010,M=500010;
int tot1,tot2,head[3][N],ver[3][M],Next[3][M];
int d1[N],d2[N],price[N];
bool v[N];
int x,y,z,n,m,ans;
queue<int> q;
void add1(int x,int y){
    ver[1][++tot1]=y;Next[1][tot1]=head[1][x];head[1][x]=tot1;
}
void add2(int x,int y){
    ver[2][++tot2]=y;Next[2][tot2]=head[2][x];head[2][x]=tot2;
}
void spfa(){
    memset(d1,0x3f,sizeof(d1));
    d1[1]=price[1];v[1]=1;
    q.push(1);
    while(!q.empty()){
        int x=q.front();q.pop();
        v[x]=0;
        for(int i=head[1][x];i;i=Next[1][i]){
            int y=ver[1][i];
            if(d1[y]>min(d1[x],price[y])){
                d1[y]=min(d1[x],price[y]);
                if(!v[y]) q.push(y),v[y]=1;
            }
        }
    }
    memset(v,0,sizeof(v));
    d2[n]=price[n];v[n]=1;
    q.push(n);
    while(!q.empty()){
        int x=q.front();q.pop();
        v[x]=0;
        for(int i=head[2][x];i;i=Next[2][i]){
            int y=ver[2][i];
            if(d2[y]<max(d2[x],price[y])){
                d2[y]=max(d2[x],price[y]);
                if(!v[y]) q.push(y),v[y]=1;
            }
        }
    }
      
}
int main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;++i) scanf("%d",&price[i]);
    for(int i=1;i<=m;++i){
        scanf("%d %d %d",&x,&y,&z);
        if(z==1) add1(x,y),add2(y,x);
        else{
            add1(x,y);add1(y,x);
            add2(x,y);add2(y,x);
        }
    }
    spfa();
    for(int i=1;i<=n;++i){
        ans=max(d2[i]-d1[i],ans);
    }
    printf("%d",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/donkey2603089141/p/11416384.html
Recommended