Gym - 101137K Knights of the Old Republic (Kruskal+dp)

题目链接

留坑,题解之后再补

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 typedef double db;
 5 const int N=3e5+10;
 6 struct E {
 7     int u,v,c;
 8     bool operator<(const E& b)const {return c<b.c;}
 9 } e[N];
10 int n,m,a[N],b[N],fa[N];
11 ll mi[N],mx[N],dp[N];
12 int fd(int x) {return ~fa[x]?fa[x]=fd(fa[x]):x;}
13 void mg(int x,int y,int c) {
14     int fx=fd(x),fy=fd(y);
15     if(fx==fy)return;
16     fa[fx]=fy;
17     mx[fy]=max((ll)c,max(mx[fx],mx[fy]));
18     mi[fy]=min(mi[fx],mi[fy]);
19     dp[fy]=min(dp[fx]+dp[fy],mi[fy]*mx[fy]);
20 }
21 
22 ll kruskal() {
23     ll ret=0;
24     sort(e+1,e+1+m);
25     for(int i=1; i<=n; ++i)fa[i]=-1,mx[i]=a[i],mi[i]=b[i],dp[i]=mx[i]*mi[i];
26     for(int i=1; i<=m; ++i) {
27         int u=e[i].u,v=e[i].v,c=e[i].c;
28         mg(u,v,c);
29     }
30     for(int i=1; i<=n; ++i)if(!~fa[i])ret+=dp[i];
31     return ret;
32 }
33 
34 int main() {
35     scanf("%d%d",&n,&m);
36     for(int i=1; i<=n; ++i)scanf("%d%d",&a[i],&b[i]);
37     for(int i=1; i<=m; ++i)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
38     printf("%lld\n",kruskal());
39     return 0;
40 }

 

posted @ 2021-04-03 20:16  jrltx  阅读(48)  评论(0编辑  收藏  举报