spfa练少了。。。WA了2次。。
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(i=l;i<=r;i++) 3 #define dec(i,l,r) for(i=l;i>=r;i--) 4 #define inf 1e9 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define ll long long 7 #define succ(x) (1<<x) 8 #define NM 100000+5 9 #define nm 1000000+5 10 using namespace std; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 14 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 15 return x*f; 16 } 17 struct edge{ 18 int s,t; 19 edge *next,*nxt; 20 }e[nm],*h[NM],*_h[NM]; 21 int i,n,ans,d[NM],f[NM],m,a[NM],s,_x,_y; 22 bool v[NM]; 23 queue<int >q; 24 void add(int x,int y){ 25 e[++s].s=x;e[s].t=y;e[s].next=h[x];e[s].nxt=_h[y];h[x]=_h[y]=&e[s]; 26 } 27 void spfa(){ 28 inc(i,1,n)d[i]=inf; 29 q.push(1);v[1]++;d[1]=a[1]; 30 while(!q.empty()){ 31 int t=q.front();q.pop();d[t]=min(d[t],a[t]); 32 for(edge *j=h[t];j;j=j->next) 33 { 34 d[j->t]=min(d[j->t],d[t]); 35 if(!v[j->t]){ 36 v[j->t]++;q.push(j->t); 37 } 38 } 39 } 40 } 41 void _spfa(){ 42 inc(i,1,n)f[i]=-inf; 43 mem(v); 44 q.push(n);v[n]++;f[n]=a[n]; 45 while(!q.empty()){ 46 int t=q.front();q.pop();f[t]=max(f[t],a[t]); 47 for(edge *j=_h[t];j;j=j->nxt){ 48 f[j->s]=max(f[j->s],f[t]); 49 if(!v[j->s]){ 50 v[j->s]++;q.push(j->s); 51 } 52 } 53 } 54 } 55 int main(){ 56 n=read();m=read(); 57 inc(i,1,n)a[i]=read(); 58 inc(i,1,m){ 59 _x=read();_y=read(); 60 add(_x,_y); 61 if(read()==2)add(_y,_x); 62 } 63 spfa(); 64 _spfa(); 65 inc(i,1,n) 66 ans=max(ans,f[i]-d[i]); 67 printf("%d\n",ans); 68 return 0; 69 }