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. Java Web(六) JSP

    现在的Java Web开发已经很少使用JSP脚本了,业务逻辑都交给Servlet处理,JSP只负责显示视图,所以接下来的内容就对JSP脚本不做叙述了... JSP概述 JSP全名为Java Serve ...

  2. AI工具5.13

    如果想选中上面的很多图形,可以锁定不需要选择的下面的图形.选择需要锁定的对象.“对象”“锁定”“所选对象” “对象”“变换”“再次变换”快捷键“ctrl=d"一般前面有其他操作如“移动”“复 ...

  3. Cracking The Coding Interview4.3

    //Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal h ...

  4. ubuntu多显示器单触摸屏校准

    多显示器单触摸屏屏幕校准 0.触摸屏重定向 sudo xinput map-to-output 13 DP1  #将触摸屏映射到指定的显示器 其中:13为触摸屏设备id,可通过 xinput命令查看 ...

  5. 《软件调试 Windows概要》

    操作系统是计算机系统中的基本软件.它负责管理系统中的软硬件资源.通常都包括文件管理.内存管理.进程管理.打印管理.网络管理等基本功能.除此之外,支持调试也是操作系统设计的一项根本任务. 0x01  进 ...

  6. htmlayout 最简单的实践,用于理解实现原理。

    / testHtmlayout.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include "testHtmlayout.h&qu ...

  7. tf 模型保存

    tf用 tf.train.Saver类来实现神经网络模型的保存和读取.无论保存还是读取,都首先要创建saver对象. 用saver对象的save方法保存模型 保存的是所有变量 save( sess, ...

  8. <Consistency><of HBase><CAP><ACID>

    Overview 讨论一些(分布式)(存储)系统的一致性 CAP原理 随着分布式事务的出现,传统的单机事务模型(ACID)已经无法胜任,尤其是对于一个高访问量.高并发的互联网分布式系统来说. 如何构建 ...

  9. python自学第6天,文件修改,字符编码

    文件的修改: 一般是把旧文件的内容改了,在写入到新的文件中去. file_old=open("test","r",encoding="utf-8&qu ...

  10. 2019-03-19-day014-内置函数

    昨日回顾 装饰器 对扩展开放 对修改封闭 不改变原调用方式 def a(c): def b(*args,**kwargs): c(*args,**kwargs) return b a() def a( ...