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
| #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <iostream> #include <stack> #include <set> #include <vector> #include <queue> #include <cmath> #include <cstdlib> #include <bits/stdc++.h> using namespace std; #define eps 1e-8 #define INF 0x3f3f3f3f #define rep(i,a,n) for(int i=a;i<n;i++) #define per(i,a,n) for(int i=n-1;i>=a;i--) #define all(x) x.begin(),x.end() #define pb push_back #define np next_permutation #define ms(x,a) memset((x),(a),sizeof(x))
const int maxn=1e5+5; struct Edge{ int u,v,w; Edge(int _u,int _v,int _w):u(_u),v(_v),w(_w){ }; }; vector <Edge> edges[maxn];
int dis[maxn],meizi[maxn],mei[maxn]; bool vis[maxn]; int n,m,uu,vv,ww;
void init(int n){ rep(i,0,maxn){ edges[i].clear(); } }
void addEdge(int u,int v,int w){ edges[u].pb(Edge(u,v,w)); }
struct Node{ int Id,W; Node(int _Id,int _W):Id(_Id),W(_W){ }; bool operator < (const Node &b) const { return W>b.W;
} };
void Dijkstra(int s){ ms(vis,0); ms(meizi,0); ms(dis,INF); priority_queue <Node> q; q.push(Node(s,0)) ; dis[s]=0;meizi[s]=mei[s]; while(!q.empty()){ Node now=q.top(); q.pop(); int index=now.Id; if(vis[index]) continue;
vis[index]=1; rep(i,0,edges[index].size()){ Edge temp=edges[index][i]; if(dis[temp.v]>dis[index]+temp.w){ dis[temp.v]=dis[index]+temp.w; meizi[temp.v]=meizi[index]+mei[temp.v]; q.push(Node(temp.v,dis[temp.v])); } else if(dis[temp.v]==dis[index]+temp.w){ if(meizi[temp.v]<meizi[index]+mei[temp.v]){ meizi[temp.v]=meizi[index]+mei[temp.v]; q.push(Node(temp.v,dis[temp.v])); } } } } }
int main(){ ios::sync_with_stdio(false);
while(cin>>n>>m){ init(n); rep(i,1,n+1){ cin>>mei[i]; } rep(i,1,m+1){ cin>>uu>>vv>>ww; addEdge(uu,vv,ww); addEdge(vv,uu,ww); } Dijkstra(1); if(dis[n]>100000000) cout<<"-1"<<endl; else cout<<meizi[n]<<endl; } }
|