題意 "題目鏈接" Sol 一開始的思路:新建一個虛點向每個點連邊,再加上題面中給出的邊,邊權均為大小 需要購買的數量 然後發現死活都過不去 看了題解才發現題目中有個細節——買了$A$就可以買$B$,但是人家沒告訴你必須買夠$A$的數量才能買$B$呀qwqqqqqqq 所以建圖的時候只算一次貢獻就行 ...
題意
Sol
一開始的思路:新建一個虛點向每個點連邊,再加上題面中給出的邊,邊權均為大小*需要購買的數量
然後發現死活都過不去
看了題解才發現題目中有個細節——買了\(A\)就可以買\(B\),但是人家沒告訴你必須買夠\(A\)的數量才能買\(B\)呀qwqqqqqqq
所以建圖的時候只算一次貢獻就行了
這樣剩下的個數都可以通過最小值買到
// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10, INF = 1e9 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c -'0', c = getchar();
return x * f;
}
int N, M, R, m[MAXN], id[MAXN], vis[MAXN], fa[MAXN];
double mn[MAXN], val[MAXN];
struct Edge {
int u, v; double w; int nxt;
}E[MAXN];
int head[MAXN], num = 1;
inline void AddEdge(int x, int y, double w) {
E[num] = (Edge) {x, y, w, head[x]}; head[x] = num++;
}
double ZhuLiu() {
double ans = 0; R = N;
while("Liang Liang") {
for(int i = 1; i <= N; i++) id[i] = vis[i] = 0, mn[i] = 1e9; int cnt = 0, x;
for(int i = 1; i <= num; i++)
if(E[i].v != E[i].u && (mn[E[i].v] > E[i].w))
mn[E[i].v] = E[i].w, fa[E[i].v] = E[i].u;
mn[R] = 0;
for(int i = 1; i <= N; i++) {
ans += mn[i];
for(x = i; (!id[x]) && (vis[x] != i) && x != R; x = fa[x]) vis[x] = i; //tag
if(x != R && (!id[x])) {
id[x] = ++cnt;
for(int t = fa[x]; t != x; t = fa[t]) id[t] = cnt;
}
}
if(cnt == 0) return ans;
for(int i = 1; i <= N; i++) if(!id[i]) id[i] = ++cnt;
for(int i = 1; i <= num; i++) {
double pre = mn[E[i].v];
if((E[i].u = id[E[i].u]) != (E[i].v = id[E[i].v])) E[i].w -= pre;
}
N = cnt; R = id[R];
}
return ans;
}
int main() {
N = read();
for(int i = 1; i <= N; i++) {
scanf("%lf", &val[i]), m[i] = read(), AddEdge(N + 1, i, val[i]);
}
N++;
M = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read(); double z; scanf("%lf", &z);
AddEdge(x, y, z); val[y] = min(val[y], z);
}
double ans = 0;
for(int i = 1; i <= N - 1; i++) ans += 1.0 * (m[i] - 1) * val[i];// printf("%d\n", m[i] - 1);
printf("%.2lf", ans + ZhuLiu());
return 0;
}