去年NOIP第二毒瘤(并不)的题终于被我攻克了,接下来就只剩noip难度巅峰列队了。
首先说一下三种做法:随机化,状压DP和搜索。
前两种做法我都A了,搜索实在是毒瘤,写鬼啊。
有些带DFS的记忆化搜索版状压DP,我看着感觉有点问题......
就连随机化里面那个贪心我看起来也感觉是错的.....但还是A了。
所以考试的时候不要虚,大胆写随机化,随便混个几十分,说不定就A了。
20分算法:给定一棵树,枚举根即可。
1 #include <cstdio> 2 #include <algorithm> 3 4 const int N = 13, M = 1010, INF = 0x7f7f7f7f; 5 6 int G[N][N], n; 7 8 int DFS(int x, int k, int f) { 9 int ans = 0; 10 for(int i = 1; i <= n; i++) { 11 if(G[x][i] && i != f) { 12 ans += DFS(i, k + 1, x); 13 ans += G[x][i] * k; 14 } 15 } 16 return ans; 17 } 18 19 int main() { 20 int m; 21 scanf("%d%d", &n, &m); 22 for(int i = 1, x, y, z; i <= m; i++) { 23 scanf("%d%d%d", &x, &y, &z); 24 if(G[x][y]) { 25 z = std::min(z, G[x][y]); 26 } 27 G[x][y] = G[y][x] = z; 28 } 29 30 int ans = INF; 31 for(int i = 1; i <= n; i++) { 32 ans = std::min(ans, DFS(i, 1, 0)); 33 } 34 printf("%d", ans); 35 36 return 0; 37 }