题目大意:$NOIPD2T2$宝藏

题解:正常做法:状压DP 。这次模拟退火,随机一个排列,$O(n^2)$贪心按排列的顺序加入生成树

卡点:没开$long\;long$,接受较劣解时判断打错,没判$n=1​$的情况

C++ Code:

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#define maxn 14
const int inf = 0x3f3f3f3f;
const int Tim = 20;
const double ST = 500, DelT = 0.9, eps = 1e-5; inline int min(int a, int b) {return a < b ? a : b;}
inline double rand_d() {return static_cast<double> (rand()) / RAND_MAX;} int n, m;
int e[maxn][maxn];
int dep[maxn]; struct node {
int s[maxn];
long long ans;
inline long long calc() {
ans = 0;
dep[s[1]] = 1;
for (register int i = 2; i <= n; i++) {
long long MIN = inf;
for (register int j = 1; j < i; j++) if (e[s[i]][s[j]] != inf) {
long long tmp = static_cast<long long> (dep[s[j]]) * e[s[i]][s[j]];
if (tmp < MIN) MIN = tmp, dep[s[i]] = dep[s[j]] + 1;
}
ans += MIN;
}
return ans;
}
} ans, now, nxt; void SA() {
double T = ST;
long long del;
now = ans;
while (T > eps) {
int x = rand() % n + 1, y = rand() % n + 1;
while (x == y) x = rand() % n + 1, y = rand() % n + 1;
nxt = now;
std::swap(nxt.s[x], nxt.s[y]);
del = nxt.calc();
if (del < now.ans || exp((now.ans - del) / T) > rand_d()) now = nxt;
if (del < ans.ans) ans = nxt;
T *= DelT;
}
} int main() {
srand(20040826);
scanf("%d%d", &n, &m);
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) e[i][j] = e[j][i] = inf;
}
for (int i = 1, a, b, c; i <= m; i++) {
scanf("%d%d%d", &a, &b, &c);
e[b][a] = e[a][b] = min(e[a][b], c);
}
int __Tim = Tim;
for (int i = 1; i <= n; i++) ans.s[i] = i;
ans.calc();
while (__Tim --> 0) SA();
printf("%lld\n", ans.ans);
return 0;
}

  

[NOIP2017 TG D2T2]宝藏(模拟退火)的更多相关文章

  1. [NOIP2017 TG D2T2]宝藏

    题目大意:给定一个有重边,边有权值的无向图.从某一个点出发,求到达所有的点需要的最少费用,并且限制两点之间只有一条路径.费用的计算公式为:所有边的费用之和.而边$x->y$的费用就为:$y$到初 ...

  2. 【NOIP题解】NOIP2017 TG D2T3 列队

    列队,NOIP2017 TG D2T3. 树状数组经典题. 题目链接:洛谷. 题意: Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. ...

  3. [NOIP2018 TG D2T2]填数游戏

    题目大意:$NOIP2018\;TG\;D2T2$ 题解:在skip2004的博客基础上修改的,也是暴搜. 说明一下把vector改成数组并不可以通过此题,记录. 结论:在$m>n+1$时答案为 ...

  4. NOIP2017 D2T2宝藏

    考场上写的prim一遍过了大样例也没想什么别的,反例也没举出来. 后来才知道由于要乘上深度所以无法贪心. 正解是状压但我不会,考后一个爆搜碾过去了. 心凉. #include<bits/stdc ...

  5. NOIP2017 D2T2 宝藏

    洛谷P3959 其实就是一道暴力搜索题……只是需要一个状态压缩的剪枝比较难想而已 这根本不叫dfs!只是一个递归而已……开始就被dfs坑了 思路: 首先一个基本的预处理 数据范围n≤12,m≤5000 ...

  6. P3959 宝藏 模拟退火。。。

    竟然模拟退火能做!我就直接抄代码了,我加了点注释. 题干: 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 nn 个深埋在地下的宝藏屋, 也给出了这 nn 个宝藏屋之间可供开发的 mm ...

  7. noip2017 TG 游记

    嗨小朋友们大家好,还记得我是谁吗?我就是为GG代言的蒟蒻--xzz 今天呐我特别的要向HN的dalao们ZJ的巨佬们还有全国的神犇们问声好 为什么呢因为我们在2017年11月份来到了吔屎的长沙理工大学 ...

  8. [NOIP2017 TG D2T3]列队

    题目大意:有一个$n \times m$的方阵,第$i$行第$j$列的人的编号是$(i-1) \times m + j$. 现在有$q$个出列操作,每次让一个人出列,然后让这个人所在行向左看齐,再让最 ...

  9. NOIP2017[提高组] 宝藏 题解

    解析 我们观察范围可以发现n非常的小,(一般来说不是搜索就是状压dp)所以说对于这题我们可以用记忆化搜索或者dp,我们发现起点不同那么最终答案也就不同,也就是说答案是跟起点有关的,于是我们便可以想到去 ...

随机推荐

  1. html5 手风琴菜单

    因为项目需要,现在需要做个手风琴菜单,于是自己就瞎整了一下,所用只是less.js  javascript  jquery效果如图: 具体代码如下: <!DOCTYPE html> < ...

  2. C# WebClient 使用http免费代理

    static void Main(string[] args) { WebClient client = new WebClient(); client.Encoding = Encoding.Get ...

  3. eclipse全选包

    按住shift键,点击第一个jar包,然后点击最后一个jar包,就全选了所有jar包,然后添加build path 添加到类路径

  4. python应用:爬虫实例(静态网页)

    爬取起点中文网某本小说实例: # -*-coding:utf8-*- import requests import urllib import urllib2 from bs4 import Beau ...

  5. dijkstra算法学习

    dijkstra算法学习 一.最短路径 单源最短路径:计算源点到其他各顶点的最短路径的长度 全局最短路径:图中任意两点的最短路径 Dijkstra.Bellman-Ford.SPFA求单源最短路径 F ...

  6. Eclipse报错:An internal error occurred during: "Building workspace". Java heap space),卡死解决办法

    在项目工程的根目录下,找到.project,用记事本打开,把两处删除掉: 第一处: <buildCommand> <name>org.eclipse.wst.jsdt.core ...

  7. C++11中std::move的使用

    std::move is used to indicate that an object t may be "moved from", i.e. allowing the effi ...

  8. 9 udp广播

    udp有广播  写信 tcp没有广播·  打电话 #coding=utf-8 import socket, sys dest = ('<broadcast>', 7788) # 创建udp ...

  9. 【JDK配置原创】JDK(JRE)环境变量配置原理 --费元星

    已经配置了很多次jdk了,每次都是安装网上的教程配的,今天突然想了解了解原理了,整理一下! 参考贴( http://blog.csdn.net/wkupaochuan/article/details/ ...

  10. TensorLayer 中文文档

    TensorLayer 中文文档 好消息 我们获得了 ACM Multimedia (MM) 年度最佳开源软件奖. TensorLayer 是为研究人员和工程师设计的一款基于Google Tensor ...