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 ...
随机推荐
- Vue v-if与v-show的区别
用了 viewjs 预览图片的时候 发现 用着两个 还是有区别的, 相同点==== v-if与v-show都可以动态控制dom元素显示隐藏 不同点 = ====v-if显示隐藏是将dom元素整个添加 ...
- 利用python进行数据分析2_数据采集与操作
txt_filename = './files/python_baidu.txt' # 打开文件 file_obj = open(txt_filename, 'r', encoding='utf-8' ...
- github更换仓库
1.找到.git目录 2.打开config文件 3.修改仓库地址 4.重新提交 git push --all origin 这样就替我们的项目换仓啦!!!^_^ 分类: git 参考资料: h ...
- js采用正则表达式获取地址栏参数
getQueryString:function(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*) ...
- Luogu P2397 yyy loves Maths VI (mode)
题目传送门 虽然只是一道黄题,但还是学到了一点新知识-- 摩尔投票法 用\(O(1)\)的内存,\(O(n)\)的时间来找出一串长度为n的数中的众数,前提是众数出现的次数要大于\(n/2\) 方法很简 ...
- 【贪心 思维题】[USACO13MAR]扑克牌型Poker Hands
看似区间数据结构的一道题 题目描述 Bessie and her friends are playing a unique version of poker involving a deck with ...
- [LUOGU] P1908 逆序对
题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为"逆序对"的 ...
- [POJ] 3362 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7978 Accepted: 2885 Descr ...
- 蓝牙学习(4) -- L2CAP
L2CAP in protocol 首先看一下L2CAP在Bluetooth protocol architecture diagram中的位置: Features of L2CAP Logical ...
- 王小胖之 Base64编码/解码
使用场景:编码网址作为URL参数,简单编码或加密数据,下载地址生成或解析. 实现功能:BASE64在线编码和解码. 数据实例:王小胖好啊,王小胖顶呱呱!! ~~ english 123 !@#$%^& ...