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]++;
}
其实这样子更方便啊。。。我那样还要一个排序,不然没有办法从大到小输出,就是空间节省了
本文介绍了一种通过编程解决农场大规模围栏年度维修路径规划的问题。利用Euler回路算法,确保每段围栏仅被巡检一次,同时提供一条有效的路径方案。文章详细展示了如何构建图论模型并实现算法。
258

被折叠的 条评论
为什么被折叠?



