In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directional of course. A road-inspector’s task is to travel through the highways (in either direction) and to check if everything is in order. Now, a road-inspector has a list of highways he must inspect. However, it might not be possible for him to travel through all the highways on his list without using other highways. He needs a constant amount of time to traverse any single highway. As you can understand, the inspector is a busy fellow does not want to waste his precious time. He needs to know the minimum possible time to complete his task. He has the liberty to start from and end with any city he likes. Please help him out.

Input

The input file has several test cases. First line of each case has three integers: V (1 ≤ V ≤ 1000), the number of cities, E (0 ≤ E ≤ V ∗ (V − 1)/2), the number of highways the inspector needs to check and T (1 ≤ T ≤ 10), time needed to pass a single highway. Each of the next E lines contains two integers a and b (1 ≤ a, b ≤ V , a ̸= b) meaning the inspector has to check the highway between cities a and b. The input is terminated by a case with V = E = T = 0. This case should not be processed.

Output

For each test case, print the serial of output followed by the minimum possible time the inspector needs to inspect all the highways on his list. Look at the output for sample input for details.

这道题需要先理解一个概念:欧拉通路,要想达到题目说的那样每个边恰好只走一次,除了起点和终点外,其他点都不能是奇度点。

那么就这么做,首先每次都寻求一块的连通块,统计它们的奇数点个数,然后每个两个奇数点都至少需要一条边来使他变成偶数点,然后又因为起点和终点可以为奇数点,这种情况从中剪去。

DFS找奇数点+欧拉方法解决。

#include"iostream"
#include"cstring"
#include"vector"
using namespace std;
const int maxn=400000; vector<int>q[1010]; int cnt; int book[1010]; void DFS(int n)
{
if(book[n]!=0)
return;
book[n]=1;
cnt+=q[n].size()&1; for(int k=0;k<q[n].size();k++)
DFS(q[n][k]);
return;
} int main()
{
int v,e,c,flag,f=1,a,b;
while(cin>>v>>e>>c&&v)
{
memset(book,0,sizeof(book));
for(int k=0;k<1010;k++)
q[k].clear(); //每次都必须删除上次残余数据
for(int i=0;i<e;i++)
{
cin>>a>>b;
q[a].push_back(b);
q[b].push_back(a);
}
int ans=0;
cnt=0;
for(int j=1;j<=v;j++)
{
// cout<<book[j]<<' ';
if(book[j]!=1&&!q[j].empty())
{
cnt=0;
DFS(j);
ans+=max(cnt,2); //每次的数都要大于二,以保证能够形成哈密顿图
}
}
cout<<"Case "<<f++<<": "<<(max(ans/2-1,0)+e)*c<<endl;
}
return 0;
}

Inspector's Dilemma(欧拉通路)的更多相关文章

  1. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

  2. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

  3. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  4. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  5. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  7. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...

  8. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

  9. HDU 5883 F - The Best Path 欧拉通路 & 欧拉回路

    给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为 ...

  10. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

随机推荐

  1. PhpStorm Xdebug远程调试环境搭建原理分析及问题排查

    2017年05月26日  经验心得 目录   一. 环境介绍 二. 远程环境配置 2.2 Xdebug安装 2.3 配置 三. 本地phpstorm配置 3.1 下载远程代码 3.2 添加php解释器 ...

  2. 【小程序】基于.NET CORE2.1 的 微信开放平台 第三方平台开发 教程一 准备工作

    微信第三方平台概述 公众平台第三方平台是为了让公众号或小程序运营者,在面向垂直行业需求时,可以一键授权给第三方平台(并且可以同时授权给多家第三方),通过第三方平台来完成业务,开放给所有通过开发者资质认 ...

  3. 2、IO流的分类和IO流体系

  4. 员工管理系统(集合与IO流的结合使用 beta5.0 BufferedReader/ BufferedWriter)

    package cn.gee; public class Emp { private String id;//员工编号 一般是唯一的 private String sname; private int ...

  5. 转 SQL - 字符串中的转义字符

    一位同事在使用SQL处理一串字符时,出现一个意料之外的问题:这个字符串中包括字符‘&’.我们先看一下现象:     SQL> select * from v$version;     B ...

  6. solr 6.0 没有schema.xml未自动创建schema文件

    solr 6.0 没有schema.xml未自动创建schema文件 摘要:在之前的Solr版本中(Solr5之前),在创建core的时候,Solr会自动创建好schema.xml,但是在之后的版本中 ...

  7. 后缀数组 (Suffix Array) 学习笔记

    \(\\\) 定义 介绍一些写法和数组的含义,首先要知道 字典序 . \(len\):字符串长度 \(s\):字符串数组,我们的字符串存储在 \(s[0]...s[len-1]\) 中. \(suff ...

  8. javascript中函数的四种调用模式详解

    介绍函数四种调用模式前,我们先来了解一下函数和方法的概念,其实函数和方法本质是一样,就是称呼不一样而已.函数:如果一个函数与任何对象关系,就称该函数为函数.方法:如果一个函数作为一个对象属性存在,我们 ...

  9. redis 在windows 集群

    前言:为什么自己要花时间写一篇redis集群文章,网上众多的文章大都是思路正确,但是细节不足,这里写一篇文章记录自己部署时候遇到的问题,当下次再部署的时候避免跳入重复的坑. 上篇文章(http://w ...

  10. 掌握Spark机器学习库-07-线性回归算法概述

    1)简介 自变量,因变量,线性关系,相关系数,一元线性关系,多元线性关系(平面,超平面) 2)使用线性回归算法的前提 3)应用例子 沸点与气压 浮力与表面积