POJ 3553 Task schedule【拓扑排序 + 优先队列 / 贪心】
Task schedule
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 515 Accepted: 309 Special Judge
Description
There are n preemptive jobs to be processed on a single machine. Each job j has a processing time pj and deadline dj. Preemptive constrains are specified by oriented graph without cycles. Arc (i,j) in this graph means that job i has 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 two 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 two 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
2
4 1
4 0
1
1 2
Sample Output
1
2
Source
Northeastern Europe 2003, Western Subregion
【代码】:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
using namespace std;
const int maxn = 1e6+5;
const int mod = 142857;
int n,m,num,u,v;
int p[maxn],d[maxn];
priority_queue<P> q;
vector<int> G[maxn];
int inDeg[maxn];
void topSort()
{
int ok=0;
while(!q.empty()) q.pop();
for(int i=1; i<=n; i++) if(!inDeg[i]) q.push(P(d[i],i));
while(!q.empty())
{
int now = q.top().second; q.pop();
printf("%d\n",now);
for(int i=0;i<G[now].size();i++)
{
int nxt = G[now][i];
if(--inDeg[nxt] == 0)
{
q.push(P(d[nxt],nxt));
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",p+i,d+i);
scanf("%d",&m);
memset(inDeg,0,sizeof(inDeg));
for(int i=1;i<=n;i++) G[i].clear();
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
inDeg[v]++;
}
topSort();
}
POJ 3553 Task schedule【拓扑排序 + 优先队列 / 贪心】的更多相关文章
- POJ 3553 Task schedule
原题链接:http://poj.org/problem?id=3553 这道题主要就是贪心思想吧,对于每个job,根据其截止时间 dj 从小到大排序,我们必须要尽快把dj最小的job完成掉,这样才能使 ...
- HDU 4857 拓扑排序 优先队列
n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...
- 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU-4857-逃生-反向拓扑排序+优先队列
HDU-4857 题意就是做一个符合条件的排序,用到拓扑序列. 我一开始wa了多发,才发现有几个样例过不了,发现1->2->3...的顺序无法保证. 后来就想用并查集强连,还是wa: 后来 ...
- POJ 3687 Labeling Balls【拓扑排序 优先队列】
题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- HDU 4857 (反向拓扑排序 + 优先队列)
题意:有N个人,M个优先级a,b表示a优先于b.而且每一个人有个编号的优先级.输出顺序. 思路来自:与PKU3687一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- c# 以多个字符串分隔字符串数据 分组 分隔 split 正则分组
string str="aaa[##]ccc[##]ddd[##]bb" Regex regex=new Regex("[##]");//以 [##] 分割 s ...
- SPOJ DQUERY (主席树求区间不同数个数)
题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
- C++——OOP面向对象理解
从Rob Pike 的 Google+上的一个推看到了一篇叫<Understanding Object Oriented Programming>的文章,我先把这篇文章简述一下,然后再说说 ...
- Divljak
Divljak Alice 有 $n$ 个字符串 $ S_1,S_2,\cdots,S_n $ ,Bob有一个字符串集合 $T$ ,一开始集合是空的. 接下来会发生 $q$ 个操作,操作有两种形式: ...
- 适用于iview的表格转Excel插件
在网上找的一个表格转excel插件,经过修改后使其适用于iview中的table组件 let idTmr; const getExplorer = () => { let explorer = ...
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- 怎么给word加底纹
- NetTime
NetTime NetTime is a Simple Network Time Protocol (SNTP) client for Windows 95/98/Me/NT/2000/XP/Vist ...
- js 扒一扒这些题目都考了哪些知识点
1.空数组是true还是false 题目: <script> var tmp = 'a'; if([]==false) tmp+='b'; if(![]) tmp +='c'; alert ...
- Spring学习--HelloWorld
Spring: Spring 是一个开源框架. Spring 是为简化企业级应用开发而生,使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. Spring 是一 ...