In most recipes, certain tasks have to be done before others. For each task, if we are given a list of
other tasks that it depends on, then it is relatively straightforward to come up with a schedule of tasks
that satisfies the dependencies and produces a stunning dish. Many of us know that this can be solved
by some algorithm called toplogical sort.
But life is not so easy sometimes. For example, here is a recipe for making pizza dough:c++
The input consists of a number of cases. The first line of each case gives the number of tasks n
(1 ≤ n ≤ 100). This is followed by a line containing a non-negative integer m giving the number of
constraints. Each of the next m lines specify a constraint. The two possible forms of constraints are:
task i starts at least A minutes later than task j
task i starts within A minutes of the starting time of task j
where i and j are the task numbers of two different tasks (1 ≤ i, j ≤ n), and A is a non-negative integer
(A ≤ 150). The first form states that task i must start at least A minutes later than the start time of
task j. The second form states that task i must start no earlier than task j, and within A minutes of
the starting time of task j. There may be multiple constraints involving the same pair of tasks.
Note that at least and within include the end points (i.e. if task 1 starts at 1 minute and task 2
starts at 4 minutes, then task 2 starts at least 3 minutes later than task 1, and within 3 minutes of the
starting time of task 1).
The input is terminated by n = 0.less
For each case, output a single line containing the starting times of task 1 through task n separated by a
single space. Each starting time should specify the minute at which the task starts. The starting time
of each task should be positive and less than 1000000. There may be many possible schedules, and any
valid schedule will be accepted. If no valid schedule is possible, print ‘Impossible.’ on a line instead.ide
6
10
task 3 starts at least 5 minutes later than task 1
task 3 starts within 10 minutes of the starting time of task 1
task 3 starts at least 7 minutes later than task 2
task 3 starts within 9 minutes of the starting time of task 2
task 4 starts at least 10 minutes later than task 3
task 4 starts within 15 minutes of the starting time of task 3
task 5 starts at least 90 minutes later than task 4
task 5 starts within 120 minutes of the starting time of task 4
task 6 starts at least 10 minutes later than task 5
task 6 starts within 15 minutes of the starting time of task 5
3
4
task 2 starts at least 0 minutes later than task 1
task 2 starts within 2 minutes of the starting time of task 1
task 3 starts at least 3 minutes later than task 2
task 3 starts within 2 minutes of the starting time of task 1
0this
3 1 8 18 108 118
Impossible.spa
給你n個變量,而後m組描述:
每組描述A,B,C,
要麼是A比B至少大C
要麼是A超過B最多Crest
而後輸出一組合法解。code
拆開看,描述實際上是:
A-B>=C
B-A>=-C,A-B>=0orm
根據這個描述創建差分約束,沒有負環即有解。ip
#include<bits/stdc++.h> using namespace std; const int maxn = 105; vector<int> E1[maxn]; vector<int> V1[maxn]; int vis[maxn]; int n,m,d[maxn]; int spfa(int x){ queue<int> Q; Q.push(x); d[x]=0; vis[x]=1; int T = 0; while(!Q.empty()){ x=Q.front(); Q.pop(); T++; if(T>10000)return 0; for(int i=0;i<E1[x].size();i++){ int v=E1[x][i]; if(d[v]>d[x]+V1[x][i]){ d[v]=d[x]+V1[x][i]; vis[v]=1; Q.push(v); } } } return 1; } int main(){ while(scanf("%d",&n)!=EOF){ if(n==0)break; memset(vis,0,sizeof(vis)); scanf("%d",&m); for(int i=0;i<maxn;i++) E1[i].clear(),V1[i].clear(); for(int i=0;i<m;i++){ int A,B,C; string s1; //task i starts at least A minutes later than task j //task i starts within A minutes of the starting time of task j cin>>s1; scanf("%d",&A); cin>>s1; string s; cin>>s; if(s[0]=='a'){ cin>>s1; scanf("%d",&B); for(int i=0;i<4;i++) cin>>s1; scanf("%d",&C); E1[A].push_back(C); V1[A].push_back(-B); E1[A].push_back(C); V1[A].push_back(0); }else{ scanf("%d",&B); for(int i=0;i<7;i++) cin>>s1; scanf("%d",&C); E1[C].push_back(A); V1[C].push_back(B); E1[A].push_back(C); V1[A].push_back(0); } } for(int i=0;i<maxn;i++) d[i]=1e9; int flag=1; for(int i=1;i<=n;i++){ if(!vis[i]){ if(spfa(i)==0){ flag=0; break; } } } if(flag==0){ printf("Impossible.\n"); continue; } for(int i=1;i<=n;i++){ if(i==1)printf("%d",d[i]+1); else printf(" %d",d[i]+1); } printf("\n"); } }