骑马修栅栏(fence) 、铲雪车(snow)

问题 K: 骑马修栅栏(fence)

时间限制: 1 Sec  内存限制: 128 MB
 

题目描述

农民John每年有很多栅栏要修理。他总是骑着马穿过每一个栅栏并修复它破损的地方。

John是一个与其他农民一样懒的人。他讨厌骑马,因此从来不两次经过一个一个栅栏。你必须编一个程序,读入栅栏网络的描述,并计算出一条修栅栏的路径,使每个栅栏都恰好被经过一次。John能从任何一个顶点(即两个栅栏的交点)开始骑马,在任意一个顶点结束。

每一个栅栏连接两个顶点,顶点用1到500标号(虽然有的农场并没有500个顶点)。一个顶点上可连接任意多(≥1)个栅栏。所有栅栏都是连通的(也就是你可以从任意一个栅栏到达另外的所有栅栏)。

你的程序必须输出骑马的路径(用路上依次经过的顶点号码表示)。我们如果把输出的路径看成是一个500进制的数,那么当存在多组解的情况下,输出500进制表示法中最小的一个 (也就是输出第一个数较小的,如果还有多组解,输出第二个数较小的,等等)。 输入数据保证至少有一个解。

输入

第1行:一个整数F(1≤F≤1024),表示栅栏的数目;

第2到F+1行:每行两个整数i,j(1≤=i,j≤500)表示这条栅栏连接i与j号顶点。

输出

输出应当有F+1行,每行一个整数,依次表示路径经过的顶点号。注意数据可能有多组解,但是只有上面题目要求的那一组解是认为正确的。

样例输入

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

样例输出

1
2
3
4
2
5
4
6
5
7

徐不可说:简化题意就是一个一笔画问题,即欧拉回路,注意要注意此题的路径为无向图。

#include<iostream>
#include<cstdio>
#include<algorithm>
 
using namespace std;
const int MN = 512;
struct edge {
    int u, v;
}k;
int m, n;
int t[MN][MN], way[MN], d[MN], cnt = 0;
 
void find(int k) {
    for (int i = 1; i <= n; i++) {
        if (t[k][i]) {
            t[k][i]--;
            t[i][k]--;
            find(i);
        }
    }
    way[++cnt] = k;
}
int main() {
    cin >> m;
    int u, v;
    for (int i = 0; i != m; i++) {
        cin >> u >> v;
        t[u][v]++;
        t[v][u]++;
        d[u]++;
        d[v]++;
        n = max(n, max(u, v));
    }
     
    int k1 = 600, k2 = 600;
    for (int i = 1; i <= n; i++) {
        if (d[i] & 1)k1 = min(k1, i);
        else if (d[i])k2 = min(k2, i);
    }
    if (k1 != 600)find(k1);
    else find(k2);
    for (int i = cnt; i >= 1; i--) {
        cout << way[i] << endl;
    }
    return 0;
}

下面是与之相似的一道题——铲雪车。同样可以用欧拉回路解决。

铲雪车

Problem Description

随着白天越来越短夜晚越来越长,我们不得不考虑铲雪问题了。整个城市所有的道路都是双车道,因为城市预算的削减,整个城市只有1辆铲雪车。铲雪车只能把它开过的地方(车道)的雪铲干净,无论哪儿有雪,铲雪车都得从停放的地方出发,游历整个城市的街道。现在的问题是:最少要花多少时间去铲掉所有道路上的雪呢?

Input

输入有多组数据,每组数据的第1行表示铲雪车的停放坐标(x,y),x,y为整数,单位为米。第2行为一个数n(n<=4000)表示下面有n行,每行给出了一条街道的起点坐标和终点坐标,所有街道都是笔直的,且都是双向一个车道。铲雪车可以在任意交叉口或任何街道的末尾任意转向,包括转U型弯。铲雪车铲雪时前进速度为20千米/时,不铲雪时前进速度为50千米/时。保证:铲雪车从起点一定可以到达任何街道。

Output

对于每组数据输出铲掉所有街道上的雪并且返回出发点的最短时间,精确到分钟。

Sample Input

0 0
3
0 0 10000 10000
5000 -10000 5000 10000
5000 10000 10000 10000

Sample Output

3:55
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double fun(int a,int b,int c,int d)
{
        long double x,y;
        x = abs(a - c);
        y = abs(b - d);
        return sqrt(x * x + y * y) / 10000;
}
int main()
{
//  freopen("a.txt","r",stdin);
    int x, y;
	while(cin >> x >> y)
	{
		int i, n, x1, y1, x2, y2;
		double sum = 0;
		cin >> n;
		for(i = 1; i <= n; i ++)
		{
			cin >> x1 >> y1 >> x2 >> y2;
			sum += fun(x1,y1,x2,y2);
		}
		int T = sum - 1;
		double t = sum - T;
		int te =floor(t * 60 + 0.5);
		if(te >= 60)
		{
			T ++;
			te -= 60;
		}
		cout << T << ":";
		if(te < 10)  cout << "0";
		cout << te << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现数据结构图论的骑马栅栏算法的代码: ```c #include <stdio.h> #include <stdlib.h> #include <limits.h> // 图的邻接表结构体 struct AdjListNode { int dest; // 目标节点编号 int weight; // 边的权重 struct AdjListNode* next; }; // 图的邻接表头结构体 struct AdjList { struct AdjListNode* head; }; // 图结构体 struct Graph { int V; // 节点数 struct AdjList* array; }; // 栅栏结构体 struct Fence { int id; // 栅栏编号 int height; // 栅栏高度 }; // 栅栏比较函数 int cmp(const void *a, const void *b) { const struct Fence *fa = a; const struct Fence *fb = b; return fa->height - fb->height; } // 创建邻接表节点 struct AdjListNode* newAdjListNode(int dest, int weight) { struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); newNode->dest = dest; newNode->weight = weight; newNode->next = NULL; return newNode; } // 创建图 struct Graph* createGraph(int V) { struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph)); graph->V = V; graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); for (int i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } // 添加边 void addEdge(struct Graph* graph, int src, int dest, int weight) { struct AdjListNode* newNode = newAdjListNode(dest, weight); newNode->next = graph->array[src].head; graph->array[src].head = newNode; } // 迪杰斯特拉算法 void dijkstra(struct Graph* graph, int start, int* dist) { int V = graph->V; int* visited = (int*) malloc(V * sizeof(int)); for (int i = 0; i < V; i++) { visited[i] = 0; dist[i] = INT_MAX; } dist[start] = 0; for (int count = 0; count < V - 1; count++) { int u = -1; for (int i = 0; i < V; i++) if (!visited[i] && (u == -1 || dist[i] < dist[u])) u = i; visited[u] = 1; struct AdjListNode* node = graph->array[u].head; while (node != NULL) { int v = node->dest; int weight = node->weight; if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) dist[v] = dist[u] + weight; node = node->next; } } free(visited); } // 骑马栅栏函数 void horseRepairFence(struct Fence *fences, int n, struct Graph* graph, int maxDiff) { int* dist = (int*) malloc(n * sizeof(int)); dijkstra(graph, 0, dist); int lastHeight = fences[0].height; // 上一个栅栏的高度 printf("栅栏:%d\n", fences[0].id); for (int i = 1; i < n; i++) { if (dist[i] == INT_MAX) { printf("无法建到 %d 号栅栏\n", fences[i].id); continue; } if (dist[i] - (fences[i].height - lastHeight) > maxDiff) { // 如果与上一个栅栏的高度差超过了最大值 printf("栅栏:%d\n", fences[i].id); lastHeight = fences[i].height; } } free(dist); } int main() { int n; // 栅栏数量 int maxDiff; // 最大高度差 printf("请输入栅栏数量和最大高度差:"); scanf("%d %d", &n, &maxDiff); // 读入栅栏信息 struct Fence *fences = malloc(n * sizeof(struct Fence)); for (int i = 0; i < n; i++) { fences[i].id = i + 1; scanf("%d", &fences[i].height); } // 根据栅栏高度排序 qsort(fences, n, sizeof(struct Fence), cmp); // 创建图 struct Graph* graph = createGraph(n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int weight = fences[j].id - fences[i].id; // 权重为栅栏之间的距离 addEdge(graph, i, j, weight); addEdge(graph, j, i, weight); } } // 骑马栅栏 horseRepairFence(fences, n, graph, maxDiff); free(fences); free(graph); return 0; } ``` 代码中使用了图的邻接表结构体来存储图的信息,通过`createGraph`函数创建了一个V个节点的图,并使用`addEdge`函数添加了所有的边。在`horseRepairFence`函数中,首先使用迪杰斯特拉算法求出所有节点到起点的最路径,然后根据栅栏高度和与上一个栅栏的距离以及最路径来判断是否栅栏。最后在`main`函数中读入栅栏信息并进行排序,创建图,然后调用`horseRepairFence`函数进行栅栏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值