Schedule Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1085    Accepted Submission(s): 448
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

 
Source
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1529 1384 1531 3592 3666 
 
 //0MS    248K    1537 B    G++
/* 题意:
给出完成作业需要的时间,以及它们间完成的先后关系,问是否可行,可行输出每个作业的开始时间 差分约束: 有n个作业,第 i 个作业所需的时间是 a[i]; SAS u v 表示 v开始后 u 才能开始;f(u)>=f(v); SAF u v 表示 v结束后 u 才能开始;f(u)+a[u]>=f(v); FAF u v 表示 v结束后 u 才能结束;f(u)+a[u]>=f(v)+a[v]; FAS u v 表示 v开始后 u 才能结束;f(u)>=f(v)+a[v] 这里使用bellman_ford算法,建立反向边,求最长路径 */
#include<stdio.h>
#include<string.h>
#define N 1005
#define inf 0x7ffffff
struct node{
int u,v,w;
}edge[*N];
int d[N];
int a[N];
int n,edgenum;
bool bellman_ford()
{
memset(d,,sizeof(d));
bool flag=true;
for(int i=;i<=n;i++){
if(!flag) break;
flag=false;
for(int j=;j<edgenum;j++){
if(d[edge[j].v]<d[edge[j].u]+edge[j].w){
d[edge[j].v]=d[edge[j].u]+edge[j].w;
flag=true;
}
}
}
return flag; //如果执行n次后还能松弛证明有正权环
}
int main(void)
{
char opr[];
int x,y;
int k=;
while(scanf("%d",&n),n)
{
edgenum=;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
while(scanf("%s",opr)){
if(strcmp(opr,"#")==) break;
scanf("%d%d",&x,&y);
edge[edgenum].u=y;
edge[edgenum].v=x;
if(strcmp(opr,"SAS")==){
edge[edgenum].w=;
}
if(strcmp(opr,"SAF")==){
edge[edgenum].w=a[y];
}
if(strcmp(opr,"FAS")==){
edge[edgenum].w=-a[x];
}
if(strcmp(opr,"FAF")==){
edge[edgenum].w=a[y]-a[x];
}
edgenum++;
}
printf("Case %d:\n",k++);
if(bellman_ford()) puts("impossible");
else{
for(int i=;i<=n;i++)
printf("%d %d\n",i,d[i]);
}
printf("\n");
}
return ;
}

再贴一个SPFA的:

 //218MS    456K    1791 B    G++
#include<iostream>
#include<vector>
#include<queue>
#define N 1005
#define inf 0x7ffffff
using namespace std;
struct node{
int v,w;
node(int a,int b){
v=a;w=b;
}
};
vector<node>V[N];
int a[N];
int d[N],in[N],vis[N];
int n;
bool spfa()
{
memset(in,,sizeof(in));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) d[i]=-inf;
queue<int>Q;
Q.push();
vis[]=;
in[]=;
d[]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(in[u]>n) return false;
vis[u]=;
int n0=V[u].size();
for(int i=;i<n0;i++){
int v=V[u][i].v;
int w=V[u][i].w;
if(d[v]<d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
in[v]++;
Q.push(v);
vis[v]=;
}
}
}
}
return true;
}
int main(void)
{
string opr;
int x,y;
int k=;
while(cin>>n)
{
if(!n) break;
for(int i=;i<=n;i++) V[i].clear();
for(int i=;i<=n;i++){
cin>>a[i];
V[].push_back(node(i,));
}
while(cin>>opr){
if(opr=="#") break;
cin>>x>>y;
if(opr=="SAS") V[y].push_back(node(x,));
if(opr=="SAF") V[y].push_back(node(x,a[y]));
if(opr=="FAS") V[y].push_back(node(x,-a[x]));
if(opr=="FAF") V[y].push_back(node(x,a[y]-a[x]));
}
cout<<"Case "<<k++<<":"<<endl;
if(!spfa()) cout<<"impossible"<<endl;
else{
for(int i=;i<=n;i++)
cout<<i<<" "<<d[i]<<endl;
}
cout<<endl;
}
return ;
}

最新文章

  1. Bootstrap &lt;基础八&gt;图片
  2. js删除数组中的&#39;NaN&#39;
  3. java发送短信--httpclient方式
  4. 【C#】依赖于SharpZipLib的Zip压缩工具类
  5. 强、软、弱、虚引用,ReferenceQueue,WeakHashMap
  6. 张恭庆编《泛函分析讲义》第二章第2节 $Riesz$ 定理及其应用习题解答
  7. Unity Shader : Ghost(残影) v1
  8. Boost.Any
  9. JPA学习---第二节:JPA开发环境和思想介绍
  10. 反射(学习整理)----Class类和加载器ClassLoader类的整理
  11. iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
  12. 代码中函数、变量、常量 / bss段、data段、text段 /sct文件、.map文件的关系[实例分析arm代码(mdk)]
  13. linux ftp安装和配置
  14. 【BZOJ2005】【NOI2010】能量采集(莫比乌斯反演,容斥原理)
  15. 【WC2019】数树 树形DP 多项式exp
  16. Flask从入门到精通
  17. The Code analysis of the FFDNet model
  18. (C#基础) ref 和out练习
  19. 一个MySQL 5.7 分区表性能下降的案例分析
  20. String 的常用操作

热门文章

  1. jQuery 效果使用
  2. django+xadmin在线教育平台(十五)
  3. Logrotate实现Catalina.out日志每俩小时切割
  4. nuxt generate静态化后回退问题
  5. python__基础 : 类的继承,调用父类的属性和方法
  6. 史上最强大的wordpress后台框架redux-framework安装及使用
  7. CentOS7 配置环境
  8. 树上dfs+思维
  9. 通过IIS共享文件夹来实现静态资源&quot;本地分布式&quot;部署
  10. python基础之函数参数、嵌套、返回值、对象、命名空间和作用域