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\ ...
随机推荐
- UVA 246 10-20-30
题意: 给52张的扑克堆,先从左往右发7张牌,之后连续不断从左往右发7张牌,如果有牌堆形成了以下3种情况(按顺序判断):1.头两张+尾一张和为10或20或30.2.头一张+尾两张和为10或20或30. ...
- Redis系列整理
0.Redis系列-安装部署维护篇 1.Redis系列-远程连接redis并给redis加锁 2.Redis系列-存储篇string主要操作函数小结 3.Redis系列-存储篇list主要操作函数小结 ...
- php输出json中文显示编码-解决办法
$str = "中华人民共和国";$ar = array( "a" => "a0", "b" => &quo ...
- hdu 4707 Pet hdu 2013 Asia Regional Online —— Warmup
一道简单的搜索题目,建一个树,根节点是 0 ,连接的两个节点的距离是 1 ,求 到 根节点长度是2的节点的个数. #include<stdio.h> #include<string. ...
- [MFC]解决回车键 ESC 默认关闭窗口的一般方法
在一般情况下编写的对话框程序,用户在运行的时候,如果不注意按下了ENTER或者ESC键,程序就会立刻退出,之所以会这样,是因为按下Enter键时,Windows就会自动去找输入焦点落在了哪一个按钮上, ...
- 轻松搞定javascript原型链 _proto_
//如有错误或不同观点,欢迎批评与讨论! 首先,prototype出现的目的,是为了解决 代码重用 的问题 , prototype 相当于是在内存上划分出一个公共的区域, 专用于存放 实例化对象 的相 ...
- Mining 任务分类
1.预测任务: 根据其它属性的值预测特定属性的值. 2.描述性任务: 发现数据之间潜在的关联关系.
- EL表达式(转)
转自:http://www.cnblogs.com/Fskjb/archive/2009/07/05/1517192.html EL 全名为Expression Language EL 语法很简单,它 ...
- perl 创建包
<pre name="code" class="python"><pre name="code" class=" ...
- iOS5系统API和5个开源库的JSON解析速度测试
iOS5系统API和5个开源库的JSON解析速度测试 iOS5新增了JSON解析的API,我们将其和其他五个开源的JSON解析库进行了解析速度的测试,下面是测试的结果和工程代码附件. 我们选择的测试对 ...