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. 关于c#多线程中的几个信号量

    信号量在c#多线程通信中主要用来向阻塞的线程传达信号从而使得阻塞线程继续执行 多线程信号(线程交互):通常是指线程必须等待一个线程或者多个线程通知交互(释放信号)才可以继续执行 在c#中信号量主要有这 ...

  2. 微服务效率工具 goctl 深度解析(上)

    前言 本文根据 安前松 的视频分享整理而来,视频回放地址如下: https://www.bilibili.com/video/BV1Hr4y1x7Ne goctl 的由来 1. goctl 的诞生 g ...

  3. Excel导表工具-开源

    功能 支持int.float.bool.string基础类型 支持数组 支持kv 支持枚举 支持unity类型vector3,vector2,color 自动生成csharp类 单个excel中多个s ...

  4. 用Arduino显示颜色序列(u8g2,OLED)

    目录 用Arduino显示颜色序列(u8g2,OLED) 用Arduino显示颜色序列(u8g2,OLED) 提前祝大家新年快乐! 主控:Arduino Mega 2560 硬件:126×64 OLE ...

  5. 深度学习与CV教程(12) | 目标检测 (两阶段,R-CNN系列)

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/37 本文地址:http://www.showmeai.tech/article-det ...

  6. linux运维基础1

    内容概要 运维简介 运维岗位职责 服务器 服务器硬件介绍 磁盘阵列 虚拟化软件及环境 虚拟化软件 安装操作系统 Vmware虚拟机安装及相关配置流程 内容详情 运维简介 运维岗位职责 核心:运行维护应 ...

  7. 十二张图:从0开始理解对称/非对称加密、CA认证、以及K8S各组件颁发证书原由

    目录 一.对称加密 二.对称加密-不安全 三.非对称加密 四.非对称加密-不安全 五.对称加密和非对称加密结合 六.对称加密和非对称加密结合-不安全 七.Https的做法-引入CA机构 八.乘胜追击理 ...

  8. 技术分享 | app自动化测试(Android)--App 控件定位

    原文链接 客户端的页面通过 XML 来实现 UI 的布局,页面的 UI 布局作为一个树形结构,而树叶被定义为节点.这里的节点也就对应了要定位的元素,节点的上级节点,定义了元素的布局结构.在 XML 布 ...

  9. Java学习-第一部分-第一阶段-第二节:变量

    变量 变量介绍 为什么需要变量 变量是程序的基本组成单位 不论是使用哪种高级程序语言编写程序,变量都是其程序的基本组成单位,比如: //变量有三个基本要素(类型+名称+值) class Test{ p ...

  10. 基于web3D展示技术的煤矿巷道3D可视化系统

    地下开采离不开巷道工程.煤矿的生产.运输.排水.通风等各个环节都少不了巷道的支持.在煤矿智能化建设被提上日程的今天,巷道工程的智能化.可视化建设也成了行业趋势.尤其是复杂的井下作业环境,人员信息安全问 ...