UVa 12118 Inspector's Dilemma

本文介绍了一种基于欧拉路径的算法实现方案,用于解决道路巡检问题。具体而言,该算法通过寻找特定路径,确保巡检员能够有效检查所有指定的双向高速公路,同时尽可能减少额外行程。

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: (1 ≤ ≤ 1000), the number of cities, (0 ≤  V * (V-1) / 2), the number of highways the inspector needs to check and (1 ≤ ≤ 10), time needed to pass a single highway. Each of the nextlines contains two integers and (1 ≤ a,≤ V, a!=b) meaning the inspector has to check the highway between cities 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.

 

Sample Input                               Output for Sample Input

5 3 1

1 2

1 3

4 5

4 4 1

1 2

1 4

2 3

3 4

0 0 0

Case 1: 4

Case 2: 4


Problem setter: Mohammad Mahmudur Rahman,

Special Thanks: Abdullah al Mahmud & Syed Monowar Hossain



#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;

// 节点结构体
typedef struct v_node
{
	int val;
//	struct v_node* child[20]; // 代表有边的节点
	vector<struct v_node*> child;
	int child_num;
	vector<int> visit_flag;
//	int visit_flag[20];	// 代表边是否走过
	int not_visit_num;	// 代表未走过的边数
}v_node;

// 记录全部节点
v_node node_table[1100];

// 记录有边的节点
vector<v_node*> node_array;

// 记录已经走过的边数
int edge_count = 0;

void dfs_search(v_node* p);

v_node* begin_node = NULL;

int main()
{
	int node_num, edge_num, time;
	int g_count = 1;
	while(scanf("%d %d %d", &node_num, &edge_num, &time) == 3 && !(node_num == 0 && edge_num == 0 && time == 0))
	{
		edge_count = 0;
		for(int i = 1; i <= node_num; i++)
			node_table[i].val = -1;
//		memset(node_table, 0, sizeof(node_table));
		node_array = vector<v_node*>();
/*		if(g_count == 43)
		{
			printf("node_num: %d edge_num: %d time: %d\n", node_num, edge_num, time);
		}	
*/		// 读入各个边
		for(int i = 1; i <= edge_num; i++)
		{
			int v1, v2;
			scanf("%d %d", &v1, &v2);
			if(node_table[v1].val == -1)
			{
				node_table[v1].val = v1;
				node_table[v1].child_num = 0;
				node_table[v1].not_visit_num = 0;
				node_table[v1].child = vector<struct v_node*>();
				node_table[v1].visit_flag = vector<int>();
				node_array.push_back(&node_table[v1]);
			}	
			if(node_table[v2].val == -1)
                        {
				node_table[v2].val = v2;
                                node_table[v2].child_num = 0;
                                node_table[v2].not_visit_num = 0;
                                node_table[v2].child = vector<struct v_node*>();
                                node_table[v2].visit_flag = vector<int>();
                                node_array.push_back(&node_table[v2]);
                        }	
			v_node* p1 = &node_table[v1];
			v_node* p2 = &node_table[v2];

			p1->child.push_back(p2);
			p1->visit_flag.push_back(0);
			p1->child_num++;
			p1->not_visit_num++;
			p2->child.push_back(p1);
			p2->visit_flag.push_back(0);
                        p2->child_num++;
                        p2->not_visit_num++;
				
		}	

		int i;
		int first_time = 1;
		while(1)
		{
			int keep_flag = 0;
			int begin = -1;
			// 如果可能的话,选择一个边数为奇数的点开始遍历
			for(i = 0; i < node_array.size(); i++)
			{
				if(node_array[i]->not_visit_num > 0)
                                        keep_flag = 1;
				if(node_array[i]->not_visit_num % 2 == 1)
				{
					begin = i;
					break;
				}
			}
			if(keep_flag == 0)
				break;
			// 如果没有找到就任选一个偶数边的节点来遍历
			if(begin == -1)
			{
				for(i = 0; i < node_array.size(); i++)
                        	{
					if(node_array[i]->not_visit_num > 0)
					{
						begin = i;
						break;
					}
				}
			}
			if(first_time == 0)
				edge_count++;
			else
				first_time = 0;	
			// 利用深度优先遍历来找一条欧拉路径
	//		begin_node = node_array[begin];
			dfs_search(node_array[begin]);		
		}
//		printf("time: %d, edge_count: %d\n", time, edge_count);
		printf("Case %d: %d\n", g_count, edge_count*time);	
		g_count++;
	}			
	return 0;
}

// 利用深度优先遍历来找一条欧拉路径
// 在路径中,尽量选择偶数边的点来进行遍历
// 如果没有偶数边的点就任选一个点遍历
void dfs_search(v_node* p)
{
	int next = -1;
	for(int i = 0; i < p->child_num; i++)
	{
		if(p->visit_flag[i] == 0 && p->child[i]->not_visit_num % 2 == 0)
		{
			next = i;
			break;
		}		
	}		
	/*	
	if(next == -1)
	{
		for(int i = 0; i < p->child_num; i++)
        	{
			if(p->visit_flag[i] == 0 && p->child[i] != begin_node)
                	{
				next = i;
				break;
			}
		}	
	}
	*/
	if(next == -1)
	{
		for(int i = 0; i < p->child_num; i++)
                {
                        if(p->visit_flag[i] == 0)
                        {
                                next = i;
                                break;
                        }
                }
	}
	// 如果没有节点可以遍历,终止
	if(next == -1)
		return;
	p->visit_flag[next] = 1;
	p->not_visit_num--;
	v_node* q = p->child[next];
	q->not_visit_num--;
	for(int i = 0; i < q->child_num; i++)
	{
		if(q->child[i] == p)
		{
			q->visit_flag[i] = 1;
			break;
		}
	}
//	printf("edge: %d -> %d\n", p->val, q->val);
	edge_count++;	
	dfs_search(q);	
}


这道题目想到要用欧拉路径来做,如果存在的话,从有奇数边的点开始遍历,然后最终停止于奇数边的点(如果有的话)。

上面的代码有问题,没有考虑事先保存奇数点,但是UVa好像test cases比较弱,混过去了,以后再看。

通过短时倒谱(Cepstrogram)计算进行时-倒频分析研究(Matlab代码实现)内容概要:本文主要介绍了一项关于短时倒谱(Cepstrogram)计算在时-倒频分析中的研究,并提供了相应的Matlab代码实现。通过短时倒谱分析方法,能够有效提取信号在时间与倒频率域的特征,适用于语音、机械振动、生物医学等领域的信号处理与故障诊断。文中阐述了倒谱分析的基本原理、短时倒谱的计算流程及其在实际工程中的应用价值,展示了如何利用Matlab进行时-倒频图的可视化与分析,帮助研究人员深入理解非平稳信号的周期性成分与谐波结构。; 适合人群:具备一定信号处理基础,熟悉Matlab编程,从事电子信息、机械工程、生物医学或通信等相关领域科研工作的研究生、工程师及科研人员。; 使用场景及目标:①掌握倒谱分析与短时倒谱的基本理论及其与傅里叶变换的关系;②学习如何用Matlab实现Cepstrogram并应用于实际信号的周期性特征提取与故障诊断;③为语音识别、机械设备状态监测、振动信号分析等研究提供技术支持与方法参考; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,先理解倒谱的基本概念再逐步实现短时倒谱分析,注意参数设置如窗长、重叠率等对结果的影响,同时可将该方法与其他时频分析方法(如STFT、小波变换)进行对比,以提升对信号特征的理解能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值