题目链接:https://www.luogu.org/problemnew/show/P3959

我只是心血来潮想学SA(考场上骗分总行吧)。

这个题可以状压DP、爆搜+剪枝、有意思的还是随机化搜索(是的,这个题用的不叫SA,没有降温)。

code:

#include <queue>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000;
const int inf = 0x7fffffff;
int ma[13][13], deep[maxn], n, m, ans;
struct edge{
int u, v;
};
bool operator < (struct edge a, struct edge b)
{
return deep[a.u] * ma[a.u][a.v] > deep[b.u] * ma[b.u][b.v];
}
int sear(int s)//s 起点
{
int cnt = 0, cost = 0;
bool vis[maxn];
edge e[maxn], now, now2;
priority_queue<edge> q;
memset(deep, 0, sizeof(deep));
memset(vis, 0, sizeof(vis));
deep[s] = 1, vis[s] = 1;
for(int i = 1; i <= n; i++)
if(ma[s][i] < inf)
{
now.u = s, now.v = i;
//cout<<now.u<<" "<<now.v<<endl;
q.push(now);
}
for(int i = 1; i <= n-1; i++)
{
now = q.top(); q.pop();
while(!q.empty() && ((vis[now.v] || rand()%(n) < 1)))
{
if(!vis[now.v]) e[++cnt] = now;
now = q.top(); q.pop();
}
vis[now.v] = 1, deep[now.v] = deep[now.u] + 1;
//cout<<cnt;
for(int i = 1; i <= cnt; i++) q.push(e[i]);
for(int i = 1; i <= n; i++)
{
if(ma[now.v][i] != inf && !vis[i])
{
now2.u = now.v; now2.v = i; q.push(now2);
}
}
cost += ma[now.u][now.v] * deep[now.u];
}
return cost;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) ma[i][j] = inf;
for(int i = 1; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
ma[u][v] = min(w, ma[u][v]);
ma[v][u] = min(w, ma[v][u]);
}
srand(192608);
ans = inf; for(int i = 1; i <= 500; i++)
{
for(int j = 1; j <= n; j++)
ans = min(ans, sear(j));
}
printf("%d",ans);
return 0;
}

【luogu P3959 宝藏】 题解的更多相关文章

  1. [luogu]P3959 宝藏[NOIP][状态压缩DP]

    [luogu]P3959 宝藏[TREASURE] 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的 ...

  2. [Luogu P3959] 宝藏 (状压DP+枚举子集)

    题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...

  3. Luogu P3959 宝藏

    这道题正解是状压DP,不过我不会所以写一下随机化算法来骗骗分. 听说当时考场上就有很多写prim然后挂掉的神仙,其实这道题是可以prim过的 prim是一种基于贪心的算法,在本题中由于盲目的选择当前最 ...

  4. 【题解】P3959 宝藏 - 状压dp / dfs剪枝

    P3959 宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的m  条道路和它们的长度. 小明决心亲自前往挖掘所有宝 ...

  5. Luogu P2210 Haywire 题解

    其实这题吧...有一种玄学解法 这题的要求的就是一个最小化的顺序 那么,我们就不进想到了一种显然的写法 就是random_shuffle 什么?这不是乱搞的非正解吗 然而,正如一句话说的好 一个算法, ...

  6. P3959 宝藏

    P3959 宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 nn 个深埋在地下的宝藏屋, 也给出了这 nn 个宝藏屋之间可供开发的 mm 条道路和它们的长度. 小明决心亲自前往挖掘 ...

  7. 洛谷 P3959 宝藏 解题报告

    P3959 宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 \(n\) 个深埋在地下的宝藏屋, 也给出了这 \(n\) 个宝藏屋之间可供开发的 \(m\) 条道路和它们的长度. 小 ...

  8. luogu P3959(2017noipTG D2T2

    luogu P3959(2017noipTG D2T2 不知道为什么,这两天见了好多伪装成图的dp题,这道也是. 最短路只有40分,实际上可以从数据范围n<=12看出来是状压dp. soluti ...

  9. 题解 Luogu P3959 【宝藏】

    来一篇不那么慢的状压??? 话说这题根本没有紫题难度吧,数据还那么水 我是不会告诉你我被hack了 一看数据规模,n≤12,果断状压. 然后起点要枚举,就设dp状态: f[i][j]=以i为起点到j状 ...

随机推荐

  1. I/O处理小练习--保存用户账号密码

    I/O处理小练习--保存用户账号密码 用户输入姓名和密码,将每一个姓名和密码保存到文件中,输入done时程序结束 import java.io.*; public class Example { pu ...

  2. docker启动容器报错:IPv4 forwarding is disabled. Networking will not work

    报这个错误会导致宿主机以外的pc 访问不了容器 按照网上的解决办法: 在/usr/lib/sysctl.d/00-system.conf文件后加net.ipv4.ip_forward=1 即可 然后重 ...

  3. CentOS6.5下连网以及输入法下载

    宽带拨号连网: 1.系统--首选项--网络连接(或点击桌面右上角连网图标--VPN连接--VPN配置)       2.添加--选择DSL--勾自动连接(也可不勾)--DSL下填写用户名.密码--应用 ...

  4. 4.5&4.7联考题解

    本来想加个密码的,后来一想全HE就咱们这几个人,外省的dalao愿看也没事儿,就公开算了,省得加密码各种麻烦. 先补这两天的题解吧……如果有空的话我可能会把上次联考的题解补上= =(中午没睡觉,现在困 ...

  5. 1475 m进制转十进制

    1475 m进制转十进制  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 将m进制数n转化成一个十进制数 ...

  6. encodeURIComponent() 函数的使用

    说明:encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. 维护项目中,遇到一个登录的问题:(用户的loginName为33195221,密码为147258369+), ...

  7. 如何验证一份HTML文档的格式是否正确

    在浏览器中打开 Markup Validation Service . 点击或者激活 Validate by Direct Input 栏. 将整个示范文档的代码(不仅仅是body部分)复制粘贴到在M ...

  8. Flume -- Transfer one type of source to another type

    Source within Flume is a kind of Server for outside client. Sink within Flume is a kind of client fo ...

  9. MyBatis基本配置和实践(一)

    第一步:创建Java工程和数据库表user 第二步:使用Maven管理项目依赖 第三步:在resources目录下加入log4j.properties 第四步:在resources目录下加入SqlMa ...

  10. visual box 安装 centos7后,无法上网

    ifconfig  命令后,只看到个回环网卡: 进入 /etc/sysconfig/network-scripts后,发现有设备 visual box 中设置为“桥接网卡”,也没问题,最后把控制芯片改 ...