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. .clearfix:after(清除浮动)中各个属性及值详细解说

    清除浮动.clearfix:after一词,从事web前端的朋友们对此不会陌生吧,下面为大家介绍的是.clearfix:after中用到的所有属性及值的含义,对此感兴趣的朋友可以参考下哈想,希望对大家 ...

  2. linux:NFS

    1.简介 2.安装 安装分为服务端和客户端 [1]我们先拿一台机做服务端 yum install nfs-utils rpcbind -y 然后我们查查看安装没有,查询一个包是否被安装# rpm -q ...

  3. flask-security(一)快速入门

    很多例程都是基于flask-sqlalchemy的. 但是我使用sqlalchemy,并没有使用sqlalchemy,看中的也就是flask的灵活性. 暂时写flask的程序,但是为了以后写别的程序方 ...

  4. asp.net MVC之AuthorizeAttribute浅析

    AuthorizeAttribute是asp.net MVC的几大过滤器之一,俗称认证和授权过滤器,也就是判断登录与否,授权与否.当为某一个Controller或Action附加该特性时,没有登录或授 ...

  5. Turing equation

    Turing equation 时间限制: 1 Sec 内存限制: 128 MB 题目描述 The fight goes on, whether to store numbers starting w ...

  6. jQuery $.each()常见的几种使用方法

    <code class="language-html"><!doctype html> <html> <head> <meta ...

  7. DevExpress v18.1新版亮点——Analytics Dashboard篇(一)

    用户界面套包DevExpress v18.1日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Analytics Dashboard v18.1 的新功能,快 ...

  8. Docker(2):快速入门及常用命令

    什么是Docker? Docker 是世界领先的软件容器平台.开发人员利用 Docker 可以消除协作编码时“在我的机器上可正常工作”的问题.运维人员利用 Docker 可以在隔离容器中并行运行和管理 ...

  9. <Spark><Advanced Programming>

    Introduction 介绍两种共享变量的方式: accumulators:聚集信息 broadcast variables:高效地分布large values 介绍对高setup costs任务的 ...

  10. Day12作业及默写

    1.整理今天的博客,写课上代码,整理流程图. 2.用列表推导式做下列小题 li=['alex','wusir','abds','meet','ab'] a. 过滤掉长度小于3的字符串列表,并将剩下的转 ...