Streamline improved (Spanning Tree dp)

Streamline improvement

   

0.00%

The number of submission: 1

The number by: 0

Title Description

 

Poor recently playing a hardcore war games.

Poor control of which has  n n cities, the  n have between cities n  m m bidirectional roads, the  i article urban road link i  u_i U i  and  V_I V i , and by the time this path is  W_i W I .

Recently, poor point technology called "Transfer", this technology can make poor soldiers can not go through any country road between two cities quickly transferred. So from the perspective of the war, the road almost no use, hoping to remove some of the poor roads, making road system as much as possible the inconvenience, so at the time of the invasion of the enemy will be able to gain an advantage.

Rules of the game rules at any time, you have to pass road between any two cities in the same country of arrival. Therefore, the poor need to ensure that after deleting the road, the road system is still Unicom.

Order  D (I, j) D ( I , j ) through road system from  I I city to the first  j minimum time j systems, poor definition of a road system complexity is  \ sum_ {i = 1} ^ n-\ sum_ {J} = I ^ Nd +. 1 (I, J) [Sigma I = . 1 n- [Sigma J = I + . 1 n- D ( I , J ). The poor want to delete some of the roads, in the case of guarantee diagram Unicom, the complexity of the road system as large as possible.

 

 
 

Enter a description

 

Line of input two integers  n-, m (. 1 \ n-Leq \ Leq 14,. 1 \ Leq m \ Leq \ {n-FRAC (. 1-n-)} {2}) n- , m ( . 1 n- . 1 . 4 , . 1 m 2 n- ( n- - . 1 ) ), the initial number of cities represents the number of roads.

接下来 mm 行每行输入三个整数 u_i,v_i,w_i(1 \leq u_i,v_i \leq n, 1 \leq w_i \leq 10^9)ui,vi,wi(1ui,vin,1wi109),描述了一条道路。

输入保证图中没有自环、重边,同时图是联通的。

 

输出描述

 

输出一行一个整数,表示道路系统最大的可能的复杂度。

 

样例输入 1 

5 5
1 2 1
1 3 1
2 4 1
2 5 1
1 5 1

样例输出 1

20

样例输入 2 

5 10
1 2 1
1 3 2
1 4 3
1 5 4
2 3 5
2 4 6
2 5 7
3 4 8
3 5 9
4 5 10

样例输出 2

146




生成树dp 套路
dp[s][i] 集合 s,以i为树根的 什么什么东西
两个集合合并的时候考虑连接两个集的边
然后用到了子集的遍历




 
       
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define ll long long
 5 #define INFLL 0x3f3f3f3f3f3f3f3f
 6 #define N 110
 7 int n, m;
 8 int dist[20][20];
 9 ll f[1 << 15][15];
10 
11 vector <int> vec[1 << 15];
12 int sze[1 << 15];
13 
14 int main()
15 {
16     while (scanf("%d%d", &n, &m) != EOF)
17     {
18         memset(dist, -1, sizeof dist); 
19         memset(f, -1, sizeof f);
20         for (int S = 0, len = (1 << n); S < len; ++S) 
21         {
22             for (int i = 0; i < n; ++i) if ((S >> i) & 1)
23                 vec[S].push_back(i + 1); 
24             sze[S] = vec[S].size();
25             if (sze[S] == 1)
26                 f[S][*vec[S].begin()] = 0; 
27         }    
28         for (int i = 1, u, v, w; i <= m; ++i)
29         {
30             scanf("%d%d%d", &u, &v, &w);
31             dist[u][v] = dist[v][u] = w; 
32         }
33         ll res = 0;  
34         for (int S = 1, len = (1 << n); S < len; ++S)
35         {  
36             for (auto u : vec[S])
37             {
38                 for (int T = (S - 1) & S; T != 0; T = (T - 1) & S) // 枚举子集  
39                 {
40                     for (auto v : vec[T])
41                     {
42                         if (dist[u][v] == -1 || f[T][v] == -1 || f[S - T][u] == -1) continue;   
43                         f[S][u] = max(f[S][u], f[T][v] + f[S - T][u] + 1ll * dist[u][v] * sze[T] * (n - sze[T]));  
44                     }
45                 }
46                 if (S == (1 << n) - 1)res = max(res, f[S][u]);
47             }
48         }
49         printf("%lld\n", res);
50     }
51     return 0;
52 }

 








 

Guess you like

Origin www.cnblogs.com/zhangbuang/p/10956909.html