c语言算法之贪心算法之着色问题

引言:ZZU的朋友们请不要直接抄,我只是记录每次做题的成果,别查重完之后大家一起GG。

会场安排问题

题目来源:王晓东《算法设计与分析》

假设要在足够多的会场里安排一批活动,并希望使用尽可能少的会场。设计一个有效的 贪心算法进行安排。(这个问题实际上是著名的图着色问题。若将每一个活动作为图的一个 顶点,不相容活动间用边相连。使相邻顶点着有不同颜色的最小着色数,相应于要找的最小 会场数。)

输入格式:

第一行有 1 个正整数k,表示有 k个待安排的活动。 接下来的 k行中,每行有 2个正整数,分别表示 k个待安排的活动开始时间和结束时间。时间 以 0 点开始的分钟计。

输出格式:

输出最少会场数。

输入样例:

5
1 23
12 28
25 35
27 80
36 50 

输出样例:

在这里给出相应的输出。例如:

3

Code: 

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list> 
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
using namespace std;
#define ARR_SIZE 10001

#define Num 500005

int main(){
	int time;
	int k;
	cin>>k;
	int begin[k],end[k];
	int AllB[k],AllE[k];
	for(int i = 0; i < k; i++){
		cin>>begin[i]>>end[i];
		AllB[i] = AllE[i] = -1;
	}
	AllB[0] = begin[0];
	AllE[0] = end[0];
	int res = 1;
	for(int i = 0; i < k-1; i++){
		for(int j = k-1; j >= i; j--){
			if(begin[j] < begin[j-1]){
				int m = begin[j];
				begin[j] = begin[j-1];
				begin[j-1] = m;
				m = end[j];
				end[j] = end[j-1];
				end[j-1] = m;
			}
		}
	}
	for(int i = 1; i < k; i++){
		for(int j = 0; j < res;  j++){
			if( begin[i] < AllE[j]){
				if(j == res-1){
					AllB[res] = begin[i];
					AllE[res] = end[i];
					res++;
					break;
				}
				else continue;
			}
			else{
				AllE[j] = end[i]; 
				break;
			}
		}
	}
	cout<<res;
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用贪心算法解决图的m着色问题C语言代码: ```c #include <stdio.h> #include <stdbool.h> #define MAX_VERTEX_NUM 100 // 最大顶点数 int vertexColor[MAX_VERTEX_NUM]; // 存储每个顶点的颜色 int colorNum; // 颜色总数 // 判断当前顶点是否可以染成指定颜色 bool canColor(int graph[MAX_VERTEX_NUM][MAX_VERTEX_NUM], int vertex, int color, int vertexNum) { for (int i = 0; i < vertexNum; i++) { if (graph[vertex][i] && vertexColor[i] == color) { return false; } } return true; } // 对指定顶点进行染色 bool colorVertex(int graph[MAX_VERTEX_NUM][MAX_VERTEX_NUM], int vertex, int vertexNum) { for (int i = 1; i <= colorNum; i++) { if (canColor(graph, vertex, i, vertexNum)) { vertexColor[vertex] = i; return true; } } return false; } // 对整个图进行染色 void colorGraph(int graph[MAX_VERTEX_NUM][MAX_VERTEX_NUM], int vertexNum) { for (int i = 0; i < vertexNum; i++) { if (!colorVertex(graph, i, vertexNum)) { printf("Failed to color the graph.\n"); return; } } printf("The graph has been colored successfully.\n"); printf("The colors of vertices are:\n"); for (int i = 0; i < vertexNum; i++) { printf("Vertex %d: Color %d\n", i, vertexColor[i]); } } int main() { int graph[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储图的邻接矩阵 int vertexNum; // 顶点数 printf("Please input the number of vertices: "); scanf("%d", &vertexNum); printf("Please input the adjacency matrix of the graph:\n"); for (int i = 0; i < vertexNum; i++) { for (int j = 0; j < vertexNum; j++) { scanf("%d", &graph[i][j]); } } printf("Please input the number of colors: "); scanf("%d", &colorNum); colorGraph(graph, vertexNum); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值