牛客网 十二桥问题(状压DP)
https://ac.nowcoder.com/acm/contest/1104/B
注意到\(\text{K}\)只有\(12\),因此对起点与每个毕经边对应的点单源最短路,\(\text{DP}\)出最优解
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define MP make_pair
#ifdef QWQ
#define D_e_Line printf("\n------\n")
#define D_e(x) cerr << (#x) << " " << x << endl
#define C_e(x) cout << (#x) << " " << x << endl
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <cassert>
#define PASS fprintf(stderr, "Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#else
#define D_e_Line
#define D_e(x)
#define C_e(x)
#define FileOpen()
#define FileSave()
#define Pause()
#define PASS
#endif
using namespace std;
struct FastIO {
template<typename ATP> inline FastIO& operator >> (ATP &x) {
x = 0; int sign = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') sign = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
if(sign == -1) x = -x;
return *this;
}
} io;
template<typename ATP> inline ATP Max(ATP x, ATP y) {
return x > y ? x : y;
}
template<typename ATP> inline ATP Min(ATP x, ATP y) {
return x < y ? x : y;
}
template<typename ATP> inline ATP Abs(ATP x) {
return x < 0 ? -x : x;
}
#include <climits>
#include <vector>
#include <queue>
#define int long long
const int N = 5e4 + 7;
const int M = 2e5 + 7;
vector<pair<int, int>> G[N];
struct E {
int x, y, w;
} a[N];
struct nod {
int x, w;
nod(int _x, int _w) : x(_x), w(_w) {}
bool operator < (const nod &rhs) const {
return w > rhs.w;
}
};
int dis[N], val[N], b[N], id[N], f[31][1 << 13], d[31][31];
#undef int
int main() {
FileOpen();
#define int long long
int n, m, K;
io >> n >> m >> K;
R(i,1,m){
int u, v, w;
io >> u >> v >> w;
G[u].emplace_back(MP(v, w));
G[v].emplace_back(MP(u, w));
if(i <= K) a[i] = (E){ u, v, w};
}
auto Dijkstra = [&](int st) {
memset(dis, 0x3f, (n + 3) * sizeof(int));
priority_queue<nod> q;
dis[st] = 0;
q.emplace(st, 0);
while(!q.empty()){
auto u = q.top().x, w = q.top().w;
q.pop();
if(dis[u] != w) continue;
for(auto &i : G[u]){
int v = i.first;
if(dis[v] > dis[u] + i.second){
dis[v] = dis[u] + i.second;
q.emplace(v, dis[v]);
}
}
}
};
PASS;
Dijkstra(1);
memcpy(val, dis, (n + 3) * sizeof(int));
int tot = K << 1;
R(i,1,K) b[i * 2 - 1] = a[i].x, b[i << 1] = a[i].y;
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - b - 1;
R(i,1,tot) id[b[i]] = i;
R(i,1,tot){
Dijkstra(b[i]);
R(j,1,tot) d[i][j] = dis[b[j]];
}
auto ans = LLONG_MAX;
int maxn = (1 << K) - 1;
R(i,1,tot){
memset(f, 0x3f, sizeof(f)), f[i][0] = 0;
R(s,0,maxn){
R(j,1,tot){
if(f[j][s] == 0x3f3f3f3f) continue;
R(k,1,K){
if(s >> (k - 1) & 1) continue;
int x = id[a[k].x], y = id[a[k].y];
f[x][s ^ (1 << (k - 1))] = Min(f[x][s ^ (1 << (k - 1))], f[j][s] + d[j][y] + a[k].w);
f[y][s ^ (1 << (k - 1))] = Min(f[y][s ^ (1 << (k - 1))], f[j][s] + d[j][x] + a[k].w);
}
}
}
R(j,1,tot){
ans = Min(ans, (long long)val[b[i]] + f[j][maxn] + val[b[j]]);
}
}
printf("%lld", ans);
return 0;
}
/*
3 4 2
2 3 5
2 2 10
1 2 1
3 1 4
*/
牛客网 十二桥问题(状压DP)的更多相关文章
- 牛客网 Wannafly挑战赛12 删除子串(线性dp)
题目描述 给你一个长度为n且由a和b组成的字符串,你可以删除其中任意的部分(可以不删),使得删除后的子串“变化”次数小于等于m次且最长. 变化:如果a[i]!=a[i+1]则为一次变化.(且新的字符串 ...
- 牛客网 珂学送分( 期望DP )
题意 : 题目链接 分析 : 听队友说一般概率从前往后推.期望从后往前推......... #include<bits/stdc++.h> using namespace std; ; d ...
- 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)
链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 牛客网 牛客练习赛4 A.Laptop-二维偏序+离散化+树状数组
A.Laptop 链接:https://ac.nowcoder.com/acm/contest/16/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其 ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)
链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...
- 算法题 19 二叉平衡树检查 牛客网 CC150
算法题 19 二叉平衡树检查 牛客网 CC150 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1. 给定指向树根结点的指针TreeNode* ro ...
- 牛客网第二场Jfarm(随机化+二维前缀和)
链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 White Rabbit has a rectangular farmland of n*m. ...
- 牛客网 湖南大学2018年第十四届程序设计竞赛重现赛 A game
链接:https://www.nowcoder.com/acm/contest/125/A来源:牛客网 Tony and Macle are good friends. One day they jo ...
随机推荐
- python并发编程之线程/协程
python并发编程之线程/协程 part 4: 异步阻塞例子与生产者消费者模型 同步阻塞 调用函数必须等待结果\cpu没工作input sleep recv accept connect get 同 ...
- LSP原则是什么
如果这篇文章能够帮到您,请给我一个免费的赞,谢谢QWQ! LSP原则并不难,但是地方就会把它说的很啰嗦,如果你对LSP还是感到疑惑,请往下看看. 先上代码: public class Bird { p ...
- conda创建/移除虚拟环境
conda创建python虚拟环境 前言 conda常用的命令: conda list 查看安装了哪些包. conda env list 或 conda info -e 查看当前存在哪些虚拟环境 co ...
- 大数据分析VMWare虚拟机centos系统下配置网络参数
最近搞大数据方面的数据,通过网上视频学习和自己实践,有些不成文的实践就零碎的记录在此吧. 系统也可安装CentOS DVD版. 1.先进入配置文件,并查看基本情况,如下: 2.用vi编辑器打开物理网卡 ...
- DirectX11 With Windows SDK--19(Dev) 编译Assimp并加载模型、新的Effects框架
前言 注意:这一章进行了重写,对应教程Dev分支第19章的项目,在更新完后面的项目后会替换掉原来第19章的教程 在前面的章节中我们一直使用的是由代码生成的几何模型,但现在我们希望能够导入模型设计师生成 ...
- 在Rally上,上传测试报告(文件)到每个Test Case方法
本文链接: https://www.cnblogs.com/hchengmx/p/how-to-upload-test-result-to-test-case-result-in-rally.html ...
- 教你如何用网页开发APP
用到的工具: HBuilderX app开发版1.首先你得网站必须是上线的,然后明确这一点后,点击打开HBuilderX.在文件里找到新建项目,选择wap2App,将下面信息填写完整,然后创建. 2. ...
- TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
正文 研发测试场景下,一般追求的是一键快速起环境,横向动态复制,一人一套,随起随用,用完即走.作为使用方,其不用关心实际的物理资源是怎样的,环境起在哪里,只要声明自己的使用需求即可.但作为方案构建者以 ...
- Linux 安装Apche服务
用yum 进行在线安装apche服务 yum install -y httpd 我这边是centos7 需要开启一下端口: 1 firewall-cmd --zone=public --add-por ...
- 【cartogarpher_ros】一: ros系统下的快速安装
Cartographer是一个跨多个平台和传感器配置提供 2D 和 3D实时同步定位和映射 ( SLAM ) 的系统. 使用Cartographer有Ros集成环境和无Ros环境,对于新手快速入门,推 ...