差分约束系统讲解看这里:http://blog.csdn.net/xuezhongfenfei/article/details/8685313

模板题,不多说。要注意的一点是!!!对于带有within的语句,要建立两个不等式!!!x要在y开始的z分钟内开始的话,x<=y+z 并且 x>=y。别忘了。

spfa判负权回路。

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:

  1. task 1
  2. after task 1 but within 2 minutes of it, do task 2
  3. 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 mlines 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 imust 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 Aminutes 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.
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int v[100010],first[110],w[100010],__next[100010],e;
void AddEdge(int U,int V,int W)
{
v[++e]=V;
w[e]=W;
__next[e]=first[U];
first[U]=e;
}
//struct Point
//{
// int d,u;
// Point(const int &X,const int &Y){d=X;u=Y;}
// Point(){}
//};
int n,m;
queue<int>q;
int dis[110],cnts[110];
bool inq[110];
bool spfa(const int &s)
{
memset(inq,0,sizeof(inq));
memset(cnts,0,sizeof(cnts));
while(!q.empty())
q.pop();
for(int i=1;i<=n+1;++i)
dis[i]=1000000007;
dis[s]=0; q.push(s); inq[s]=1; ++cnts[s];
while(!q.empty())
{
int U=q.front();
for(int i=first[U];i;i=__next[i])
if(dis[v[i]]>dis[U]+w[i])
{
dis[v[i]]=dis[U]+w[i];
if(!inq[v[i]])
{
q.push(v[i]);
inq[v[i]]=1;
++cnts[v[i]];
if(cnts[v[i]]>n+1)
return 0;
}
}
q.pop(); inq[U]=0;
}
return 1;
}
int main()
{
char op[12],s[12];
int x,y,z;
while(1)
{
scanf("%d",&n);
if(!n)
break;
scanf("%d",&m);
e=0;
memset(first,0,sizeof(first));
memset(__next,0,sizeof(__next));
memset(v,0,sizeof(v));
memset(w,0,sizeof(w));
for(int i=1;i<=m;++i)
{
scanf("%s",op);
scanf("%d",&x);
scanf("%s",op);
scanf("%s",op);
if(op[0]=='a')
{
scanf("%s",op);
scanf("%d",&z);
scanf("%s",op);
scanf("%s",op);
scanf("%s",op);
scanf("%s",op);
scanf("%d",&y);
AddEdge(x,y,-z);
}
else
{
scanf("%d",&z);
scanf("%s",op);
scanf("%s",op);
scanf("%s",op);
scanf("%s",op);
scanf("%s",op);
scanf("%s",op);
scanf("%s",op);
scanf("%d",&y);
AddEdge(y,x,z);
AddEdge(x,y,0);
}
}
for(int i=1;i<=n;++i)
AddEdge(n+1,i,0);
if(spfa(n+1))
{
int t=*min_element(dis+1,dis+n+1);
// if((*max_element(dis+1,dis+n+1))-t+1>=1000000)
// {
// puts("Impossible.");
// continue;
// }
for(int i=1;i<n;++i)
printf("%d ",dis[i]-t+1);
printf("%d\n",dis[n]-t+1);
}
else
puts("Impossible.");
}
return 0;
}

【差分约束系统】【spfa】UVALive - 4885 - Task的更多相关文章

  1. 差分约束系统 + spfa(A - Layout POJ - 3169)

    题目链接:https://cn.vjudge.net/contest/276233#problem/A 差分约束系统,假设当前有三个不等式 x- y <=t1 y-z<=t2 x-z< ...

  2. 【差分约束系统/SPFA】POJ3169-Layout

    [题目大意] n头牛从小到大排,它们之间某些距离不能大于一个值,某些距离不能小于一个值,求第一头牛和第N头牛之间距离的最大值. [思路] 由题意可以得到以下不等式d[AL]+DL≥d[BL]:d[BD ...

  3. BZOJ 2330 [SCOI2011]糖果 ——差分约束系统 SPFA

    最小值求最长路. 最大值求最短路. 发现每个约束条件可以转化为一条边,表示一个点到另外一个点至少要加上一个定值. 限定了每一个值得取值下界,然后最长路求出答案即可. 差分约束系统,感觉上更像是两个变量 ...

  4. UVALive - 4885 Task 差分约束

    Task 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page ...

  5. 差分约束系统+spfa(B - World Exhibition HDU - 3592 )

    题目链接:https://cn.vjudge.net/contest/276233#problem/B 思路和上一个一样,不过注意点有两个,第一,对dis数组进行初始化的时候,应该初始化成ox3f3f ...

  6. PKU 1201 Intervals(差分约束系统+Spfa)

    题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...

  7. ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)

    当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...

  8. spfa+差分约束系统(C - House Man HDU - 3440 )+对差分约束系统的初步理解

    题目链接:https://cn.vjudge.net/contest/276233#problem/C 题目大意:有n层楼,给你每个楼的高度,和这个人单次的最大跳跃距离m,两个楼之间的距离最小是1,但 ...

  9. 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801470.html 题目描述 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总 ...

随机推荐

  1. 洛谷P1273 有线电视网 (树上分组背包)

    洛谷P1273 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  3. Endnote 中文参考文献样式修改版

    http://blog.yuelong.info/post/endnote-gbt7714-2005.html 很多人不知道 EndNote 是自带中文参考文献引用样式的,即符合<文后参考文献著 ...

  4. sqrti128

    求平方根下取整,对于gcc type __uint128_t. ~45.5ns/op on i7-7700k@4.35G,即typical <200cyc/op. Together with u ...

  5. jquery中:input和input的区别

    :input表示选择表单中的input,select,textarea,button元素, input仅仅选择input元素. <button>和<input type=" ...

  6. js实现日历

    有这样一个普通的日历需求 第一反应就是找插件,结果找到了,但是改起来非常麻烦,然后查下实现的原理,发现原来很简单,于是自己实现了一个. 首先分析一下这个组件,每页显示的是 当前月的所有日期及所占据的行 ...

  7. JS模块化工具requirejs教程01

    转自:http://www.runoob.com/w3cnote/requirejs-tutorial-1.html 随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签 ...

  8. java+ssh+eclipse开发过程问题记录

    原文 http://www.sdfengxi.com/?p=408   最近在忙着的项目是基于cloudstack平台的管理平台,因为CloudStack使用java开发,管理机上已部署好rhel+t ...

  9. nginx实时生成缩略图到硬盘上

    现在随着各终端的出现(手机,ipad等平板),以及各种终端的手机分辨率和尺寸都不同,现在手机用户流量都是宝,网上出现了各种各样的生成缩略图功能的架构,有使用php实时生成缩略图的,也有用nginx + ...

  10. 在Servlet中出现一个输出中文乱码的问题

     添加:reqeust.setCharacterEncoding("utf-8");