http://acm.hdu.edu.cn/showproblem.php?pid=1534

Schedule Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2196    Accepted Submission(s): 994
Special Judge

Problem Description
A project can be divided into several parts. Each part should be completed continuously. This means if a part should take 3 days, we should use a continuous 3 days do complete it. There are four types of constrains among these parts which are FAS, FAF, SAF and SAS. A constrain between parts is FAS if the first one should finish after the second one started. FAF is finish after finish. SAF is start after finish, and SAS is start after start. Assume there are enough people involved in the projects, which means we can do any number of parts concurrently. You are to write a program to give a schedule of a given project, which has the shortest time.
 
Input
The input file consists a sequences of projects.

Each project consists the following lines:

the count number of parts (one line) (0 for end of input)

times should be taken to complete these parts, each time occupies one line

a list of FAS, FAF, SAF or SAS and two part number indicates a constrain of the two parts

a line only contains a '#' indicates the end of a project

 
Output
Output should be a list of lines, each line includes a part number and the time it should start. Time should be a non-negative integer, and the start time of first part should be 0. If there is no answer for the problem, you should give a non-line output containing "impossible".

A blank line should appear following the output for each project.

Sample Input
3
2
3
4
SAF 2 1
FAF 3 2
#
3
1
1
1
SAF 2 1
SAF 3 2
SAF 1 3
#
0
 
Sample Output
Case 1:
1 0
2 2
3 1

Case 2:
impossible

题目大意:给一堆工作所需花费的时间,然后给出工作的顺序【即某些工作要在另一些工作开始之后才能开始,有些工作在另一些工作结束之后才能开始,有些工作在另一些工作结束之后结束,有些工作要在另一些工作开始之后结束..】求怎样安排每个工作的开始时间可以使每件工作尽早结束。
题目分析:定义D【I】表示工作 I 的开始时间,T【I】为 工作 I 的花费时间,则可以根据这些先后顺序列出不等式,然后建图,确保连通,跑一遍SPFA即可
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct edge{
int to;
int next;
int len;
}qwq[];
queue<int>pq;
int edge_cnt=, n,t[],head[],in[],stk[],dist[];
bool spfa()
{
while(!pq.empty())
{
pq.pop();
}
pq.push();
in[]++;
stk[]=;
while(!pq.empty())
{
int qaq=pq.front();pq.pop();
stk[qaq]=;
for(int i = head[qaq];i!=-;i=qwq[i].next)
{
int v=qwq[i].to;
if(dist[v]<dist[qaq]+qwq[i].len)
{
dist[v]=dist[qaq]+qwq[i].len;
if(!stk[v])
{
pq.push(v);
in[v]++;
stk[v]=;
if(in[v]>n+){
return false;
}
}
}
}
}
return true;
}
void add(int x,int y,int z)
{
qwq[edge_cnt].to=y;
qwq[edge_cnt].next=head[x];
qwq[edge_cnt].len=z;
head[x]=edge_cnt++;
}
int main()
{
scanf("%d",&n);
int case1=;
while(n)
{
memset(head,-,sizeof(head));
memset(dist,-,sizeof(dist));
dist[]=;
memset(in,,sizeof(in));
memset(stk,,sizeof(stk));
edge_cnt=;
for(int i = ;i <= n ; i++)
{
scanf("%d",&t[i]);
}
char ss[];
int a,c;
scanf("%s",ss);
while(ss[]!='#')//FAS, FAF, SAF and SAS.
{
scanf("%d%d",&a,&c);
if(ss[]=='S')
{
if(ss[]=='S')
{
add(c,a,);
// cout << c << a << "0\n";
//cout << ss[6]<<" "<<ss[4]-'0' << "0" <<endl;
}
else
{
add(c,a,t[c]);
// cout << c << a <<t[c]<<endl;
// cout << ss[6]<<" "<<ss[4]-'0' << t[ss[6]-'0'] <<endl;
}
}
else
{
if(ss[]=='S')
{
add(c,a,-t[a]);
//cout << c << a <<-t[a]<<endl;
// cout << ss[6]<<" "<<ss[4]-'0' << -t[ss[4]-'0'] <<endl;
}
else
{
add(c,a,-t[a]+t[c]);
// cout << c<<a <<-t[a]+t[c]<<endl;
//cout << ss[6]<<" "<<ss[4]-'0' << -t[ss[4]-'0']+t[ss[6]-'0'] <<endl;
}
}
for(int i = ; i <= n ; i++)
{
add(,i,);
}
scanf("%s",ss);
}
printf("Case %d:\n",case1++);
if(!spfa())
printf("impossible\n");
else
for(int i = ; i <= n ;i++)
{
printf("%d %d\n",i,dist[i]);
}
printf("\n");
scanf("%d",&n);
}
return ;
}

【HDOJ1534】【差分约束+SPFA】的更多相关文章

  1. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  2. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  3. poj3159 差分约束 spfa

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

  4. 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...

  5. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  6. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  7. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  8. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  9. 图论分支-差分约束-SPFA系统

    据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...

  10. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

随机推荐

  1. dump_stack使用

    我们在调试内核时可以用printk打印信息.但有时我们不知道一个函数或者一个模块到底在哪里出了问题.这时我们可以用dump_stack打印信息,查看函数调用关系,找到问题的根源.使用实例: hello ...

  2. Android 音视频深入 四 录视频MP4(附源码下载)

    本篇项目地址,名字是<录音视频(有的播放器不能放,而且没有时长显示)>,求star https://github.com/979451341/Audio-and-video-learnin ...

  3. Fiddler系列教程2:手机抓包图文教程

    上篇Fiddler教程,我们教了大家Fiddler安装配置及如何使用Fiddler进行基本的Http抓包及模拟请求,今天给大家介绍下如何使用Fiddler进行手机抓包. 运行环境为Windows 10 ...

  4. day21-python操作mysql1

    python的mysql操作 mysql数据库是最流行的数据库之一,所以对于python操作mysql的了解是必不可少的.Python标准数据库接口为Python DB-API, Python DB- ...

  5. CSS多div放一行

    HTML代码 <body> <div class="right"></div> <div class="left"&g ...

  6. TTL反相器的外部特性

    TTL反相器的外部特性 电压传输特性 输入端噪声容限特性 静态输入特性: 静态输出特性: 动态特性: 传输延迟时间:是由晶体管的延迟时间,电阻以及寄生电容元素引起的.包括俩部分:输入由低电平跳为高电平 ...

  7. 【转】Java迭代:Iterator和Iterable接口

    Java迭代 : Iterator和Iterable接口 从英文意思去理解 Iterable :故名思议,实现了这个接口的集合对象支持迭代,是可迭代的.able结尾的表示 能...样,可以做.... ...

  8. word-wrap与break-word属性的区别

    共同点 word-wrap:break-word与word-break:break-all都能把长单词强行断句 不同点 word-wrap:break-word会首先起一个新行来放置长单词,新的行还是 ...

  9. shell脚本总结

    1.变量 A:  定义变量A=1,只会对自己所在的shell进程有效 B: 定义变量export B=1,会对自己所在的shell进程及其子进程生效 C: 在script.sh脚本中定义的变量,在当前 ...

  10. doctype和Quirks模式

    doctype: 告诉浏览器使用什么模式去渲染页面,可能会影响页面的css渲染和js代码的执行. DTD :为了兼容旧的浏览器渲染方式,将DTD作为参数告诉浏览器使用什么模式渲染页面.始于IE6; 1 ...