Task

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2886

Description

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:

  1. Mix the yeast with warm water, wait for 5 to 10 minutes.
  2. Mix the the remaining ingredients 7 to 9 minutes.
  3. Mix the yeast and the remaining ingredients together for 10 to 15 minutes.
  4. Wait 90 to 120 minutes for the dough to rise.
  5. Punch the dough and let it rest for 10 to 15 minutes.
  6. Roll the dough.

    In this case, tasks 1 and 2 may be scheduled after the first minute (we always spend the first minute

    to read the recipe and come up with a plan). The earliest task 3 may be started is at 8 minutes, and

    task 4 may start at 18 minutes after the start, and so on. This recipe is relatively simple, but if some

    tasks have many dependent tasks then scheduling can become unmanageable. Sometimes, the recipe

    may in fact be impossible to execute.

    For example, consider the following abstract recipe:
  7. task 1
  8. after task 1 but within 2 minutes of it, do task 2
  9. at least 3 minutes after task 2 but within 2 minutes of task 1, do task 3

    In this problem, you are given a number of tasks. Some tasks are related to another based on their

    starting times. You are asked to assign a starting time to each task to satisfy all constraints if possible,

    or report that no valid schedule is possible.

Input

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.

Output

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.

Sample Input

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

0

Sample Output

3 1 8 18 108 118

Impossible.

Hint

题意

给你n个变量,然后m组描述:

每组描述A,B,C,

要么是A比B至少大C

要么是A超过B最多C

然后输出一组合法解。

题解:

拆开看,描述其实是:

A-B>=C

B-A>=-C,A-B>=0

根据这个描述建立差分约束,没有负环即有解。

代码

#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");
}
}

UVALive - 4885 Task 差分约束的更多相关文章

  1. 【差分约束系统】【spfa】UVALive - 4885 - Task

    差分约束系统讲解看这里:http://blog.csdn.net/xuezhongfenfei/article/details/8685313 模板题,不多说.要注意的一点是!!!对于带有within ...

  2. UVALive 5532 King(差分约束,spfa)

    题意:假设一个序列S有n个元素,现在有一堆约束,限制在某些连续子序列之和上,分别有符号>和<.问序列S是否存在?(看题意都看了半小时了!) 注意所给的形式是(a,b,c,d),表示:区间之 ...

  3. 【拓扑排序或差分约束】Guess UVALive - 4255

    题目链接:https://cn.vjudge.net/contest/209473#problem/B 题目大意:对于n个数字,给出sum[j]-sum[i](sum表示前缀和)的符号(正负零),求一 ...

  4. Is the Information Reliable?(差分约束)

    Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...

  5. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  6. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  7. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  8. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  9. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

随机推荐

  1. 使用heptiolabs/eventrouter收集K8S的事件

    k8s的heapster项目中止以后, 事件收集的项目,就推荐使用https://github.com/heptiolabs/eventrouter项目了. 部署文档很简单,但有两个问题要解决: 一, ...

  2. HL7消息部分笔记

    1.关于HL7标准 HL7是HealthLevel7的缩写,主要用于医疗领域不同的系统.应用之间的信息传递.规范各个系统间的信息传递格式. 2.字段含义: Z信息段: Z信息段是指与HL7第二版标准其 ...

  3. Java线程池参数

    关于Java线程池的参数设置.线程池是Java多线程里开发里的重要内容,使用难度不大,但如何用好就要明白参数的含义和如何去设置.干货里的内容大多是参考别人的,加入了一些知识点的扩充和看法.希望能对多线 ...

  4. bzoj4773: 负环

    题解: 网上还有一种spfa+深度限制的算法 https://www.cnblogs.com/BearChild/p/6624302.html 是不加队列优化的spfa,我觉得复杂度上限是bellma ...

  5. python多线程之t.setDaemon(True) 和 t.join()

    0.目录 1.参考2.结论    (1)通过 t.setDaemon(True) 将子线程设置为守护进程(默认False),主线程代码执行完毕后,python程序退出,无需理会守护子线程的状态.    ...

  6. 【Android】Android 代码判断是否获取ROOT权限(一)

    [Android]Android 代码判断是否获取ROOT权限 方法比较简单,直接粘贴代码 public synchronized boolean getRootAhth() { Process pr ...

  7. Java程序编译和运行过程之 一个对象的生命之旅(类加载和类加载器)

    Java程序从创建到运行要经过两个大步骤 1:源文件(.java)由编译器编译成字节码ByteCode(.class) 2:字节码由Java虚拟机解释并运行 源文件编译成字节码,主要分成两个部分: 1 ...

  8. gitlab之三: gitlab邮件通知的配置

    参考 :  https://www.cnblogs.com/lovelinux199075/p/9072265.html gitlab 添加新用户后,会自动发送邮件到填写的邮箱. 实验版本:  11. ...

  9. HDU3488 Tour KM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8284304.html 题目传送门 - HDU3488 题意概括 给一个n的点m条边的有向图. 然后让你把这个图分 ...

  10. 今天这篇内容分享Apache由http自动跳转到https的多种方法

    本文主要和大家分享Apache http自动跳转到https的几种方法,非常不错,具有参考借鉴价值,需要的朋友参考下 本文主要和大家分享Apache http自动跳转到https的几种方法,当你的站点 ...