Greedy algorithm solves the problem of venue arrangement

Source: Internet
Author: User

Greedy algorithm solves the problem of venue arrangement.

"Problem description"

Suppose you want to arrange a number of events in enough venues and want to use as few venues as possible. (This problem is actually a well-known graph coloring issue.) If each activity is a vertex of the graph, the incompatible activities are connected by edges. Causes the adjacent vertices to have a minimum number of colors, corresponding to the minimum number of venues to find. )

"Data Entry"

The input data is given by the file Input.txt, and the first line is a positive integer k, indicating that there are k pending activities.

Next there are k rows of data, each with two positive integers representing the start and end times of the K-pending activity.


"End Output"

Output minimum number of venues.


Input.txt output.txt

5 3

1 23

12 28

25 35

27 80

36 50


Idea: Traverse all the activity time, if the time can be superimposed to the previous activity of the venue, then superimposed to the existing activities, if not superimposed, the new venue.




Code:

#include "iostream" #include "string" using namespace std;
	struct TT {int st;
int Ed;


};
	int main () {int n=0;
	FILE *FP;
	int num=1;
	if ((Fp=fopen ("Input.txt", "R")) ==null) {printf ("Can ' t find the input.txt\n");

	} fscanf (FP, "%d", &n);
	int *s_t;
    int *e_t;
	s_t = (int *) malloc (sizeof (int) *n+1);
    e_t = (int *) malloc (sizeof (int) *n+1);
	TT *t_c;

	T_c = (TT *) malloc (sizeof (TT) *n+1);
	int i=0;
	for (i=0;i<n;i++) {fscanf (FP, "%d%d", &s_t[i],&e_t[i]);
	} for (i=0;i<n;i++) {printf ("%3d", S_t[i]);
	} cout<<endl;
	for (i=0;i<n;i++) {printf ("%3d", E_t[i]);




	} cout<<endl;
	int star =1000,end=0;
	int S_key,e_key;
			for (i=0;i<n;i++) {if (S_t[i]<=star) {s_key = i;
		Star = S_t[i];
			} if (e_t[i]>=end) {e_key = i;
		end = E_t[i];
	}} int count=0;
	T_c[0].ed = E_t[s_key];

	Count + +;
		for (i=0;i<n;i++) {int KK = s_t[i];
		if (I==s_key) {continue; } for (int j=0;j<count;j++{if (T_C[J].ED&LT;KK) {T_c[j].ed = E_t[i];
			Break
				} else {T_c[count].ed = e_t[i];
				count++;
			Break







    }}} printf ("%3d\n", count);
return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.