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
| #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 100005 * 3 + 1;
int n, m, a[MAXN]; bool vis[MAXN]; int dis[MAXN]; queue<int> q;
void read(int& x) { x = 0; int f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -f; c = getchar(); } while (c <= '9' && c >= '0') { x = x * 10 + c - '0'; c = getchar(); } x *= f; }
struct edge { int v, w; edge(){} edge(int V, int W) { v = V; w = W; } }; vector<edge> G[MAXN];
void AddEdge(int u, int v, int w) { G[u + n * 0].push_back(edge(v + n * 0, 0)); G[u + n * 1].push_back(edge(v + n * 1, 0)); G[u + n * 2].push_back(edge(v + n * 2, 0)); G[u + n * 0].push_back(edge(v + n * 1, -w)); G[u + n * 1].push_back(edge(v + n * 2, w)); }
void Spfa() { memset(vis, 0, sizeof(vis)); memset(dis, 0xcf, sizeof(dis)); dis[1] = 0; vis[1] = 1; q.push(1); while (q.size()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i].v, w = G[u][i].w; if (dis[v] < dis[u] + w) { dis[v] = dis[u] + w; if (vis[v] == 0) { vis[v] = 1; q.push(v); } } } } printf ("%d\n", dis[n]); }
int main() { read(n); read(m); for (int i = 1; i <= n; i++) { read(a[i]); } for (int i = 1, u, v, opt; i <= m; i++) { read(u), read(v), read(opt); AddEdge(u, v, a[u]); if (opt == 2) { AddEdge(v, u, a[v]); } } G[n].push_back(edge(n * 3 + 1, 0)); G[n * 3].push_back(edge(n * 3 + 1, 0)); n *= 3; n++; Spfa(); return 0; }
|