Professional Ability Test (PAT) consists of several series of subject tests. Each test is divided into several levels. Level A is a prerequisite (前置要求) of Level B if one must pass Level A with a score no less than S in order to be qualified to take Level B. At the mean time, one who passes Level A with a score no less than S will receive a voucher(代金券)of D yuans (Chinese dollar) for taking Level B.
At the moment, this PAT is only in design and hence people would make up different plans. A plan is NOT consistent if there exists some test T so that T is a prerequisite of itself. Your job is to test each plan and tell if it is a consistent one, and at the mean time, find the easiest way (with minimum total S) to obtain the certificate of any subject test. If the easiest way is not unique, find the one that one can win the maximum total value of vouchers.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤) and M, being the total numbers of tests and prerequisite relations, respectively. Then M lines follow, each describes a prerequisite relation in the following format:
T1 T2 S D
where T1
and T2
are the indices (from 0 to N−1) of the two distinct tests; S
is the minimum score (in the range (0, 100]) required to pass T1
in order to be qualified to take T2
; and D
is the value of the voucher (in the range (0, 500]) one can receive if one passes T1
with a score no less than S
and plan to take T2
. It is guaranteed that at most one pair of S
and D
are defined for a prerequisite relation.
Then another positive integer K (≤) is given, followed by K queries of tests. All the numbers in a line are separated by spaces.
Output Specification:
Print in the first line Okay.
if the whole plan is consistent, or Impossible.
if not.
If the plan is consistent, for each query of test T
, print in a line the easiest way to obtain the certificate of this test, in the format:
T0->T1->...->T
However, if T
is the first level of some subject test (with no prerequisite), print You may take test T directly.
instead.
If the plan is impossible, for each query of test T
, check if one can take it directly or not. If the answer is yes, print in a line You may take test T directly.
; or print Error.
instead.
Sample Input 1:
8 15
0 1 50 50
1 2 20 20
3 4 90 90
3 7 90 80
4 5 20 20
7 5 10 10
5 6 10 10
0 4 80 60
3 1 50 45
1 4 30 20
1 5 50 20
2 4 10 10
7 2 10 30
2 5 30 20
2 6 40 60
8
0 1 2 3 4 5 6 7
Sample Output 1:
Okay.
You may take test 0 directly.
0->1
0->1->2
You may take test 3 directly.
0->1->2->4
0->1->2->4->5
0->1->2->6
3->7
Sample Input 2:
4 5
0 1 1 10
1 2 2 10
3 0 4 10
3 2 5 10
2 0 3 10
2
3 1
Sample Output 2:
Impossible. You may take test 3 directly. Error.
AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=1111; 5 vector<int> edge[maxn]; 6 struct node 7 { 8 int s,d; 9 }stu[maxn]; 10 vector<node> weight[maxn]; 11 int in[maxn],du[maxn],n,m,pre[maxn],tmp,ans[maxn],tot; 12 queue<int> que; 13 void bfs() 14 { 15 for(int i=1;i<=n;++i){ 16 if(in[i]==0) que.push(i); 17 } 18 while(!que.empty()){ 19 int hd=que.front(); 20 que.pop(); 21 ++tmp; 22 for(int i=0;i<edge[hd].size();++i){ 23 int y=edge[hd][i],s=weight[hd][i].s,d=weight[hd][i].d; 24 --in[y]; 25 if(in[y]==0) que.push(y); 26 s+=stu[hd].s; 27 d+=stu[hd].d; 28 if(stu[y].s==0){ 29 stu[y].s=s; 30 stu[y].d=d; 31 pre[y]=hd; 32 } 33 else{ 34 if(s<stu[y].s){ 35 stu[y].s=s; 36 stu[y].d=d; 37 pre[y]=hd; 38 } 39 else if(s==stu[y].s){ 40 if(d>stu[y].d){ 41 stu[y].d=d; 42 pre[y]=hd; 43 } 44 } 45 } 46 } 47 } 48 } 49 int main() 50 { 51 scanf("%d %d",&n,&m); 52 for(int i=1;i<=n;++i){ 53 stu[i].s=0; 54 stu[i].d=0; 55 } 56 while(m--){ 57 int t1,t2,s,d; 58 scanf("%d %d %d %d",&t1,&t2,&s,&d); 59 ++t1;++t2; 60 edge[t1].push_back(t2); 61 node cnt; 62 cnt.s=s;cnt.d=d; 63 weight[t1].push_back(cnt); 64 ++in[t2];++du[t2]; 65 } 66 bfs(); 67 if(tmp<n) printf("Impossible. "); 68 else printf("Okay. "); 69 int q; 70 scanf("%d",&q); 71 while(q--){ 72 int x; 73 scanf("%d",&x); 74 ++x; 75 if(du[x]==0) printf("You may take test %d directly. ",x-1); 76 else if(tmp<n) printf("Error. "); 77 else{ 78 ans[0]=x;tot=0; 79 while(pre[x]>0){ 80 ans[++tot]=pre[x]; 81 x=pre[x]; 82 } 83 for(int i=tot;i>=1;--i){ 84 printf("%d->",ans[i]-1); 85 } 86 printf("%d ",ans[0]-1); 87 } 88 } 89 return 0; 90 }