【差分约束系统】【spfa】UVALive - 4885 - Task
差分约束系统讲解看这里: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:
- Mix the yeast with warm water, wait for 5 to 10 minutes.
- Mix the the remaining ingredients 7 to 9 minutes.
- Mix the yeast and the remaining ingredients together for 10 to 15 minutes.
- Wait 90 to 120 minutes for the dough to rise.
- Punch the dough and let it rest for 10 to 15 minutes.
- 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:
- task 1
- after task 1 but within 2 minutes of it, do task 2
- 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的更多相关文章
- 差分约束系统 + spfa(A - Layout POJ - 3169)
题目链接:https://cn.vjudge.net/contest/276233#problem/A 差分约束系统,假设当前有三个不等式 x- y <=t1 y-z<=t2 x-z< ...
- 【差分约束系统/SPFA】POJ3169-Layout
[题目大意] n头牛从小到大排,它们之间某些距离不能大于一个值,某些距离不能小于一个值,求第一头牛和第N头牛之间距离的最大值. [思路] 由题意可以得到以下不等式d[AL]+DL≥d[BL]:d[BD ...
- BZOJ 2330 [SCOI2011]糖果 ——差分约束系统 SPFA
最小值求最长路. 最大值求最短路. 发现每个约束条件可以转化为一条边,表示一个点到另外一个点至少要加上一个定值. 限定了每一个值得取值下界,然后最长路求出答案即可. 差分约束系统,感觉上更像是两个变量 ...
- UVALive - 4885 Task 差分约束
Task 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page ...
- 差分约束系统+spfa(B - World Exhibition HDU - 3592 )
题目链接:https://cn.vjudge.net/contest/276233#problem/B 思路和上一个一样,不过注意点有两个,第一,对dis数组进行初始化的时候,应该初始化成ox3f3f ...
- PKU 1201 Intervals(差分约束系统+Spfa)
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...
- ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)
当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...
- spfa+差分约束系统(C - House Man HDU - 3440 )+对差分约束系统的初步理解
题目链接:https://cn.vjudge.net/contest/276233#problem/C 题目大意:有n层楼,给你每个楼的高度,和这个人单次的最大跳跃距离m,两个楼之间的距离最小是1,但 ...
- 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa
原文地址:http://www.cnblogs.com/GXZlegend/p/6801470.html 题目描述 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总 ...
随机推荐
- [fzu 2271]不改变任意两点最短路至多删的边数
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2271 题目中说每条边的边权都是[1,10]之间的整数,这个条件非常关键!以后一定要好好读题啊…… 做10次循环 ...
- HDU1213:How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Linux下只允许用户远程scp
本文将介绍在Linux环境下,让用户不能远程登录 只能使用scp命令 使用到的软件:rssh(http://pizzashack.org/rssh/index.shtml ) 环境:centos6.x ...
- Drac6-Web界面无法访问
1. ssh idracip, and reset RAC -- will need around 10mins /admin1-> racadm racreset RAC reset oper ...
- white-space——处理元素内的空白
定义和用法 white-space 属性设置如何处理元素内的空白.这个属性声明建立布局过程中如何处理元素中的空白符.值 pre-wrap 和 pre-line 是 CSS 2.1 中新增的. 默认 ...
- React 使用 fetch 请求天气
中国天气网(http://www.weather.com.cn)提供了查询天气的 API,通过传入城市 id, 可以获得当前城市的天气信息,API 相关信息如下: 规格 描述 主机地址 http:/ ...
- centos yum 安装 mysql
centos7下使用yum安装mysql 时间:2015-03-07 21:26:20 阅读:87445 评论:0 收藏:1 [点我收藏+] 标签: Cen ...
- Ueditor 1.4.3 插入表格后无边框无颜色,不能正常显示
在使用Ueditor 插入表格的功能时,发现插入时正常. 但保存到后台后再取出来,表格不能正常显示.查看保存的html代码,发现保存时并未给table 添加border属性.以致于再次取出来时,不能正 ...
- [BZOJ2502]清理雪道解题报告|带下界的最小流
滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机,每次飞 ...
- Linux环境下通过ODBC访问MSSql Server
为了解决Linux系统连接MSSql Server的问题,微软为Linux系统提供了连接MSSql Server的ODBC官方驱动.通过官方驱动,Linux程序可以方便地对MSSql Server进行 ...