题目链接

题目大意:

给一个无向图, 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的更多相关文章

  1. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  2. Codeforces 917C - Pollywog(状压 dp+矩阵优化)

    UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...

  3. Codeforces 79D - Password(状压 dp+差分转化)

    Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...

  4. Codeforces 544E Remembering Strings 状压dp

    题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...

  5. Codeforces 895C - Square Subsets 状压DP

    题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...

  6. CodeForces 327E Axis Walking(状压DP+卡常技巧)

    Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...

  7. Codeforces ----- Kefa and Dishes [状压dp]

    题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...

  8. CodeForces 907E Party(bfs+状压DP)

    Arseny likes to organize parties and invite people to it. However, not only friends come to his part ...

  9. codeforces#1215E. Marbles(状压dp)

    题目链接: http://codeforces.com/contest/1215/problem/E 题意: 至少多少次操作可以使得相同的数都是相邻的 每次操作可以交换两个相邻的数 数据范围: $1\ ...

随机推荐

  1. android代码集锦

    调用root权限的应用: /** * 执行Command命令的函数 * * @param command 命令 * @return 执行结果 */ public static boolean runR ...

  2. 读书笔记一 Java程序员的基本修养(数组及其内存管理)

    1.1 数组初始化 1.1.1 java数组是静态的 java数组被初始化之后,该数组所占的内存空间.数组长度都是不可变的. java程序中的数组必须经过初始化才可使用. 数组的初始化有两种方式: 1 ...

  3. jsoup 解析html 页面数据

    我html 页面元素: /html/body/table[2]/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/font/html/body/table[2]/tbody ...

  4. Linux下进程的文件访问权限

    本文转自 http://blog.csdn.net/chosen0ne/article/details/10581883 对进程校验文件访问权限包括两个部分,一是确定进程的角色(属于哪个用户或者组), ...

  5. (转)OpenGL中位图的操作(glReadPixels,glDrawPixels和glCopyPixels应用举例)

    (一)BMP文件格式简单介绍 BMP文件是一种像素文件,它保存了一幅图象中所有的像素.这种文件格式可以保存单色位图.16色或256色索引模式像素图.24位真彩色图象,每种模式种单一像素的大小分别为1/ ...

  6. putty修改编码

    在窗口标题上点击右键,选择 Change Settings... 在打开的配置窗口左边选择 Appearance,在右边点 Font settings 里面的 Change 按钮,选择好中文字体,比如 ...

  7. sql update小结

    以前update用的不少,但都是简单的单表操作,没有在意,最近查阅多表关联更新及更新top n,发现update还真灵活,记录如下(在mssqlserver2008r2下测试通过): 1单表操作  u ...

  8. redBag

    var redBag = (function () { var initialed = false, raining = true, createInterval, walkInterval, cre ...

  9. yii2 安装过程中的问题及解决方法

    一.php版本要求5.4+,如果使用wamp组合包,建议更换 二.各种模块的支持,一般只要修改php.ini文件,去掉相应模块前的注释即可. 注意,Intl extension模块的支持需要将     ...

  10. cx_Oracle模块详解

    1.安装cx_Oracle模块 1-1.环境准备: 1-1-1.oracle client最小安装 instantclient-sqlplus-linux.x64-11.2.0.4.0 instant ...