Los Valley - the optimal solution to a problem P1073 trade

https://www.luogu.org/problemnew/show/P1073

2009 to improve the group t3, spent $ spfa $

A little bit different when normal $ spfa $ will update with respect to the optimal solution

Note: There is no need to determine the ring, the ring will not make the answer anyway, constantly updated

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<queue>
 7 #include<map> 
 8 using namespace std;
 9 #define re register int
10 inline int read(){
11     int x=0,ff=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')ff=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
14     return x*ff;
15 }
16 struct tree{
17     int to,next;
18 }a[1000005];
19 int h[100005],tot,f[100005],g[100005],n,m,c[100005];
20 bool b[100005];
21 void add(int x,int y){
22     a[++tot].next=h[x];a[tot].to=y;h[x]=tot;
23 }
24 signed main(){
25     queue<int>dl;
26     n=read();m=read();int x,y,z;
27     for(int i=1;i<=n;i++){c[i]=read();f[i]=g[i]=c[i];dl.push(i);}
28     while(m--){
29         x=read();y=read();z=read();
30         add(x,y);if(z>1)add(y,x);
31     }
32     while(!dl.empty()){
33         x=dl.front();dl.pop();b[x]=0;
34         for(int i=h[x];i;i=a[i].next){
35             if(f[x]<f[a[i].to]){//更新卖出价
36                 f[x]=f[a[i].to];
37                 if(!b[x]){
38                     b[x]=1;dl.push(x);
39                 }
40             }
41             if(b[x])continue;
42             if(g[a[i].to]>g[x]){//更新买入价
43                 g[a[i].to]=g[x];
44                 if(!b[a[i].to]){
45                     b[a[i].to]=1;dl.push(a[i].to);
46                 }
47             }
48             if(f[x]-g[x]>f[a[i].to]-g[a[i].to]){//到此点是,已经出售
49                 f[a[i].to]=f[x];g[a[i].to]=g[x];
50                 if(!b[a[i].to]){
51                     b[a[i].to]=1;dl.push(a[i].to);
52                 }
53             }
54         }
55     }
56     printf("%d\n",max(0,f[n]-g[n]));
57     return 0;
58 }

 

Guess you like

Origin www.cnblogs.com/ffrxy01bt/p/11221205.html