codeforces 21D:Traveling Graph
Description
You are given undirected weighted graph. Find the length of the shortest cycle which starts from the vertex 1 and passes throught all the edges at least once. Graph may contain multiply edges between a pair of vertices and loops (edges from the vertex to itself).
The first line of the input contains two integers n and m (1 ≤ n ≤ 15, 0 ≤ m ≤ 2000), n is the amount of vertices, and m is the amount of edges. Following m lines contain edges as a triples x, y, w (1 ≤ x, y ≤ n, 1 ≤ w ≤ 10000), x, y are edge endpoints, and w is the edge length.
Output minimal cycle length or -1 if it doesn't exists.
3 3
1 2 1
2 3 1
3 1 1
3
3 2
1 2 3
2 3 4
14 正解:状压DP
解题报告:
考虑度数为偶数的点不需要被考虑,只需要考虑度数为奇数的情况。首先每条边必须要访问一次,所以所有边权加起来就是答案的初值。
然后度数为奇数的点就需要访问之前已经走过的边。我们考虑两个度数为奇数的点可以组合一下,变成度数为偶数的点,相当于是在这两个点之间新连了一条边,我们可以floyd预处理一下两点之间的最短路。然后状压,状态表示当前哪些结点的度数为奇数,然后枚举每次连哪两条边就可以了。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
const int MAXS = (<<);
int n,m,ans,end;
int w[MAXN][MAXN];
int d[MAXN],f[MAXS]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void work(){
n=getint(); m=getint(); int x,y,z,now; if(m==) { printf(""); return ; }
for(int i=;i<=n;i++) for(int j=;j<=n;j++) w[i][j]=inf;
for(int i=;i<=m;i++) {
x=getint(); y=getint(); z=getint();
ans+=z; d[x]++; d[y]++;
if(w[x][y]>z) w[x][y]=z,w[y][x]=z;
}
for(int k=;k<=n;k++) for(int i=;i<=n;i++) if(i!=k) for(int j=;j<=n;j++) if(i!=j && j!=k) w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
for(int i=;i<=n;i++) if(d[i]!= && w[][i]==inf) { printf("-1"); return ; }
for(int i=;i<=n;i++) if(d[i]&) end|=(<<(i-));
for(int i=;i<=end;i++) f[i]=inf; f[end]=;
for(int i=end;i>;i--) {
if(f[i]==inf) continue;
for(int j=;j<=n;j++) {
if(((<<(j-))&i )==) continue;
for(int k=;k<=n;k++) {
if(( (<<(k-))&i )==) continue; now=i^(<<(k-))^(<<(j-));
f[now]=min(f[now],f[i]+w[j][k]);
}
}
}
ans+=f[]; printf("%d",ans);
} int main()
{
work();
return ;
}
codeforces 21D:Traveling Graph的更多相关文章
- CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)
题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数. 思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色. 这里用的并查集(还可以带权并查集优化一下,或 ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- 图:无向图(Graph)基本方法及Dijkstra算法的实现 [Python]
一般来讲,实现图的过程中需要有两个自定义的类进行支撑:顶点(Vertex)类,和图(Graph)类.按照这一架构,Vertex类至少需要包含名称(或者某个代号.数据)和邻接顶点两个参数,前者作为顶点的 ...
- codeforces 21D. Traveling Graph 状压dp
题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...
- codeforces 715B:Complete The Graph
Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...
- Codeforces 1009D:Relatively Prime Graph
D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces 459E Pashmak and Graph:dp + 贪心
题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
- Codeforces 755E:PolandBall and White-Red graph(构造+思维)
http://codeforces.com/contest/755/problem/E 题意:给出n个点和一个距离d,让你在这个n个点的图里面构造一个子图,使得这个子图的直径和补图的直径的较小值为d, ...
随机推荐
- C#中excel导入sql
using Microsoft.Office.Interop.Excel; public int ledinExcel(string file, object sender, EventArgs e) ...
- C#并行编程中的Parallel.Invoke
一.基础知识 并行编程:并行编程是指软件开发的代码,它能在同一时间执行多个计算任务,提高执行效率和性能一种编程方式,属于多线程编程范畴.所以我们在设计过程中一般会将很多任务划分成若干个互相独立子任务, ...
- React Native开发技术周报2
(1).资讯 1.React Native 0.22_rc版本发布 添加了热自动重载功能 (2).技术文章 1.用 React Native 设计的第一个 iOS 应用 我们想为用户设计一款移动端的应 ...
- svn: Checksum mismatch for 'C:\Documents and Settings\Admin\workspace\pics5\src\baolintest\.svn\text-base\test1.java.svn-base'; expected: '034cc715af5
出现这个问题解决如下: 比如问题文件为\workspace\pics5\src\baolintest\test.java文件,则 1.把在工程根\workspace\pics5\src\baolint ...
- Theano2.1.15-基础知识之theano如何处理shapre信息
来自:http://deeplearning.net/software/theano/tutorial/shape_info.html How Shape Information is Handled ...
- TextBox自定义控件
首先来一发图: 今天主要说的textBox内部给予提示: 使用自定义控件方式:TextBoxTip继承TextBox 利用TextBox的背景画刷功能 VisualBrush是一种比较特殊的笔刷,它的 ...
- LiveSDK初始化/登录时失败的解决办法
环境描述 Windows 8.1+VS 2013 Update3+Live SDK 5.6 Metro风格的程序,集成LIVE认证 问题描述 如下图,提示Null Reference的异常. 解决办法 ...
- HTTP请求头参数
Accept-Language: zh-cn,zh;q=0.5 意思:浏览器支持的语言分别是中文和简体中文,优先支持简体中文. 详解: Accept-Language表示浏览器所支持的语言类型: ...
- git查看提交历史
git日志的查看 在使用 Git 提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用 git log 命令查看. 查看日志信息: $ git log 可以用 --oneline ...
- Oracle Temp表空间切换
来源于: http://www.2cto.com/database/201507/418564.html 一.TEMP表空间作用 临时表空间主要用途是在数据库进行排序运算.管理索引.访问视图等操作时 ...