题目链接

题目大意:

给一个无向图, 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. SQL 数据库知识点回顾

    SQL2008 一.安装注意事项: 1.修改用户权限(在一个安装页面中有七八个)(改成net.) 2.添加当前系统用户为账户 二.主键,约束,索引 三.增删改查: insert.delete.upda ...

  2. C++_String

    String -String的实现 -String常用操作

  3. Android 测试工具集01

    Appium是一个支持原生,混合和移动web apps的开源的跨平台测试框架工具. ANDROID依赖 Android SDK API >= 17 (Additional features re ...

  4. 做了一个jquery插件,使表格的标题列可左右拉伸

    示例下载 插件名称命名为:jquery.tableresize.js,代码如下: /* Writen by mlcactus, 2014-11-24 这是我封装的一个jquery插件,能够使table ...

  5. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  6. logstorm

    http://blog.itpub.net/15480802/viewspace-688859/ http://www.csdn.net/article/2014-09-04/2821558

  7. C#dll版本号默认生成规则

    原文:C#dll版本号默认生成规则 1.版本号自动生成方法 只需把 AssemblyInfo.cs文件中的[assembly: AssemblyVersion("1.0.0.0") ...

  8. Logstash 默认不处理一天前的文件

    The default behavior of the file input plugin is to ignore files whose last modification is greater ...

  9. 前端js模板库 JinkoTemplate

    有时候需要使用ajax来异步生成html,最土的方法就是用js的‘+’连接html代码,生成繁琐.一旦需要修改,对于少量的html代码到没啥问题,要是比较复杂的样式时,就真坑爹了,眼花缭乱有木有?Ji ...

  10. Android中API建议的方式实现SQLite数据库的增、删、改、查的操作

    package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import android. ...