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.

Example

Input
3 3
1 2 1
2 3 1
3 1 1
Output
3
Input
3 2
1 2 3
2 3 4
Output
14

题意是给一个无向图,要求从1出发回到1,但是必须经过图中每一条边,只上一次

这从不从1出发有什么用呢?这就是个欧拉回路。

然后判一下每个点的度数是不是奇数,如果是奇数就需要再找个奇数度数的点匹配。

最后,匹配直接搜索就行。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int v[];
int fa[];
int p[],cnt;
bool mrk[];
int dis[][];
LL ans,ans2;
struct edg{int x,y,z;}e[];
inline int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
inline void dfs(int now,LL tot)
{
if (now==cnt/+){ans2=min(ans2,tot);return;}
int x,y;
for (x=;x<=cnt;x++)if (!mrk[x])break;
mrk[x]=;
for (y=x+;y<=cnt;y++)if (!mrk[y]&&dis[p[x]][p[y]]<=)
{
mrk[y]=;
dfs(now+,tot+dis[p[x]][p[y]]);
mrk[y]=;
}
mrk[x]=;
}
int main()
{
n=read();m=read();
for (int i=;i<=n;i++)fa[i]=i;
for (int i=;i<n;i++)
for (int j=i+;j<=n;j++)
dis[i][j]=dis[j][i]=<<;
for (int i=;i<=m;i++)
{
e[i].x=read();
e[i].y=read();
e[i].z=read();v[e[i].x]++;v[e[i].y]++;
if (e[i].z<dis[e[i].x][e[i].y])dis[e[i].x][e[i].y]=dis[e[i].y][e[i].x]=e[i].z;
ans+=e[i].z;
int xx=getfa(e[i].x),yy=getfa(e[i].y);
if (xx>yy)swap(xx,yy);
fa[xx]=yy;
}
for (int i=;i<=m;i++)
{
if (getfa(e[i].x)==getfa()&&getfa(e[i].y)==getfa())continue;
puts("-1");
return ;
}
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (dis[i][j]>dis[i][k]+dis[k][j])
dis[i][j]=dis[i][k]+dis[k][j];
for (int i=;i<=n;i++)if (v[i]&)p[++cnt]=i;
if (cnt&){puts("-1");return ;}
ans2=1ll<<;
dfs(,ans);
printf("%lld\n",ans2);
}

cf21D

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.

Example

Input
3 3
1 2 1
2 3 1
3 1 1
Output
3
Input
3 2
1 2 3
2 3 4
Output
14

cf21D Traveling Graph的更多相关文章

  1. codeforces 21D:Traveling Graph

    Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...

  2. codeforces 21D. Traveling Graph 状压dp

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

  3. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...

  4. Traveling

    Problem J: Traveling Time Limit: 1 Sec  Memory Limit: 32 MB Description SH likes traveling around th ...

  5. Speeding Up The Traveling Salesman Using Dynamic Programming

    Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...

  6. 图论介绍(Graph Theory)

    1 图论概述 1.1 发展历史 第一阶段: 1736:欧拉发表首篇关于图论的文章,研究了哥尼斯堡七桥问题,被称为图论之父 1750:提出了拓扑学的第一个定理,多面体欧拉公式:V-E+F=2 第二阶段( ...

  7. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  8. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  9. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

随机推荐

  1. PHP 哈希表碰撞攻击

    理想情况下哈希表插入和查找操作的时间复杂度均为O(1),任何一个数据项可以在一个与哈希表长度无关的时间内计算出一个哈希值(key),然后在常量时间内定位到一个桶(术语bucket,表示哈希表中的一个位 ...

  2. Maven归纳

      一.常用功能 1.Maven的中央仓库 https://mvnrepository.com/ 2.添加jar包依赖 1.首先点击pom.xml,然后点击弹出页面中的Dependencies选项,接 ...

  3. ucosii(2.89)mutex 应用要点

    mutex 的创建在于共享资源打交道是可以可以保证满足互斥条件:1,必须保证继承优先级要高于可能与相应共享资源打交道的任务中优先级最高的优先级.2,不要将占有Mutex的任务挂起,也不要让占有mute ...

  4. 用函数创建对象、类创建对象,以及使用prototype的好处

    用函数创建对象 var CheckObject = function(){}; CheckObject.checkName = function(){ // 检验姓名 }; CheckObject.c ...

  5. 数据库-SQL语法:LEFT JOIN

    LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行.(补充:left join是一对多的关系,表里所有符合条件的记 ...

  6. Java中的线程--线程范围内共享数据

    接着学习Java中的线程,线程范围内的共享数据! 一.线程范围内的数据共享定义 对于相同的程序代码,多个模块在同一个线程中共享一份数据,而在另外线程中运行时又共享另外一份数据. 共享数据中存在的问题, ...

  7. viewDidLoad、viewWillAppear、viewWillDisappear

    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil viewDidLo ...

  8. React框架搭建单页面应用package.json基本包和依赖包

    { //依赖包 "devDependencies": { //babel "babel-core": "6.24.1", "bab ...

  9. Fortran学习笔记5(数组Array)

    数组的声明方式 一维数组 二维数组 多维数组 数组索引值的改变 自定义类型的数组定义 对数组内容的设置 利用隐含式循环设置数组初值 对整个数组操作 对部分数组的操作 where函数 Forall函数 ...

  10. 长链剖分优化dp三例题

    首先,重链剖分我们有所认识,在dsu on tree和数据结构维护链时我们都用过他的性质. 在这里,我们要介绍一种新的剖分方式,我们求出这个点到子树中的最长链长,这个链长最终从哪个儿子更新而来,那个儿 ...