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
| #include<bits/stdc++.h> #define RG register #define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout); #define for_edge(i, x) for(RG int i=head[x];i;i=e[i].next) #define clear(x, y) memset(x, y, sizeof(x)); using namespace std;
#define int long long inline int read() { int data=0, w=1; char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') w=-1, ch=getchar(); while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar(); return data*w; }
const int maxn(50010), maxm(100010); struct edge { int next, to, dis; } e[maxm<<1]; int head[maxn], e_num, val[maxn], n, m; inline void add_edge(int from, int to, int dis) { e[++e_num]=(edge){head[from], to, dis}; head[from]=e_num; } int dis[maxn];
queue<int> q; bool vis[maxn]; inline void spfa() { clear(dis, 127); q.push(1); vis[1]=true; dis[1]=0; while(!q.empty()) { int x=q.front(); q.pop(); for_edge(i, x) { int to=e[i].to; if(dis[to] > dis[x] + e[i].dis) { dis[to]=dis[x]+e[i].dis; if(!vis[to]) vis[to]=true, q.push(to); } } vis[x]=false; } }
int a, b, c, ans; #undef int int main() { #define int long long n=read(); m=read(); for(RG int i=1;i<=n;i++) val[i]=read(); for(RG int i=1;i<=m;i++) a=read(), b=read(), c=read(), add_edge(a, b, c); spfa(); for(RG int i=1;i<=n;i++) { if(dis[i]==dis[0]) return puts("No Answer")&0; ans+=dis[i]*val[i]; } printf("%lld\n", ans); return 0; }
|