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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include<iostream> #include<cstdio> #include<cmath> #include<queue> #include<cstring> #include<vector> #include<stack> #include<algorithm>
using namespace std;
int n,m,head[100050],size; int bd[100050],a[100050],zd[100050],zx[100500],mmx=-1,dt,cnt; int dfn[100050],low[100050],ins[100050],ind[100500]; int f[100050];
stack <int> s;
struct edge{ int next,to; }e[1008600],looker[1008600];
void addedge(int next,int to) { e[++size].next=head[next]; e[size].to=to; head[next]=size; }
void tarjan(int t) { dfn[t]=low[t]=++dt; s.push(t); ins[t]=1; int i,j; for(i=head[t];i;i=e[i].next) { j=e[i].to; if(!dfn[j]) tarjan(j),low[t]=min(low[t],low[j]); else if(ins[j]) low[t]=min(low[t],dfn[j]); } j=0; if(dfn[t]==low[t]) { cnt++; while(t!=j) { j=s.top(); s.pop(); ins[j]=0; bd[j]=cnt; } } }
void dagdp(int s) { queue <int> q; q.push(bd[s]); f[bd[s]]=max(f[bd[s]],zd[bd[s]]-zx[bd[s]]); while(!q.empty()) { int t=q.front(); q.pop(); int i,j; for(i=head[t];i;i=e[i].next) { j=e[i].to; ind[j]--; zx[j]=min(zx[j],zx[t]); f[j]=max(f[j],max(zd[j]-zx[j],f[t])); if(!ind[j]) { q.push(j); } } } }
int main() { int i,j; scanf("%d %d",&n,&m); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=m;i++) { int t1,t2,t3; scanf("%d %d %d",&t1,&t2,&t3); if(t3==1) { addedge(t1,t2); looker[size].next=t1; looker[size].to=t2; } if(t3==2) { addedge(t2,t1); addedge(t1,t2); } } for(i=1;i<=n;i++) if(!dfn[i]) tarjan(i); memset(zd,-1,sizeof(zd)); memset(zx,0x3f,sizeof(zx)); int sz=size; memset(head,0,sizeof(head)); size=0; for(i=1;i<=sz;i++) { int aa,bb; aa=looker[i].next; bb=looker[i].to; aa=bd[aa]; bb=bd[bb]; if(aa==bb) continue; addedge(aa,bb); ind[bb]++; } for(i=1;i<=n;i++) { zd[bd[i]]=max(zd[bd[i]],a[i]); zx[bd[i]]=min(zx[bd[i]],a[i]); } dagdp(1); printf("%d",f[bd[n]]); return 0; }
|