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 ...
随机推荐
- Sqlserver 2012 Always on技术
使用了Sqlserver 2012 Always on技术后,假如采用的配置是默认配置,会出现Primary server CPU很高的情况发生,比如默认配置如下: 需要自定义来解决这个问题. 我们先 ...
- COGS 1144. [尼伯龙根之歌] 精灵魔法
★ 输入文件:alfheim.in 输出文件:alfheim.out 简单对比时间限制:1 s 内存限制:128 MB [题目背景] 『谜题在丛林中散发芳香绿叶上露珠跳跃着歌唱火焰在隐 ...
- mvc的model验证,ajaxhelper,验证机制语法
ajaxhelper: onsuccess是调用成功后显示方法,还有一个方法是调用前显示 model验证: 控件前端验证: 需要引入的JS 其中第二个是ajaxhelper的必须验证 后台的两个同名不 ...
- TCP的三次握手与四次挥手详解
TCP的三次握手与四次挥手是TCP创建连接和关闭连接的核心流程,我们就从一个TCP结构图开始探究中的奥秘 序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序 ...
- vue 获取汉字的全拼、简拼、首拼
1.封装公共方法,获取汉字的全拼.简拼.首拼 export const Pinyin = { _JMcode:{ "-":"", "—":& ...
- javascript 写一个随机范围整数的思路
const {random} = Math; //返回 [min,max] 的随机值 //[0,1) * (max - min + 1) => [0,max-min+1) //[0,max-mi ...
- mysql 备份解密脚本
#!/bin/bash #by sk 备份解码脚本 echo "-------------------------------------------------" functio ...
- Ubuntu 18.04修改默认源
安装Ubuntu 18.04后,使用国外源太慢了,修改为国内源会快很多. 修改阿里源为Ubuntu 18.04默认的源 备份/etc/apt/sources.list #备份 cp /etc/apt/ ...
- docker系列之基础命令-1
1.docker基础命令 docker images 显示镜像列表 docker ps 显示容器列表 docker run IMAGE_ID 指定镜像, 运行一个容器 docker start/sto ...
- vuex其实超简单,只需3步
前言 之前几个项目中,都多多少少碰到一些组件之间需要通信的地方,而因为种种原因,event bus 的成本反而比vuex还高, 所以技术选型上选用了 vuex, 但是不知道为什么,团队里的一些新人一听 ...