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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include <bits/stdc++.h> using namespace std; const int iol = 1024 * 1024; char buf[iol], *ioh, *iot, ioc; bool iosig; inline char read() { if (ioh == iot) { iot = (ioh = buf) + fread(buf, 1, iol, stdin); if (ioh == iot) return -1; } return *ioh++; } template<class T> inline bool read(T &x) { iosig = false; for (ioc = read(); !isdigit(ioc); ioc = read()) { if (ioc == -1) return false; if (ioc == '-') iosig = true; } x = 0; while (ioc == '0') ioc = read(); for (; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0'); ioh--; if (iosig) x = -x; return true; } struct DisjointSet { int *rank, *father, size; inline int get(int x) { register int p = x, i; while (p != father[p]) p = father[p]; while (x != p) i = father[x], father[x] = p, x = i; return p; } inline void put(int x, int y) { x = get(x), y = get(y); if (rank[x] > rank[y]) father[y] = x; else { father[x] = y; if (rank[x] == rank[y]) rank[y]++; } } inline void init(int x) { rank[x] = 0, father[x] = x; } DisjointSet(int n) : rank(new int[n + 1]), father(new int[n + 1]), size(n + 1) { for (register int i = 1; i <= n; i++) father[i] = i; } } *s; int n, m; #define INF 0x3fffffff const int MAX = 100000 + 50; struct Node { int v; long long w; Node() {} Node(int v0, long long w0) : v(v0), w(w0) {} inline bool operator < (const Node &b) const { return w < b.w; } }; bool vis[MAX], toPush[MAX]; vector<Node> edge[MAX];
long long dis[MAX];
inline void spfa() { queue<int> q; memset(dis, 127, sizeof(dis)); memset(vis, false, sizeof(vis)); for (register int i = 1; i <= n; i++) if (toPush[i]) q.push(i), vis[i] = true, dis[i] = 0; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = false; for (int i = 0; i < edge[now].size(); ++i) { Node *x = &edge[now][i]; if (dis[x->v] > dis[now] + x->w) { dis[x->v] = dis[now] + x->w; if (!vis[x->v]) vis[x->v] = true, q.push(x->v); } } } } inline void addEdge(int u, int v, int w) { edge[u].push_back(Node(v, w)); edge[v].push_back(Node(u, w)); } inline void init() { read(n), read(m); for (register int i = 1; i <= n; i++) read(toPush[i]); for (register int i = 1, u, v, w; i <= m; i++) read(u), read(v), read(w), addEdge(u, v, w); spfa(); } struct data { int u, v; long long w; inline bool operator < (const data &b) const { return w < b.w; } } d[400000 + 20]; int cnt; long long ans; inline void solve() { for (register int i = 1; i <= n; i++) { if (!toPush[i]) { for (register int j = 0; j < edge[i].size(); j++) { Node *e = &edge[i][j]; if (dis[e->v] + e->w == dis[i]) d[++cnt].u = e->v, d[cnt].v = i, d[cnt].w = e->w; } } } s = new DisjointSet(n + 1); register int k = 0; for (register int i = 1; i <= n; i++) if (toPush[i]) s->put(i, n + 1), k++; sort(d + 1, d + 1 + cnt); for (register int i = 1; i <= cnt; i++) { if (s->get(d[i].u) != s->get(d[i].v)) { s->put(d[i].u, d[i].v); k++, ans += d[i].w; if (k == n) break; } } if (k != n) cout << "impossible"; else cout << ans; } int main() { init(); solve(); return 0; }
|