您的位置:首页 > 大数据 > 人工智能

[AIZU1361] Deadlock Detection [2015 ACM-ICPC Asia Tsukuba Regional Contest F]

2016-07-30 14:58 651 查看

题意

给出p个进程和r种资源,每一种资源有ri个资源,每个进程对每一种资源需要need[p][r]个。

给出时刻表,每个时刻会有一个进程请求某一种资源中的一个,当一个进程将所有需要的资源请求完毕后会释放掉所有资源。由于资源分配顺序不当可能出现“锁死现象”,某两个进程等待对方施放资源。问什么时刻会“不可避免地”将要出现“锁死现象”。

题解

二分时间t,t及之前的请求按照给出的分配顺序进行分配,之后不断枚举所有的进程,如果该进程满足所有的资源数目,则释放它占用的资源,直到不能结束进程。当这个过程结束后还存在未结束的进程,那么当前时刻就是个“不可避免地”发生锁死的时刻,反之,则为可能不会发生锁死的时刻。

代码

/****************************************\
* Author : ztx
* Title  :
* ALG    :
* CMT    :
* Time   :
\****************************************/

#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define rep(i,l,r) for(i=(l);i< (r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
#define rev(i,r,l) for(i=(r);i> (l);i--)
typedef long long ll ;
typedef double lf ;
int CH , NEG ;
template <typename TP>inline void read(TP& ret) {
ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
if (CH == '-') NEG = true , CH = getchar() ;
while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
if (NEG) ret = -ret ;
}
template <typename TP>inline void readc(TP& ret) {
while (ret=getchar() , ret<'!') ;
while (CH=getchar() , CH>'!') ;
}
template <typename TP>inline void reads(TP *ret) {
ret[0]=0;while (CH=getchar() , CH<'!') ;
while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ;
ret[ret[0]+1]=0;
}

#include <cstring>

#define  maxx  310LL
#define  maxt  200010LL

int pro, res, tim;
int orineed[maxx][maxx];
int p[maxt], r[maxt];

int t_need[maxx][maxx], t_avai[maxx];
bool t_term[maxx];
struct statu {
int need[maxx][maxx];
int apply[maxx], avai[maxx];
bool term[maxx];
inline void init() {
int i,j;
Rep (i,1,res) read(avai[i]);
Rep (i,1,pro) term[i] = false, apply[i] = 0;
Rep (i,1,pro) Rep (j,1,res)
read(orineed[i][j]), need[i][j] = orineed[i][j], apply[i] += need[i][j];
}
inline void acqu(int p,int r) {
need[p][r] -- , apply[p] -- , avai[r] -- ;
if (!apply[p]) {
term[p] = true;
Rep (r,1,res) avai[r] += orineed[p][r];
}
}
inline void transcript() {
memcpy(t_need, need, sizeof need);
memcpy(t_avai, avai, sizeof avai);
memcpy(t_term, term, sizeof term);
}
inline bool valid() {
int i, j;
bool RE = false, release;
transcript();
#define  need  t_need
#define  avai  t_avai
#define  term  t_term
while (!RE && (RE=true))
Rep (i,1,pro) if (!term[i]) {
release = true;
Rep (j,1,res) if (need[i][j] > avai[j]) {
release = false; break;
}
if (release) {
term[i] = true;
Rep (j,1,res)
avai[j] += orineed[i][j]-need[i][j];
RE = false;
}
}
Rep (i,1,pro) if (!term[i]) return false;
return true;
#undef  need
#undef  avai
#undef  term
}
}last, now;

int main() {
int i, L, R, M;
//  #define READ
#ifdef  READ
freopen(".in" ,"r",stdin ) ;
freopen(".out","w",stdout) ;
#endif
while (scanf("%d%d%d", &pro, &res, &tim) != EOF) {
last.init();
Rep (i,1,tim)
read(p[i]), read(r[i]);
last.acqu(p[1],r[1]);
L = 1, R = tim+1;
while (M=L+R>>1 , L+1 < R) {
now = last;
Rep (i,L+1,M) now.acqu(p[i],r[i]);
if (now.valid()) last = now, L = M;
else R = M;
}
printf("%d\n", (R>tim ? -1 : R));
}
#ifdef  READ
fclose(stdin) ; fclose(stdout) ;
#else
getchar() ; getchar() ;
#endif
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: