题目链接

题目大意:

给一个无向图, 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. linux通过文件查找依赖关系

    通过文件查找安装包安装缺少libstdc++6这个文件在ls /usr/lib/libstd*下有两个文件/usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6 ...

  2. C# 获取本机IP地址以及转换字符串

    /// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址 ...

  3. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  4. Oracle 添加 scott 示例用户

    学习SQL有一段时间了,但是也忘记的差不多了,今天有赶紧复习复习,然后发现一个问题,为啥之前看的视频教程,马士兵用的Oracle有scott用户和那些表格,而我的没有?难道是Oracle取消了?然后百 ...

  5. Spring学习之切入点表达式

    链接地址:http://jinnianshilongnian.iteye.com/blog/1415606

  6. DataTable中执行DataTable.Select("条件"),

    我们在使用Sql ******这些数据库时,可以轻松的通过Sum.Aver.Count等统计出相关结果,那么,在已经把数据检索出来的DataSet(DataTable)中呢?特别是通过Web Serv ...

  7. 关于PHPExcel

    在学PHPExcel的时候,在网上查了很多资料,花了很多时间,下面是我想要分享给大家的,我找到的并进行了一定修改的亲身实践成功的资料,希望大家对大家有所帮助. 首先,需要下载PhpExcel资料,下载 ...

  8. 6 支持向量机SVM

    注:理论部分参考:http://blog.csdn.net/v_july_v/article/details/7624837 (1)SVM是现成最好的分类器,这里“现成”指的是分类器不加修改即可直接使 ...

  9. SQL Server 数据库文件 4 点注意

    1.数据库被分解成逻辑页面,每页8K,在每一个文件中页面从 0 --> x 连续编号: 2.可以通过 [database_id]   [file_id]   [page_id] 的形式引用页面: ...

  10. J2SE知识点摘记(三)

    1.         在java只允许单继承,而不允许多重继承,也就是说一个子类只能有一个父类.但在java中却允许多层继承. 2.         非内部Class只能被public或者defaul ...