XMU 1040 Schedule 【拓扑排序】

1040: Schedule

Time Limit: 500 MS  Memory Limit: 64 MB
Submit: 12  Solved: 2
[Submit][Status][Web Board]

Description

  Resently, loneknight is doing research on job shop schedule problem(JSP for short). Let us take a look at JSP, there are n jobs and m machines, and every job must be processed in every machines, with a process time t[i,j] for job i being processed in machines j. One restrain is that the order for each job processed in machines is fixed, which means that for every job i, there is a process oder (a[i,1], a[i,2], ..., a[i,m]), job i must processed in machine a[i,1] first then a[i,2], ..., a[i,m]. Another restrain is every machine can process amost one job at any time, and every job can be process in amost one machine at any time. The problem is to find a schedule fit this restrains, that make the end time for all jobs, namely the makespan is minimum. Because of the fact that JSP is a NP-Complete problem, loneknight try using simulated anealing and gene algorithm to construct a heuristics algorithm for it. In developing such algorithm for JSP, he confront with a problem that if a schedule is already given, what is the makespan of this schedule, now this your task to solve this problem.

Input

  There are mutiple test cases in the input. The beginning of each case is n, the number of jobs, m, the number of machines. (0 < n,m <= 300) Each follow three components. First is a nxm matrix, the value in the ith row and jth column is t[i,j]. (0 <= t[i,j] < 100) Second is a nxm matrix, the jobs process order, the value in the ith row and jth column is a[i,j]. Third is a mxn matrix the machines process order, the value in the ith row and jth column is b[i,j], (b[i,1], b[i,2], ..., b[i,n]) is the jobs process order in machine i, which means machine i process b[i,1] first, then b[i,2], ..., b[i,n]. (jobs and machines are indexed from 1) The input end with EOF

Output

  For each test case, you should output a single integer, which is the makespan for that schedule in a single line.

Sample Input

3 3
83 86 77 
15 93 35 
86 92 49

3 1 2 
3 1 2 
1 3 2

1 2 3 
1 3 2 
1 2 3

Sample Output

495

HINT

Source

厦门大学第四届程序设计竞赛 现场决赛 by Loneknight @ btALT

[Submit][Status][Web Board]

题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1040

题目大意:

  有N个任务,M台机器,每个任务都必须在M台机器上运行一次才行。

  任务i在机器j上的运行时间为T[i][j]

  任务i必须满足先在机器A[i][1]上运行完才能在A[i][2]上,A[i][3]...A[i][m]上(按A[i]的顺序运行)

  机器j必须满足先运行任务B[j][1]才能再运行B[j][2],...,B[j][n](按B[j]顺序运行)

  问所有任务完成的时间。

题目思路:

  【拓扑排序】

  首先可以知道,如果一个任务在某一个机器上做需要之前的步骤都已经完成,每一个机器做当前任务也需要之前的任务均完成

  所以按照这个建图,按照第i个任务第j个机器设为节点A[i][j]。由于每个任务都有机器的先后顺序,每个机器也有任务的先后顺序

  所以A[i][j]往它的下一个任务,下一个机器连一条边。

  (一开始用SPFA写T了。。)  

  之后拓扑排序,每次更新最长路径的值。最后的答案即为解。

  d[xx][yy]=max{ d[x][y]+t[xx][yy] }

  

  1 /****************************************************
  2
  3     Author : Coolxxx
  4     Copyright 2017 by Coolxxx. All rights reserved.
  5     BLOG : http://blog.csdn.net/u010568270
  6
  7 ****************************************************/
  8 #include<bits/stdc++.h>
  9 #pragma comment(linker,"/STACK:1024000000,1024000000")
 10 #define abs(a) ((a)>0?(a):(-(a)))
 11 #define lowbit(a) (a&(-a))
 12 #define sqr(a) ((a)*(a))
 13 #define mem(a,b) memset(a,b,sizeof(a))
 14 const double EPS=1e-8;
 15 const int J=10000;
 16 const int MOD=100000007;
 17 const int MAX=0x7f7f7f7f;
 18 const double PI=3.14159265358979323;
 19 const int N=304;
 20 using namespace std;
 21 typedef long long LL;
 22 double anss;
 23 LL aans;
 24 int cas,cass;
 25 int n,m,lll,ans;
 26 int t[N][N],a[N][N],b[N][N],d[N][N],in[N][N];
 27 int nex[N][N][2][2];
 28 void tuopu()
 29 {
 30     int i,j,x,y,xx,yy;
 31     mem(d,0);
 32     queue<int>qx,qy;
 33     for(i=1;i<=n;i++)
 34     {
 35         if(!in[i][a[i][1]])
 36         {
 37             d[i][a[i][1]]=t[i][a[i][1]];
 38             qx.push(i);
 39             qy.push(a[i][1]);
 40         }
 41     }
 42     while(!qx.empty())
 43     {
 44         x=qx.front();qx.pop();
 45         y=qy.front();qy.pop();
 46         for(i=0;i<2;i++)
 47         {
 48             xx=nex[x][y][i][0];
 49             yy=nex[x][y][i][1];
 50             if(!x || !y)continue;
 51             d[xx][yy]=max(d[xx][yy],d[x][y]+t[xx][yy]);
 52             if(!--in[xx][yy])
 53             {
 54                 qx.push(xx);
 55                 qy.push(yy);
 56             }
 57         }
 58         ans=max(ans,d[x][y]);
 59     }
 60 }
 61 int main()
 62 {
 63     #ifndef ONLINE_JUDGE
 64     freopen("1.txt","r",stdin);
 65 //    freopen("2.txt","w",stdout);
 66     #endif
 67     int i,j,k,l;
 68     int x,y,z;
 69 //    for(scanf("%d",&cass);cass;cass--)
 70 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
 71 //    while(~scanf("%s",s))
 72     while(~scanf("%d",&n))
 73     {
 74         ans=0;
 75         mem(nex,0);mem(in,0);
 76         scanf("%d",&m);
 77         for(i=1;i<=n;i++)
 78             for(j=1;j<=m;j++)
 79                 scanf("%d",&t[i][j]);
 80         for(i=1;i<=n;i++)
 81             for(j=1;j<=m;j++)
 82                 scanf("%d",&a[i][j]);
 83         for(i=1;i<=m;i++)
 84             for(j=1;j<=n;j++)
 85                 scanf("%d",&b[i][j]);
 86         for(i=1;i<=n;i++)
 87         {
 88             for(j=1;j<m;j++)
 89             {
 90                 nex[i][a[i][j]][0][0]=i,
 91                 nex[i][a[i][j]][0][1]=a[i][j+1];
 92                 in[i][a[i][j+1]]++;
 93             }
 94         }
 95         for(i=1;i<=m;i++)
 96         {
 97             for(j=1;j<n;j++)
 98             {
 99                 nex[b[i][j]][i][1][0]=b[i][j+1],
100                 nex[b[i][j]][i][1][1]=i;
101                 in[b[i][j+1]][i]++;
102             }
103         }
104         tuopu();
105         printf("%d\n",ans);
106     }
107     return 0;
108 }
109 /*
110 //
111
112 //
113 */

时间: 2024-08-10 02:36:24

XMU 1040 Schedule 【拓扑排序】的相关文章

LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)

求有向图中是否有环. 法一:拓扑排序 用一个队列维护所有入度为0的节点,每次弹出一个节点v,查看从v可达的所有节点u; 将u的入读减一,若u的入度此时为0, 则将u加入队列. 在队列为空时,检查所有节点的入度,若所有节点入度都为0, 则存在这样的一个拓扑排序 -- 有向图中不存在环. 代码: class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisite

LeetCode 207. Course Schedule(拓扑排序)

题目 题意:有n门课程,就是n个顶点,有m个对应关系:x,y,表示只有先上了y,才能上x.也就是x到y有一条有向边.问你求是否存在环. 题解:对于有向图求是否存在环,可以用拓扑排序,拓扑排序就是寻找入度为0的顶点,然后删去,并减少相邻点的入度,再寻找入度为0的点,直到所有顶点都删去,如果存在换,那么一定会有一些点是无法删除的. class Solution { public: vector<int> edge[10005]; int a[10005]; int b[10005]; int vi

LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到,在for (auto p: prerequistites)中特判了输入中可能出现的平行边或自环. 代码: class Solution { public: vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { // [0, {1, 2, 3}], me

LeetCode 207. Course Schedule(拓扑排序)

题目 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and

(拓扑排序) bzoj 1040

4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec  Memory Limit: 512 MBSubmit: 498  Solved: 275[Submit][Status][Discuss] Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予 1到N的顺序编号,预估质量最高的菜肴编号为1.由于菜肴之间口味搭配的问题, 某些菜肴必须在另一些菜肴之前制作,具体的,一

Gym 100792 King&#39;s Rout 拓扑排序

K. King's Rout time limit per test 4.0 s memory limit per test 512 MB input standard input output standard output The great rout will be held this evening in the palace of his majesty Nassah II, the king of Occorom. There are n guests invited. While

LeetCode编程训练 - 拓扑排序(Topological Sort)

拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序(被依赖的排在前面),或给出排序结果. 最常用解决拓扑排序问题的方法是Kahn算法,步骤可以概括为: 1. 根据依赖关系,构建邻接矩阵或邻接表.入度数组 2. 取入度为0的数据(即不依赖其他数据的数据),根据邻接矩阵/邻接表依次减小依赖其的数据的入度 3. 判断减小后是否有新的入度为0的数据,继续进

拓扑排序的简单证明以及由来

介绍 拓扑排序,很多人都可能听说但是不了解的一种算法.或许很多人只知道它是图论的一种排序,至于干什么的不清楚.又或许很多人可能还会认为它是一种啥排序.而实质上它是对有向图的顶点排成一个线性序列. 至于定义,百科上是这么说的: 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边<u,v>∈E(G),则u在线性序列中出现在v之前.通常,这样的线性序列称为满足拓扑次序(Topological O

拓扑排序讲解

在这里我们要说的拓扑排序是有前提的 我们在这里说的拓扑排序是基于有向无环图的!!!. (⊙o⊙)…我所说的有向无环图都知道是什么东西吧.. 如果不知道,我们下面先来来说说什么是有向无环图. 所谓有向无环图,顾名思义是不存在环的有向图(至于有向图是什么不知道的在前面我们有一个图论讲解上都有). 点的入度:以这个点为结束点的边数. 点的出度:以这个点为出发点的边的条数. 拓扑序就是对于一个节点的一个排列,使得(u,v)属于E,那么u一定出现在v的前面.然而拓扑排序就是一个用来求拓扑序的东西. 对于左