xxx
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <vector> #include <bitset> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <set> #include <map> #define endc std::ios::sync_with_stdio(false); // 关掉c++流 #define INOPEN freopen("in.txt", "r", stdin) #define OUTOPEN freopen("out.txt", "w", stdout) #define mes(a, b) memset(a, b, sizeof(a)) #define isdigit(a) ((a)>=‘0‘&&(a)<=‘9‘) #define xiao(a) (a)>=‘a‘&&(a)<=‘z‘ #define da(a) (a)>=‘A‘&&(a)<=‘Z‘ #define pii pair<int, int> #define lowbit(x) x & (-x) #define fi first #define se second #define lson id<<1 #define rson id<<1|1 typedef unsigned long long int ull; typedef long long int ll; const double eps = 1e-8; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const ll INF = 1e18 + 8; const int maxm = 2e5 + 6; const int maxn = 2e5 + 10; const int mod = 100000009; using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while(!isdigit(ch)){if(ch == ‘-‘) f = -f; ch = getchar();} while(isdigit(ch)){x = (x<<3)+(x<<1)+ch-‘0‘; ch = getchar();} return x * f; } struct node { int v, next; ll w; node() {} node(int v, int n, ll w) : v(v), next(n), w(w) {} }edge[maxm << 1]; ll dis[maxn], a[maxn]; int head[maxn]; bool vis[maxn]; int tol, n; void add(int u, int v, ll w) { edge[tol] = node(v, head[u], w); head[u] = tol++; } void init() { tol = 0; mes(head, -1); mes(vis, false); for(int i = 0; i < maxn; ++i) dis[i] = INF; } ll spfa(int s, int t) { queue<int> q; while(!q.empty()) q.pop(); dis[s] = 0, vis[s] = true; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i = head[u]; ~i; i = edge[i].next) { node &e = edge[i]; if(dis[e.v] > dis[u] + e.w) { dis[e.v] = dis[u] + e.w; if(vis[e.v]) continue; vis[e.v] = true; q.push(e.v); } } } return dis[t]; } int main() { int T; // INOPEN; // OUTOPEN; scanf("%d", &T); while(T--) { init(); int n, m, u, v; ll w; scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]); while(m --) { scanf("%d%d%lld", &u, &v, &w); add(u, v, w); add(v, u, w); } spfa(1, n); ll ans = 0; int flag = 0; for(int i = 2; i <= n; ++i) { if(dis[i] == INF) { flag = 1; break; } ans += dis[i] * a[i]; } if(flag) printf("No Answer\n"); else printf("%lld\n", ans); } return 0; }
原文地址:https://www.cnblogs.com/Guugle/p/11781277.html
时间: 2024-11-09 00:02:36