贪心算法☞会场安排问题/图着色
来源:互联网 发布:js继承面试题详解 编辑:程序博客网 时间:2024/05/24 00:14
------------------------------------------------------------Sun---------------------------------------------------------------------------
问题描述:在足够多的会场里安排一批活动,并用尽可能少的会场安排这些活动。
图着色:将每一个活动作为图的一个顶点,不相容活动间用边相连,使相邻顶点有不同颜色的最小着色数,相应于要找的最小会场数
输入活动数n,活动开始时间A[i].s 结束时间A[i].e 。
输入: 5
1 23
12 28
25 35
27 80
36 50
#include <stdio.h>#include <stdlib.h>#define SIZE 100/*安排n个活动,最少需要的会场数目*/struct point{ int s; int e;}A[SIZE];void sort(struct point A[],int n); //按结束时间排序int greedy(struct point A[],int n); //计算最小会场数int main(){ int n,i; printf("请输入活动个数:\n"); scanf("%d",&n); printf("请输入活动开始结束时间:\n"); for(i=0;i<n;i++) { scanf("%d",&A[i].s); scanf("%d",&A[i].e); } sort(A,n); printf("最少需要%d个会场喔",greedy(A,n)); return 0;}void sort(struct point A[],int n){ int i; int temp; for(i = 0;i<n;i++) { if(A[i].e > A[i+1].e) { temp = A[i].e; A[i].e = A[i+1].e; A[i+1].e = temp; temp = A[i].s; A[i].s = A[i+1].s; A[i+1].s = temp; } }}int greedy(struct point A[],int n){ int i,sum = 1,max = A[0].e; //首先第一个活动占用一个会场 for(i = 1;i <n ; i++) //从第二个活动开始 { if((A[i].s < A[i-1].e) || A[i].s > max) //若是后一个开始时间大于前一个结束时间 or 活动开始时间已经大于max 继续判断 { if(A[i].s < max) { sum++; //开始时间小于max,会场数+1 }else{ max = A[i].e; //否则修改max } }else{ sum++; } } return sum;}
0 0
- 贪心算法☞会场安排问题/图着色
- 【贪心算法】会场安排问题
- 会场安排问题 贪心算法
- 会场安排问题贪心算法
- 贪心算法-会场安排
- 会场安排问题(贪心算法)
- 贪心算法解决会场安排问题
- 会场安排问题(贪心算法)
- 贪心算法之nyoj会场安排问题
- NTOJ-14-会场安排问题(贪心算法)
- 会场安排问题(贪心算法)
- 贪心算法作业之会场安排问题
- 贪心算法——会场安排问题
- 会场安排问题之贪心算法
- 会场安排问题(贪心算法,Ruby)
- 贪心算法——会场安排问题
- 贪心算法实现会场安排问题
- 贪心--会场安排问题
- Gradle 笔记——Java构建入门
- (SDL)(01)SDL Introduction
- 在糖尿病患者信息管理系统中,交流区模块想模仿论坛那样几楼几楼的!
- C++各种数据类型之间的转换
- 开源CORBA OmniORB开发环境搭建及编译问题解决方法
- 贪心算法☞会场安排问题/图着色
- c++ map使用
- 将WEB PROJECT部署到tomcat
- csuoj-1729-齿轮传动
- 杭电2612
- Android JSONArray.remove ()报错
- Onethink 系统架构 开发规划图
- Noip2007普及组
- android Binder 学习