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. visual studio中使用clrscr程序出错

    clrscr()函数的作用是“清屏”,即把标准输出设备中以前的显示记录清除,包含在头文件#include<conio.h>中,但暂时较旧的编译器中没有这个. 如果想要具有相同作用的函数,可 ...

  2. 《Windows核心编程系列》九谈谈同步设备IO与异步设备IO之同步设备IO

    同步设备IO 所谓同步IO是指线程在发起IO请求后会被挂起,IO完成后继续执行. 异步IO是指:线程发起IO请求后并不会挂起而是继续执行.IO完毕后会得到设备的通知.而IO完成端口就是实现这种通知的很 ...

  3. [POI2009]石子游戏Kam

    Description 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏 ...

  4. linux 前台后台程序切换命令总结

    1.在Linux终端运行命令的时候,在命令末尾加上 & 符号,就可以让程序在后台运行 root@Ubuntu$ ./tcpserv01 & 2.如果程序正在前台运行,可以使用 Ctrl ...

  5. 2017 JUST Programming Contest 3.0 D. Dice Game

    D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...

  6. 洛谷 P3804 【模板】后缀自动机

    来一份模板 #include<cstdio> #include<algorithm> #include<cstring> #include<queue> ...

  7. 贪心 Codeforces Round #236 (Div. 2) A. Nuts

    题目传送门 /* 贪心:每一次选取最多的线段,最大能放置nuts,直到放完为止,很贪婪! 题目读不懂多读几遍:) */ #include <cstdio> #include <alg ...

  8. Oracle对表空间无权限

    有的时候我们在Oracle数据库中对执行insert.update之类的语句时会出错,Oracle说我们对表空间无权限.执行下面的语句就可以修改用户对表空间的权限了. 执行语句: alter user ...

  9. AJPFX:递归与非递归之间的转化

    在常规表达式求值中: 输入为四则运算表达式,仅由数字.+.-.*./ .(.) 组成,没有空格,要求求其值. 我们知道有运算等级,从左至右,括号里面的先运算,其次是* ./,再是+.- : 这样我们就 ...

  10. (七)Mybatis总结之注解开发

    请移步到 https://www.cnblogs.com/lxnlxn/p/5996707.html