Task Schedule
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 306 |
|
Accepted: 193 |
|
Special Judge |
Description
There is n preemptive jobs to being processed on a. Each job J has a processing time PJ and Deadline DJ. Preemptive constrains is specified by oriented graph without cycles. ARC (i,j) In this graph means that job I have to be processed before job J. A solution is specified by a sequence of the jobs. For any solution the completion time Cj is easily determined.
The objective is to find the optimal solution in order to minimize
max{Cj-DJ, 0}.
Input
The first line contains a single integer n, 1≤ n ≤50000. Each of the next n lines contains, integers pj and DJ, 0≤ pj ≤ 1000, 0≤ dj≤1000000, separated by one or more spaces. Line n+2 contains an integer m (number of arcs), 0≤ m ≤10*n. Each of the next m lines contains II integers i and J, 1≤ i, J ≤ N.
Output
Each of the n lines contains integer i (number of job in the optimal sequence).
Sample Input
24 14 011 2
Sample Output
12
Source
Northeastern Europe 2003, Western subregion test instructions looked for a long time to understand, finally finally understand. What you see on the Internet is +DFS with greedy algorithms. Test instructions: After completing all the tasks, ask how to complete all the tasks in a certain chronological order (each task has a complete processing time, only one machine, and the machine can only handle one task at a time, M pair (I j) table to complete the J task, then complete the I task), so that in all cases min{max{
Cj-
DJ, 0}} has the lowest value.
Problem Solving: We can think backwards, the total time to complete all tasks is determined, so long as to determine which task in the final processing (the last task has a feature: the degree of 0), tomax{
Cj-
DJ , 0} The smallest, then find the degree of 0
D
J The largest point, when a final output point is determined, you can delete the point and the relevant information in the diagram, then repeat the above step a certain point, and finally get a sequence. So we just have to reverse the map, then the point of the degree of 0 into a point of 0, with the priority queue processing, from the queue to take out the points, first with the array to deposit, and finally processed, reverse output sequence.
#include <stdio.h> #include <queue> #include <vector>using namespace std;const int N = 50005; struct node {int id,d; friend bool operator< (node A,node b) {return b.d>a.d; }};vector<int>mapt[n];int path[n],in[n],d[n];void Print (int N) {while (n--) {printf ("%d\n", Path[n]); }}void tope (int n) {int k=0; priority_queue<node>q; Node Pre,now; for (int i = 1, i <= n; i++) if (in[i]==0) {now.d = D[i]; now.id = i; Q.push (now); } while (!q.empty ()) {pre = Q.top (); Q.pop (); path[k++] = pre.id; int len = Mapt[pre.id].size (); for (int i = 0; i < len; i++) {now.id = Mapt[pre.id][i]; NOW.D = D[now.id]; in[now.id]--; if (in[now.id]==0) {Q.push (now); }}} print (k);} int main () {int n,m,a,b; while (scanf ("%d", &n) >0) {for (int i = 1; I <= n; i++) {scanf ("%d%d", &a,&d[i]); In[i] = 0; Mapt[i].clear (); } scanf ("%d", &m); while (m--) {scanf ("%d%d", &a,&b); Mapt[b].push_back (a); in[a]++; } tope (n); }}
POJ3553 Task Schedule (topology sort + priority queue) Classic