UVa 12118 Inspector's Dilemma

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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比较弱,混过去了,以后再看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值