codeforces 21D. Traveling Graph 状压dp
题目链接
题目大意:
给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1。 所走的距离最短是多少。
如果这个图是一个欧拉回路, 即所有点的度数为偶数。 那么距离就是所有边的长度相加。
当有的点度数为奇数时, 我们可以在两个度数为奇数的点之间连一条边, 距离相当于这两个点之间的最短路。
所以最终答案就是所有边的长度相加+新加的边的长度。
加边的时候用状压dp枚举, 求出最小值。
对于状态s, 如果某一位是1, 表示这个点度数为偶数, 为0表示奇数。 然后转移就可以了。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int a[16][16], n, dp[1<<16], d[16];
void floyd() {
for(int k = 0; k < n; k++) {
for(int j = 0; j < n; j++) {
for(int i = 0; i < n; i++) {
a[i][j] = min(a[i][j], a[i][k]+a[k][j]);
}
}
}
}
int solve() {
floyd();
for(int i = 0; i < n; i++) {
if(a[0][i] == inf && d[i])
return -1;
}
mem2(dp);
dp[0] = 0;
for(int s = 0; s < (1<<n); s++) {
int flag = 0;
for(int i = 0; i < n; i++) {
if(d[i] % 2 && (s>>i&1)) {
flag = 1;
break;
}
}
if(!flag) {
dp[s] = 0;
}
for(int i = 0; i < n; i++) {
if(d[i] % 2 && !(s>>i&1)) {
for(int j = i + 1; j < n; j++) {
if(d[j]%2 && !(s>>j&1) && a[i][j] != inf) {
int tmp = s | (1<<j) | (1<<i);
dp[tmp] = min(dp[tmp],dp[s] + a[i][j]);
}
}
}
}
}
return dp[(1<<n)-1] == inf?-1:dp[(1<<n)-1];
}
int main()
{
int m, x, y, w, sum = 0;
cin >> n >> m;
mem2(a);
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &x, &y, &w);
x--, y--;
d[x]++, d[y]++;
sum += w;
a[x][y] = min(a[x][y], w);
a[y][x] = a[x][y];
}
for(int i = 0; i < n; i++)
a[i][i] = 0;
int tmp = solve();
if(tmp == -1) {
puts("-1");
} else {
cout<<tmp+sum<<endl;
}
return 0;
}
codeforces 21D. Traveling Graph 状压dp的更多相关文章
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...
- Codeforces 79D - Password(状压 dp+差分转化)
Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...
- Codeforces 544E Remembering Strings 状压dp
题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...
- Codeforces 895C - Square Subsets 状压DP
题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...
- CodeForces 327E Axis Walking(状压DP+卡常技巧)
Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...
- Codeforces ----- Kefa and Dishes [状压dp]
题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...
- CodeForces 907E Party(bfs+状压DP)
Arseny likes to organize parties and invite people to it. However, not only friends come to his part ...
- codeforces#1215E. Marbles(状压dp)
题目链接: http://codeforces.com/contest/1215/problem/E 题意: 至少多少次操作可以使得相同的数都是相邻的 每次操作可以交换两个相邻的数 数据范围: $1\ ...
随机推荐
- Ubuntu kylin 有可能成为未来中国的主流系统吗?
编前语: 无意间开始研究起linux,因为目前互联网很多人,包括我都隐约感觉到,windows系统在中国乃至世界在今后的流行度会逐步降低,不为什么,其中最主要的是安全问题,Microsoft 微软公司 ...
- Ubuntu 13.10 下安装 eclipse
Ubuntu软件社区用的3.8,个人想用最新版本,所有手动下载安装. 1.下载安装Jdk sudo apt-get install openjdk-7-jdk 2.查看系统JVM sudo updat ...
- codevs1127
学校里有一个水房,水房里一共装有m 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为1. 现在有n 名同学准备接水,他们的初始接水顺序已经确定.将这些同学按接水顺序从1到n 编号,i 号同学的 ...
- python基础之语句结束
1 2 3 4 5 if a : if b: # 这里是if b的作用区间 #这里是if a的作用区间 #这里不在if 区间 python 是按缩进来识别代码块的.
- 用OpenCV实现Otsu算法
算法的介绍 otsu法(最大类间方差法,有时也称之为大津算法)使用的是聚类的思想,把图像的灰度数按灰度级分成2个部分,使得两个部分之间的灰度值差异最大,每个部分之间的灰度差异最小,通过方差的计算来寻找 ...
- 以Python列表特性为切入点的求质数列表的方法
一般,构造一个含有2-x之间所有质数的列表,我们采用最简单的遍历判断质数的方法: # 方法一 1 prime = [] def is_prime(n): if n <= 1: return Fa ...
- Python学习之编写登陆接口(Day1,作业一)
作业一:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定(下次登陆还是锁定) 知识点:while循环,for循环,文件操作,if判断,列表操作 思路: 1.登陆,三次登陆失败,锁定 ...
- jQuery随记
每次申明一个jQuery对象的时候,返回的是jQuery.prototype.init对象,很多人就会不明白,init明明是jQuery.fn的方法啊,实际上这里不是方法,而是init的构造函数,因为 ...
- 启用 ASP.NET MVC 项目的 Edit and Continue
VS 的 Edit and Continue 功能允许你在 Debug 的过程中,修改代码并且编译运行修改后的代码.对于编程阶段非常的好用,不需要你停止正在进行的 Debug,修改代码然后运行代码. ...
- C4.5较ID3的改进
1.ID3选择最大化Information Gain的属性进行划分 C4.5选择最大化Gain Ratio的属性进行划分 规避问题:ID3偏好将数据分为很多份的属性 解决:将划分后数据集的个数考虑 ...