cf21D Traveling Graph
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
3 3
1 2 1
2 3 1
3 1 1
3
3 2
1 2 3
2 3 4
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
3 3
1 2 1
2 3 1
3 1 1
3
3 2
1 2 3
2 3 4
14
cf21D Traveling Graph的更多相关文章
- codeforces 21D:Traveling Graph
Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...
- codeforces 21D. Traveling Graph 状压dp
题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...
- UESTC 1852 Traveling Cellsperson
找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...
- Traveling
Problem J: Traveling Time Limit: 1 Sec Memory Limit: 32 MB Description SH likes traveling around th ...
- Speeding Up The Traveling Salesman Using Dynamic Programming
Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...
- 图论介绍(Graph Theory)
1 图论概述 1.1 发展历史 第一阶段: 1736:欧拉发表首篇关于图论的文章,研究了哥尼斯堡七桥问题,被称为图论之父 1750:提出了拓扑学的第一个定理,多面体欧拉公式:V-E+F=2 第二阶段( ...
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
随机推荐
- hadoop ssh 问题WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
0.0.0.0: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@0.0.0.0: @ WARNING: REMOTE HO ...
- Python学习日志9月13日
昨天的学习日志没有写,乱忙了一整天,政治电脑. 好奇心重,想要给电脑装上传说中LInux操作系统,各种小问题折腾到半夜,今天又折腾到晚上才真正的装上系统. 可是装上系统后又发现各种的不好用.虽然界面比 ...
- 使用python查询天气
python主代码 weather.py import urllib2 import json from city import city cityname = raw_input('你想查哪个城市的 ...
- C# Process.Start方法
System.Diagnostics.Process.Start(); 主要功能: 1.打开某个链接网址(弹窗). 2.定位打开某个文件目录. 3.打开系统特殊文件夹,如“控制面板”等. Proces ...
- python自动化基础问题解析
(1)自动化代码中用到的设计模式: po模式(page object): 1.PO提供了一种业务流程与页面元素操作分离的模式,这使得测试代码变得更加清晰. 2.页面对象与用例分离,使得我们更好的复 ...
- 计算机图形学(Conputer Graphics):非均匀有理B样条
计算机图形学(Conputer Graphics):非均匀有理B样条 非均匀有理B样条(Non-Uniform Rational B-Spline)英文缩写,NURBS. 它是贝塞尔曲线的一个推广,而 ...
- shell脚本,如何监控目录下的文件内容是否被修改。
第一种方法是通过cmp来进行比对[root@localhost bo]# ls .html .html .html .html .html .html .html .html .html cat.sh ...
- sass --watch 失败bug
NameError: uninitialized constant Sass::Plugin::Compiler::SassListen 网上说法是sass v3.2.10有bug 但是我版本3.5. ...
- AHB2reg接口转换
assign mcu_xxx_addr = (rd_after_wr_reg || reg_valid_write_trans) ? haddr_reg[ADDR_WIDTH+:] : haddr[A ...
- leepcode作业解析 - 5-19
18.两数之和II -输入有序数组 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 ...