牛客网 十二桥问题(状压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 ...
随机推荐
- 【Unity Shader】syntax error: unexpected token 'struct' at line x 错误解决办法
以下代码处出现了syntax error: unexpected token 'struct' at line 33的错误 struct a2v { float4 vertex_position : ...
- kruskar重构树
只略略讲一点基本方式与思想了 构建 并查集,边按从小(大)到大(小)加入,建新点,点权为此边权,该点为两点根的父亲. 性质:(此处为最小生成树重构树) 1.lca(u,v)为u到v路径上的最大边权 2 ...
- Mac 睡眠唤醒 不睡眠 问题
问题 之前一直有夜晚睡觉前电脑关机的习惯,主要是想着电脑也跟人一样️要休息.然后最近想着自己 Mac 干脆每天睡眠算了,省得每天开关机麻烦,所以就最近这段时间每次夜晚睡觉前主动去点了电脑的 「Slee ...
- ARM学习1
ARM相关概念 1.ARM的发展史 1. 1978年,CPU公司 Cambridge processing Unit 2. 1979年 Acorn 3. 1985年, 32位,8MHz, 使用的精简指 ...
- 2021.06.05【NOIP提高B组】模拟 总结
T1 题意:给你一个 \(n\) 个点 \(n\) 条边的有向图, 求每个店经过 \(K\) 条边后的边权和.最小边权 \(K\le 10^{10}\) 考试时:一直想着环,结果一直不知道怎么做 正解 ...
- 入坑KeePass(一)安全桌面输入管理密钥后,不能输入中文
坑一:设置了在安全桌面输入管理密钥后,重启电脑后今日keepass软件中不能输入中文: 解决方式:进入keepass软件中取消勾选,在重启软件就可以输入中文了.
- 开始讨论离散型随机变量吧!《考研概率论学习之我见》 -by zobol
上一文中,笔者给出了随机变量的基本定义:一个可测映射,从结果空间到实数集,我们的目的是为了引入函数这个数学工具到考研概率论中,但是我们在现实中面对的一些事情结果,映射而成的随机变量和其对应的概率值,并 ...
- 驱动开发实战之TcpClient
场景模拟 假设你有一批非标设备需要对接,对方提供了如下协议文档: 协议概述 设备作为TCPServer,端口6666 字节序:Little-Endian,即低地址存放低位 请求回复 需要你主动发起读取 ...
- python+anaconda+pycharm的使用
研一开学的时候开始接触了这些,但是对于其各种功能感到十分混乱,现在通过这篇博文将其功能详细的写出来. 1.python解释器 首先要了解python解释器,我们俗称的下载python也就是下载pyth ...
- UiPath从入门到精通视频教程
匠厂出品,必属精品 Uipath中文社区qq交流群:465630324 微信小程序搜索RPA之家小店可以购买相关RPA的课程,现在联系有优惠 官网:https://rpazj.com uipath ...