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. Unity中UI界面颤抖解决方法

    将Render Mode中属性改为Screen Space - Camera 摄像机挂在Canvas属性下会出现UI界面颤抖的效果. UI界面颤抖解决方式:将Render Mode中属性改为Scree ...

  2. Linux Shell 编程 教程 常用命令

    概述: Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用户 ...

  3. 3.2 C++继承方式

    参考: http://www.weixueyuan.net/view/6359.html  总结: 子类继承父类,继承方式将限制父类的成员属性在子类中的访问权限,子类访问父类的成员,也需要遵循其成员的 ...

  4. bootstrap table 列求和

    <div class="modal fade in" id="_modalDialog" tabindex="1" role=&quo ...

  5. jquery获取URL的参数和锚点

    由于经常会用到替换URL参数值,而网上写的方法代码都太长了,所以在这里写了一个简单的方法,供大家使用. 1)获取url参数 function getUrlParam(name) { var reg = ...

  6. CodeForces ~ 996

    Allen has a LOT of money. He has nn dollars in the bank. For security reasons, he wants to withdraw ...

  7. Chrome插件-网页版BusHound

    Chrome插件-网页版BusHound

  8. centos下mysql数据迁移方法

    第一种: 原始数据库不需要重新安装: 默认mysql会安装在/var/lib/mysql这里,若将数据迁移到/data/mysql目录下,步骤如下: 1.停止mysql服务 2.#cp /var/li ...

  9. ios读取plist文件:

    @property (nonatomic,strong) NSArray *imageData;//定义一个数组 //懒加载数据 -(NSArray *)imageDate { if(_imageDa ...

  10. Day11作业及默写

    1.写函数,传入n个数,返回字典{'max':最大值,'min':最小值} 例如:min_max(2,5,7,8,4) 返回:{'max':8,'min':2}(此题用到max(),min()内置函数 ...