5 5 4 3 5 6 1 1 2 1 1 4 1 2 3 2 3 5 1 4 5 2
5
#include"stdio.h" #include"string.h" #include"queue" using namespace std; #define max(a,b) (a>b?a:b) #define min(a,b) (aq; int x,i,v; int mark1[N],mark2[N]; memset(mark1,0,sizeof(mark1)); memset(mark2,0,sizeof(mark2)); mark1[s]=1; mark2[n]=1; q.push(s); while(!q.empty()) { x=q.front(); q.pop(); for(i=head1[x];i!=-1;i=bian[i].next) { v=bian[i].v; a[v]=min(a[v],a[x]); if(!mark1[v]) { mark1[v]=1; q.push(v); } } } q.push(n); while(!q.empty()) //從終點原路返回起點,即走反向邊 { x=q.front(); q.pop(); for(i=head2[x];i!=-1;i=bian[i].next) { v=bian[i].v; b[v]=max(b[v],b[x]); if(!mark2[v]) { mark2[v]=1; q.push(v); } } } int ans=0; for(i=1;i<=n;i++) { if(mark1[i]&&mark2[i]) { ans=max(ans,b[i]-a[i]); } } return ans; } int main() { int n,m,u,v,x,i; while(scanf("%d%d",&n,&m)!=-1) { for(i=1;i<=n;i++) { scanf("%d",&a[i]); b[i]=a[i]; //a[i]記錄最小值,b[i]記錄最大值 } memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); e=0; while(m--) { scanf("%d%d%d",&u,&v,&x); add(u,v); if(x==2) add(v,u); } printf("%d\n",spfa(1,n)); } return 0; }