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. git开发过程的配置和使用

    git开发过程的使用 1.创建仓库 2.新建项目,填写项目名称等信息 3.初始化仓库,创建git仓库 git init 4.配置个人信息(配置过可忽略) git config --global use ...

  2. learning at command AT+CIMI

    AT command AT+CIMI [Purpose]        Learning how to get the International Mobile Subscriber Identity ...

  3. xadmin自定义关联菜单

    网上好多自定义xadmin后台数据很少有关怎样设置外键关联菜单的显示,如下图所示: 现有个需求根据model中status字段值,来显示关联菜单三道杠,如上图app状态只有是审核成功才会显示,未审核不 ...

  4. mybatis 无法自动补全,没有获得dtd文件

    由于网络原因,eclipse无法下载到http://mybatis.org/dtd/mybatis-3-mapper.dtd,导致eclipse的编辑器无法自动补全标签. 解决办法:将dtd文件下载到 ...

  5. Java遍历集合的几种方法分析(实现原理、算法性能、适用场合)

    概述 Java语言中,提供了一套数据集合框架,其中定义了一些诸如List.Set等抽象数据类型,每个抽象数据类型的各个具体实现,底层又采用了不同的实现方式,比如ArrayList和LinkedList ...

  6. ssm使用双数据源

    工作中需要接入其他公司业务系统的数据进行分析,于是接入它们的db. 使用双数据源配置感觉如下: database.sessionFactory.扫描器.事务管理器等双份. 听说如果两个数据源需要一起使 ...

  7. Oracle 12c中新建pdb用户登录问题分析

    Oracle 12c新建用户登录问题分析1 用sys用户新建用户,提示公用用户名或角色名无效.原因:Oracle 12c中,在容器中建用户(或者应该称为使用者),须在用户名前加c##.默认登录连接的就 ...

  8. svn+http+ad域

    svn本地添加用户太麻烦了,如果公司有一百个开发人员要访问,要创建账号密码太麻烦了:所以让他们用AD域账号去登录就很方便,但是权限的管控还是在svn的本地添加(这个暂时还没办法很好的解决) 一.安装依 ...

  9. SpringMVC实现从磁盘中下载文件

    除了文件的上传我们还需要从磁盘下载 实现文件的下载只要编写一个控制器,完成读写操作和响应头和数据类型的设置就可以了 下面演示的是从G盘imgs文件夹中下载文件 具体代码如下 package com.c ...

  10. Jmeter监听tomcat

    配置cd /usr/local/tomcat/conf/tomcat-users.xml