两遍SPFA搞定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// <trade.cpp> - 02/27/16 22:26:43 // This file is created by XuYike's black technology automatically. // Copyright (C) 2015 ChangJun High School, Inc. // I don't know what this program is. #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef long long lol; int getint(){ int res=0,fh=1;char ch=getchar(); while((ch>'9'||ch<'0')&&ch!='-')ch=getchar(); if(ch=='-')fh=-1,ch=getchar(); while(ch>='0'&&ch<='9')res=res*10+ch-'0',ch=getchar(); return fh*res; } const int MAXN=100001; const int INF=1000000000; int a[MAXN],v[MAXN][2]; bool in[MAXN]; vector <int> b[MAXN][2]; queue <int> q; void ins(int x,int y){b[x][0].push_back(y);b[y][1].push_back(x);} void SPFA(int x,bool f){ q.push(x);in[x]=1; while(!q.empty()){ int now=q.front();q.pop();in[now]=0; for(int i=0;i<b[now][f].size();i++){ int next=b[now][f][i]; if(!f&&max(v[now][0],a[next])>v[next][0]){ v[next][0]=max(v[now][0],a[next]); if(in[next])continue; q.push(next); in[next]=1; } if(f&&min(v[now][1],a[next])<v[next][1]){ v[next][1]=min(v[now][1],a[next]); if(in[next])continue; q.push(next); in[next]=1; } } } } int main(){ freopen("trade.in","r",stdin); freopen("trade.out","w",stdout); int n=getint(),m=getint(); for(int i=1;i<=n;i++)a[i]=getint(); for(int i=1;i<=m;i++){ int x=getint(),y=getint(),z=getint(); ins(x,y); if(z==2)ins(y,x); } for(int i=1;i<=n;i++)v[i][0]=-INF,v[i][1]=INF; SPFA(1,0);SPFA(n,1); int ans=0; for(int i=1;i<=n;i++)ans=max(ans,v[i][0]-v[i][1]); printf("%d",ans); return 0; } |