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).

Input

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

Output minimal cycle length or -1 if it doesn't exists.

Examples
Input
  1. 3 3
    1 2 1
    2 3 1
    3 1 1
Output
  1. 3
Input
  1. 3 2
    1 2 3
    2 3 4
Output
  1. 14
  2.  
  3. 正解:状压DP
    解题报告:
      考虑度数为偶数的点不需要被考虑,只需要考虑度数为奇数的情况。首先每条边必须要访问一次,所以所有边权加起来就是答案的初值。 
      然后度数为奇数的点就需要访问之前已经走过的边。我们考虑两个度数为奇数的点可以组合一下,变成度数为偶数的点,相当于是在这两个点之间新连了一条边,我们可以floyd预处理一下两点之间的最短路。然后状压,状态表示当前哪些结点的度数为奇数,然后枚举每次连哪两条边就可以了。
      
  1. //It is made by jump~
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <ctime>
  9. #include <vector>
  10. #include <queue>
  11. #include <map>
  12. #include <set>
  13. using namespace std;
  14. typedef long long LL;
  15. const int inf = (<<);
  16. const int MAXN = ;
  17. const int MAXS = (<<);
  18. int n,m,ans,end;
  19. int w[MAXN][MAXN];
  20. int d[MAXN],f[MAXS];
  21.  
  22. inline int getint()
  23. {
  24. int w=,q=; char c=getchar();
  25. while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
  26. while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
  27. }
  28.  
  29. inline void work(){
  30. n=getint(); m=getint(); int x,y,z,now; if(m==) { printf(""); return ; }
  31. for(int i=;i<=n;i++) for(int j=;j<=n;j++) w[i][j]=inf;
  32. for(int i=;i<=m;i++) {
  33. x=getint(); y=getint(); z=getint();
  34. ans+=z; d[x]++; d[y]++;
  35. if(w[x][y]>z) w[x][y]=z,w[y][x]=z;
  36. }
  37. 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]);
  38. for(int i=;i<=n;i++) if(d[i]!= && w[][i]==inf) { printf("-1"); return ; }
  39. for(int i=;i<=n;i++) if(d[i]&) end|=(<<(i-));
  40. for(int i=;i<=end;i++) f[i]=inf; f[end]=;
  41. for(int i=end;i>;i--) {
  42. if(f[i]==inf) continue;
  43. for(int j=;j<=n;j++) {
  44. if(((<<(j-))&i )==) continue;
  45. for(int k=;k<=n;k++) {
  46. if(( (<<(k-))&i )==) continue; now=i^(<<(k-))^(<<(j-));
  47. f[now]=min(f[now],f[i]+w[j][k]);
  48. }
  49. }
  50. }
  51. ans+=f[]; printf("%d",ans);
  52. }
  53.  
  54. int main()
  55. {
  56. work();
  57. return ;
  58. }
  1.  

codeforces 21D:Traveling Graph的更多相关文章

  1. CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)

    题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数. 思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色. 这里用的并查集(还可以带权并查集优化一下,或 ...

  2. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  3. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  4. 图:无向图(Graph)基本方法及Dijkstra算法的实现 [Python]

    一般来讲,实现图的过程中需要有两个自定义的类进行支撑:顶点(Vertex)类,和图(Graph)类.按照这一架构,Vertex类至少需要包含名称(或者某个代号.数据)和邻接顶点两个参数,前者作为顶点的 ...

  5. codeforces 21D. Traveling Graph 状压dp

    题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...

  6. 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 ...

  7. Codeforces 1009D:Relatively Prime Graph

    D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. Codeforces 459E Pashmak and Graph:dp + 贪心

    题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...

  9. Codeforces 755E:PolandBall and White-Red graph(构造+思维)

    http://codeforces.com/contest/755/problem/E 题意:给出n个点和一个距离d,让你在这个n个点的图里面构造一个子图,使得这个子图的直径和补图的直径的较小值为d, ...

随机推荐

  1. js/jquery判断浏览器的方法小结

    在网站前端开发中,浏览器兼容性是前端开发框架要解决的第一个问题,要解决兼容性问题就得首先准确判断出浏览器的类型及其版本,而判断浏览器的版本一般只能通过分析浏览器的userAgent才能知道.今天我们把 ...

  2. BZOJ 1251: 序列终结者

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 3773  Solved: 1579 [Submit][Status][Dis ...

  3. android studio 使用问题 解决方法

    1. Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.ap ...

  4. 反复请求某个URL缓存严重解决办法

    有2个iframe页面A和B 点击B页面某按钮刷新A,A缓存严重. 后来发现是因为反复请求同样的URL,浏览器就在调用缓存. 解决方法是在URL后添加一个当前时间即可 var url,e=/[?]/g ...

  5. Hadoop: MapReduce2的几个基本示例

    1) WordCount 这个就不多说了,满大街都是,网上有几篇对WordCount的详细分析 http://www.sxt.cn/u/235/blog/5809 http://www.cnblogs ...

  6. C#以post方式调用struts rest-plugin service的问题

    struts2: 玩转 rest-plugin一文中,学习了用struts2开发restful service的方法,发现用c#以post方式调用时各种报错,但java.ajax,包括firefox ...

  7. JQuery实现资讯上下滚动悬停效果

    第一步:使用repeater绑定一个table. <table width="530" id="rollBar"> <asp:Repeater ...

  8. Code First操作Mysql数据库

    前面博客也讲了,自己做一个网站,选用的是MVC+EF Code First+MySql+EasyUI,先说下技术选型.一.为什么选择MVC? 因为之前自己做的系统大部分是webForm,MVC的之前也 ...

  9. md5加密篇(一)

    /// <summary> /// 获取文件的md5摘要 /// </summary> /// <param name="sFile">文件流& ...

  10. [Offer收割]编程练习赛5-1 小Ho的防护盾

    #1357 : 小Ho的防护盾 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho的虚拟城市正在遭受小Hi的攻击,小Hi用来攻击小Ho城市的武器是一艘歼星舰,这艘歼星 ...