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
| #include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<cmath> using namespace std; struct st{ int s; bool f; };int n,m; int vis[19999][2],dis[19999][2],head[1999],nex[19999],cost[19999],to[199999],w[19999],tot,col[199999],s[19999]; deque <st> q; void add(int x,int y,int z) { tot++; nex[tot]=head[x]; to[tot]=y; cost[tot]=z; head[x]=tot; } int dist(int stt,int too,bool ff)//判断起点 { int sf=col[stt]^ff; int tf=col[too]^ff; int ww=abs(w[stt]-w[too]); if(sf==tf)return 0; if(sf==1) return ww; else return -ww; } void spfa(int ss) { dis[ss][0]=0; vis[ss][0]=1; q.push_back((st){ss,0}); while(!q.empty()) { st o=q.front();q.pop_front(); int x=o.s; vis[x][o.f]=0; for(int i=head[x];i;i=nex[i]) { int tmp=to[i]; if(dis[tmp][o.f^1]>dis[x][o.f]+max(cost[i]+dist(x,tmp,o.f),0)) { dis[tmp][o.f^1]=dis[x][o.f]+max(cost[i]+dist(x,tmp,o.f),0); if(!vis[tmp][o.f^1]) { if(dis[tmp][o.f^1]<=dis[q.front().s][q.front().f]) q.push_front((st){tmp,o.f^1}); else q.push_back((st){tmp,o.f^1}); vis[tmp][o.f^1]=1; } } } if(dis[x][o.f^1]>dis[x][o.f]+(col[x]^o.f)*s[x]) { dis[x][o.f^1]=dis[x][o.f]+(col[x]^o.f)*s[x]; if(!vis[x][o.f^1]) { if(dis[x][o.f^1]<=dis[q.front().s][q.front().f]) q.push_front((st){x,o.f^1}); else q.push_back((st){x,o.f^1}); vis[x][o.f^1]=1; } } } } int main() { memset(dis,127,sizeof dis); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&col[i]); for(int i=1;i<=n;i++) scanf("%d",&w[i]); for(int i=1;i<=n;i++) scanf("%d",&s[i]); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); } spfa(1); printf("%d",min(dis[n][0],dis[n][1])); }
|