USACO SECTION 3.3 Riding the Fences

本文介绍了一种通过编程解决农场大规模围栏年度维修路径规划的问题。利用Euler回路算法,确保每段围栏仅被巡检一次,同时提供一条有效的路径方案。文章详细展示了如何构建图论模型并实现算法。
Riding the Fences

Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts.

Farmer John is as lazy as the next farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.

Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection. It is always possible to ride from any fence to any other fence (i.e., all fences are "connected").

Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.

There will always be at least one solution for each set of input data supplied to your program for testing.

PROGRAM NAME: fence

INPUT FORMAT

Line 1:The number of fences, F (1 <= F <= 1024)
Line 2..F+1:A pair of integers (1 <= i,j <= 500) that tell which pair of intersections this fence connects.

SAMPLE INPUT (file fence.in)

9
1 2
2 3
3 4
4 2
4 5
2 5
5 6
5 7
4 6

OUTPUT FORMAT

The output consists of F+1 lines, each containing a single integer. Print the number of the starting intersection on the first line, the next intersection's number on the next line, and so on, until the final intersection on the last line. There might be many possible answers to any given input set, but only one is ordered correctly.

SAMPLE OUTPUT (file fence.out)

1
2
3
4
2
5
4
6
5
7

 

/*
ID: conicoc1
LANG: C
TASK: fence
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

FILE *fin,*fout;
int FenceNumber;
int Link[501][1025];
int MinFence=500;
int Ans[1501],flag=1500;

void Euler(int Vertex)
{
	int i,j,temp;
	for(i=1;i<=Link[Vertex][0];i++)
		if(Link[Vertex][i]!=0){
			temp=Link[Vertex][i];
			Link[Vertex][i]=0;
			for(j=1;j<=Link[temp][0];j++)
				if(Link[temp][j]==Vertex){
					Link[temp][j]=0;
					break;
				}
			Euler(temp);
		}
	Ans[flag--]=Vertex;
}

int comp(const void *a,const void *b)
{
	return *(int*)a-*(int *)b;
}

int main()
{
	fin=fopen("fence.in","r");
	fout=fopen("fence.out","w");
	memset(Link,0,sizeof(Link));
	
	int i,V,W;
	fscanf(fin,"%d",&FenceNumber);
	for(i=0;i<FenceNumber;i++){
		fscanf(fin,"%d %d",&V,&W);
		Link[V][++Link[V][0]]=W;
		Link[W][++Link[W][0]]=V;
	}
	
	for(i=1;i<=500;i++)
		qsort(Link[i]+1,Link[i][0],sizeof(int),comp);
		
	for(i=500;i>=1;i--){
		if(Link[i][0]!=0){
			if(Link[i][0]%2!=0||Link[MinFence][0]%2==0)
				MinFence=i;
		}
	}
	
	Euler(MinFence);
	for(i=flag+1;i<=1500;i++)
		fprintf(fout,"%d\n",Ans[i]);
	return 0;
}


 

居然那么喜欢用邻接表。。。然后用着用着发现删边的时候好麻烦啊。。。还要遍历一遍,

	for(i=0;i<n;i++){
		scanf("%d%d",&j,&k);
		map[j][0]++;
		map[k][0]++;
		map[j][k]++;
		map[k][j]++;
	}

其实这样子更方便啊。。。我那样还要一个排序,不然没有办法从大到小输出,就是空间节省了

### Feeding the Cows B 问题解析 在《USACO 2022年12月比赛》的 Silver 组第二题中,题目要求解决一个关于奶牛喂养的问题。具体描述如下: 输入包括一个长度为 $ n $ 的字符串,表示一排奶牛,其中每个字符为 &#39;C&#39; 或 &#39;W&#39;,分别表示该位置有一头奶牛或是一片草地。目标是通过最少的操作次数,使得每头奶牛(&#39;C&#39;)都能在其左侧或右侧至少有一个相邻的草地(&#39;W&#39;),以便能够被喂养。每次操作可以将一个 &#39;C&#39; 变成 &#39;W&#39; 或者将一个 &#39;W&#39; 变成 &#39;C&#39;。 输出为最小的操作次数,若无法满足条件则输出 -1。 #### 问题分析 1. **问题条件**: - 每个奶牛必须在其左右至少有一个相邻的草地。 - 每次操作可以修改一个字符(&#39;C&#39; <-> &#39;W&#39;)。 - 需要找出最小操作次数。 2. **贪心策略**: - 从左到右遍历字符串,当遇到一个奶牛(&#39;C&#39;)时,检查其右侧是否有一个草地(&#39;W&#39;),如果存在,将该草地变为奶牛的“喂养点”。 - 如果右侧没有草地,则需要修改当前奶牛或其右侧的某个字符以满足条件。 3. **实现逻辑**: - 遍历字符串,维护一个指针,标记当前可以使用的草地位置。 - 如果当前字符是 &#39;C&#39;,且其右侧没有可用草地,则需要修改一个字符。 - 记录每次操作,并确保最终所有奶牛都能被喂养。 #### 示例代码实现 ```cpp #include <iostream> #include <string> using namespace std; int main() { int n; string s; cin >> n >> s; int operations = 0; int lastGrass = -1; for (int i = 0; i < n; ++i) { if (s[i] == &#39;C&#39;) { if (lastGrass == -1 || lastGrass < i - 1) { // Check if there is a grass to the right bool found = false; for (int j = i + 1; j < n; ++j) { if (s[j] == &#39;W&#39;) { lastGrass = j; found = true; break; } } if (!found) { cout << -1 << endl; return 0; } } lastGrass = i + 1; // Mark the next available grass position continue; } else if (s[i] == &#39;W&#39;) { lastGrass = i; } } cout << operations << endl; return 0; } ``` #### 时间复杂度 - 该算法的时间复杂度为 $ O(n) $,因为每个字符最多被访问两次(一次遍历,一次查找草地)。 #### 空间复杂度 - 空间复杂度为 $ O(1) $,仅使用了常量级的额外空间。 #### 注意事项 - 需要处理边界情况,例如字符串末尾没有草地。 - 如果无法满足条件(即存在无法喂养的奶牛),应输出 -1。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值