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

[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] }



/****************************************************
	
	Author : Coolxxx
	Copyright 2017 by Coolxxx. All rights reserved.
	BLOG : http://blog.csdn.net/u010568270
	
****************************************************/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
const double EPS=1e-8;
const int J=10000;
const int MOD=100000007;
const int MAX=0x7f7f7f7f;
const double PI=3.14159265358979323;
const int N=304;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int t[N][N],a[N][N],b[N][N],d[N][N],in[N][N];
int nex[N][N][2][2];
void tuopu()
{
	int i,j,x,y,xx,yy;
	mem(d,0);
	queue<int>qx,qy;
	for(i=1;i<=n;i++)
	{
		if(!in[i][a[i][1]])
		{
			d[i][a[i][1]]=t[i][a[i][1]];
			qx.push(i);
			qy.push(a[i][1]);
		}
	}
	while(!qx.empty())
	{
		x=qx.front();qx.pop();
		y=qy.front();qy.pop();
		for(i=0;i<2;i++)
		{
			xx=nex[x][y][i][0];
			yy=nex[x][y][i][1];
			if(!x || !y)continue;
			d[xx][yy]=max(d[xx][yy],d[x][y]+t[xx][yy]);
			if(!--in[xx][yy])
			{
				qx.push(xx);
				qy.push(yy);
			}
		}
		ans=max(ans,d[x][y]);
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k,l;
	int x,y,z;
//	for(scanf("%d",&cass);cass;cass--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
	while(~scanf("%d",&n))
	{
		ans=0;
		mem(nex,0);mem(in,0);
		scanf("%d",&m);
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
				scanf("%d",&t[i][j]);
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
				scanf("%d",&a[i][j]);
		for(i=1;i<=m;i++)
			for(j=1;j<=n;j++)
				scanf("%d",&b[i][j]);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<m;j++)
			{
				nex[i][a[i][j]][0][0]=i,
				nex[i][a[i][j]][0][1]=a[i][j+1];
				in[i][a[i][j+1]]++;
			}
		}
		for(i=1;i<=m;i++)
		{
			for(j=1;j<n;j++)
			{
				nex[b[i][j]][i][1][0]=b[i][j+1],
				nex[b[i][j]][i][1][1]=i;
				in[b[i][j+1]][i]++;
			}
		}
		tuopu();
		printf("%d\n",ans);
	}
	return 0;
}
/*
//

//
*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章