最优贸易
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int maxn=1e5+7; 5 const int maxm=5e5+7; 6 const int inf=0x7f7f7f7f; 7 int n,m,num; 8 int head[maxn],val[maxn],f[maxn],mi[maxn]; 9 int read(){ 10 int f=1;int x=0;char s=getchar(); 11 while(s<'0'||s>'9') {if(s=='-') f=-1;s=getchar();} 12 while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();} 13 x*=f; 14 return x; 15 } 16 struct Edge{ 17 int next,to; 18 }edge[maxm]; 19 void add(int from,int to){ 20 edge[++num].next=head[from]; 21 edge[num].to=to; 22 head[from]=num; 23 } 24 void dfs(int x,int pre,int minn){ 25 bool flag=1; 26 minn=min(minn,val[x]); 27 if(mi[x]>minn){mi[x]=minn;flag=false;} 28 int maxx=max(f[pre],val[x]-minn); 29 if(f[x]<maxx){f[x]=maxx;flag=false;} 30 if(flag==true) return; 31 for(int i=head[x];i;i=edge[i].next){ 32 int v=edge[i].to; 33 dfs(v,x,minn); 34 } 35 } 36 int main(){ 37 cin>>n>>m; 38 for(int i=1;i<=n;i++) cin>>val[i]; 39 for(int i=1;i<=m;i++){ 40 int x,y,z;x=read();y=read();z=read(); 41 if(z==1) add(x,y); 42 if(z==2) {add(x,y);add(y,x);} 43 } 44 for(int i=1;i<=n;i++) mi[i]=inf; 45 dfs(1,0,inf); 46 cout<<f[n]<<endl; 47 return 0; 48 }