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)的更多相关文章

  1. 牛客网 Wannafly挑战赛12 删除子串(线性dp)

    题目描述 给你一个长度为n且由a和b组成的字符串,你可以删除其中任意的部分(可以不删),使得删除后的子串“变化”次数小于等于m次且最长. 变化:如果a[i]!=a[i+1]则为一次变化.(且新的字符串 ...

  2. 牛客网 珂学送分( 期望DP )

    题意 : 题目链接 分析 : 听队友说一般概率从前往后推.期望从后往前推......... #include<bits/stdc++.h> using namespace std; ; d ...

  3. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  4. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  5. 牛客网 牛客练习赛4 A.Laptop-二维偏序+离散化+树状数组

    A.Laptop 链接:https://ac.nowcoder.com/acm/contest/16/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其 ...

  6. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)

    链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...

  7. 算法题 19 二叉平衡树检查 牛客网 CC150

    算法题 19 二叉平衡树检查 牛客网 CC150 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1. 给定指向树根结点的指针TreeNode* ro ...

  8. 牛客网第二场Jfarm(随机化+二维前缀和)

    链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 White Rabbit has a rectangular farmland of n*m. ...

  9. 牛客网 湖南大学2018年第十四届程序设计竞赛重现赛 A game

    链接:https://www.nowcoder.com/acm/contest/125/A来源:牛客网 Tony and Macle are good friends. One day they jo ...

随机推荐

  1. git bisect:让你闭眼都能定位疑难 bug的利器

    摘要:git bisect命令使用二分搜索算法来查找提交历史中的哪一次提交引入了错误.它几乎能让你闭着眼睛快速定位任何源码导致的问题,非常实用. 本文分享自华为云社区<利用好 git bisec ...

  2. 基于.NetCore开发博客项目 StarBlog - (9) 图片批量导入

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  3. 在 GitHub 上玩转开源项目的 Code Review

    一.幕后故事 时光荏苒,岁月如梭-- (太文绉绉了,这不是我的风格) 今天我准备聊聊在 GitHub 上如何玩 Code Review. 突发奇想?心血来潮?不是. 咋回事呢?(对八卦不感兴趣的可以直 ...

  4. Apache Shiro反序列化漏洞(Shiro550)

    1.漏洞原理: Shiro 是 Java 的一个安全框架,执行身份验证.授权.密码.会话管理 shiro默认使用了CookieRememberMeManager,其处理cookie的流程是:得到rem ...

  5. C#中的String Interpolation

    本文迁移自Panda666原博客,原发布时间:2021年4月17日. 在英文中,$符号表示美元符号(United States dollar).这也是很多人喜欢的东西.甚至是一生最求的东西.但在编程语 ...

  6. C++ 炼气期之算术运算符

    1. 前言 编写程序时,数据确定后,就需要为数据提供相应的处理逻辑(方案或算法).所谓逻辑有 2 种存在形态: 抽象形态:存在于意识形态,强调思考过程,与具体的编程语言无关. 具体形态:通过代码来实现 ...

  7. Linux文件的删除和软硬链接

    文件的构成 由元数据(metadata)和数据(data)两部分组成 硬盘分区上一块空间存该分区上文件的元数据,一块空间存这些文件的数据 因为元数据和数据分离存放,所以需要通过指针地址来进行关联 元数 ...

  8. IP寻址与规划

    一.IP寻址和子网划分 IP地址的主机部分可被分为三种地址:网络地址.主机地址和定向广播地址. 网络地址是网络号中的第一个地址.它用来将网络内的其他所有网段唯一标识为一个网段或广播域.定向广播地址是网 ...

  9. jquery通过id和class取值

    一.Jquery通过id获取Input文本框value值 二.Jquery通过id获取文本内容(1) 三.Jquery通过id获取文本内容(2) 四.Jquey通过class获取文本内容 (注:Jqu ...

  10. 女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!

    1. 背景 MySQL实现事务.崩溃恢复.集群的主从复制,底层都离不开日志,所以日志是MySQL的精华所在.只有了解MySQL日志,才算是彻底搞懂MySQL. 今天一灯就带你深入浅出的学习MySQL的 ...