【codeforces】gym 101137 K - Knights of the Old Republic【用最小生成树对图做集合dp】
2016-11-02 20:40
656 查看
题目链接:【codeforces】gym 101137 K - Knights of the Old Republic
考虑对图集合dp,一个连通块的dp值为两个连通块的值的和或者强制加一条新边后的最小值,取个最小值(边从小到大枚举,则强制加一条最大的边会导致连通块内较小的边一定都选,则会构成一个生成树)。用kruskal实现这个dp过程即可。
考虑对图集合dp,一个连通块的dp值为两个连通块的值的和或者强制加一条新边后的最小值,取个最小值(边从小到大枚举,则强制加一条最大的边会导致连通块内较小的边一定都选,则会构成一个生成树)。用kruskal实现这个dp过程即可。
#include <bits/stdc++.h> using namespace std ; typedef long long LL ; const int MAXN = 300005 ; struct Edge { int u , v , c ; bool operator < ( const Edge& a ) const { return c < a.c ; } } ; Edge E[MAXN] ; int a[MAXN] , b[MAXN] , p[MAXN] ; LL sum[MAXN] ; int n , m ; int F ( int x ) { return p[x] == x ? x : ( p[x] = F ( p[x] ) ) ; } void solve () { for ( int i = 1 ; i <= n ; ++ i ) { p[i] = i ; } for ( int i = 1 ; i <= n ; ++ i ) { scanf ( "%d%d" , &a[i] , &b[i] ) ; sum[i] = 1LL * a[i] * b[i] ; } for ( int i = 0 ; i < m ; ++ i ) { scanf ( "%d%d%d" , &E[i].u , &E[i].v , &E[i].c ) ; } sort ( E , E + m ) ; for ( int i = 0 ; i < m ; ++ i ) { int x = F ( E[i].u ) ; int y = F ( E[i].v ) ; if ( x == y ) continue ; int na = max ( max ( a[x] , a[y] ) , E[i].c ) ; int nb = min ( b[x] , b[y] ) ; LL tmp = 1LL * na * nb ; sum[y] = min ( sum[x] + sum[y] , tmp ) ; p[x] = y ; a[y] = na ; b[y] = nb ; } LL ans = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { if ( F ( i ) == i ) ans += sum[i] ; } printf ( "%lld\n" , ans ) ; } int main () { while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ; return 0 ; }
相关文章推荐
- codeforces gym 2016-2017 NEERC, Moscow Subregional K. Knights of the Old Republic 最小生成树+dp
- 【codeforces】gym 101138 K. The World of Trains【前缀和优化dp】
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest K. Knights of the Old Republic kruskal
- 【codeforces】gym 101137 E - Economy Printing【dp】
- 【 Gym - 101138K 】 The World of Trains (DP)
- Genghis Khan the Conqueror----HDU_4126----最小生成树_DFS_DP
- codeforces 602 E. Kleofáš and the n-thlon (概率dp)
- HDU 4126 Genghis Khan the Conqueror(最小生成树+树形DP)
- hdu3080 The plan of city rebuild(最小生成树)
- HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp
- CodeForces - 472D Design Tutorial: Inverse the Problem——最小生成树
- HDU 3080 The plan of city rebuild(除点最小生成树)
- hdu4126 Genghis Khan the Conqueror--最小生成树 & 树形DP(待解决)
- hdu-4126 Genghis Khan the Conqueror(最小生成树+树形dp)
- HDU 3080 The plan of city rebuild(除点最小生成树)
- CodeForces 472D Design Tutorial: Inverse the Problem (最小生成树+lca)
- HDU - 4126 Genghis Khan the Conqueror(树形DP + 最小生成树)
- HDU 4126 Genghis Khan the Conqueror(树形dp,MST,破坏原有最小生成树边后的最小生成树代价)
- The minimum spanning tree of a graph(图的最小生成树)
- CodeForces 472D Design Tutorial: Inverse the Problem (最小生成树)