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
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
#ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const {return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;} int operator == (const typemod<mod> &x) const {return x.z == z;} int operator != (const typemod<mod> &x) const {return x.z != z;} }; typedef typemod<mod> Tm; #endif
const int maxn = 2e5 + 5; const int maxm = maxn << 1;
struct Node { int u, v, w; Node(int x = 0,int y = 0,int z = 2e9) : u(x), v(y), w(z) {} int operator < (const Node &z) const { return w < z.w; } }E[maxm];
signed main() {
int i, j; int n, m; struct Union : vector<int>{ Union(int x = 0) { this->resize(x + 1); } int getfa(int x) { return x == this->at(x) ? x : this->at(x) = getfa(this->at(x)); } }; r1(n), r1(m); Union T(n); for(i = 1; i <= n; ++ i) T[i] = i;
vector<int> a(n + 1, 0), b(n + 1, 0); vector<long long> val(n + 1, 0); vector<int> mn(n + 1, 2e9), mx(n + 1, 0); long long ans(1e18), sum(0); for(i = 1; i <= n; ++ i) r1(a[i]), r1(b[i]), val[i] = 1ll * a[i] * b[i], sum += val[i], mn[i] = b[i], mx[i] = a[i]; for(i = 0; i < m; ++ i) { r1(E[i].u), r1(E[i].v), r1(E[i].w); } sort(E, E + m);
for(int i = 0; i < m; ++ i) { int x(E[i].u), y(E[i].v), w(E[i].w); x = T.getfa(x), y = T.getfa(y); if(x == y) continue; T[y] = x; mx[x] = max(mx[x], max(mx[y], w)); mn[x] = min(mn[x], mn[y]); long long tmp1 = val[x] + val[y]; tmp1 = min(tmp1, 1ll * mx[x] * mn[x]); sum -= val[x] + val[y]; val[x] = tmp1; sum += val[x]; ans = min(ans, sum); } printf("%lld\n", ans); return 0; }
|