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 
 
  1. //0MS 248K 1537 B G++
  2. /*
  3.  
  4. 题意:
  5. 给出完成作业需要的时间,以及它们间完成的先后关系,问是否可行,可行输出每个作业的开始时间
  6.  
  7. 差分约束:
  8.  
  9. 有n个作业,第 i 个作业所需的时间是 a[i];
  10.  
  11. SAS u v 表示 v开始后 u 才能开始;f(u)>=f(v);
  12.  
  13. SAF u v 表示 v结束后 u 才能开始;f(u)+a[u]>=f(v);
  14.  
  15. FAF u v 表示 v结束后 u 才能结束;f(u)+a[u]>=f(v)+a[v];
  16.  
  17. FAS u v 表示 v开始后 u 才能结束;f(u)>=f(v)+a[v]
  18.  
  19. 这里使用bellman_ford算法,建立反向边,求最长路径
  20.  
  21. */
  22. #include<stdio.h>
  23. #include<string.h>
  24. #define N 1005
  25. #define inf 0x7ffffff
  26. struct node{
  27. int u,v,w;
  28. }edge[*N];
  29. int d[N];
  30. int a[N];
  31. int n,edgenum;
  32. bool bellman_ford()
  33. {
  34. memset(d,,sizeof(d));
  35. bool flag=true;
  36. for(int i=;i<=n;i++){
  37. if(!flag) break;
  38. flag=false;
  39. for(int j=;j<edgenum;j++){
  40. if(d[edge[j].v]<d[edge[j].u]+edge[j].w){
  41. d[edge[j].v]=d[edge[j].u]+edge[j].w;
  42. flag=true;
  43. }
  44. }
  45. }
  46. return flag; //如果执行n次后还能松弛证明有正权环
  47. }
  48. int main(void)
  49. {
  50. char opr[];
  51. int x,y;
  52. int k=;
  53. while(scanf("%d",&n),n)
  54. {
  55. edgenum=;
  56. for(int i=;i<=n;i++)
  57. scanf("%d",&a[i]);
  58. while(scanf("%s",opr)){
  59. if(strcmp(opr,"#")==) break;
  60. scanf("%d%d",&x,&y);
  61. edge[edgenum].u=y;
  62. edge[edgenum].v=x;
  63. if(strcmp(opr,"SAS")==){
  64. edge[edgenum].w=;
  65. }
  66. if(strcmp(opr,"SAF")==){
  67. edge[edgenum].w=a[y];
  68. }
  69. if(strcmp(opr,"FAS")==){
  70. edge[edgenum].w=-a[x];
  71. }
  72. if(strcmp(opr,"FAF")==){
  73. edge[edgenum].w=a[y]-a[x];
  74. }
  75. edgenum++;
  76. }
  77. printf("Case %d:\n",k++);
  78. if(bellman_ford()) puts("impossible");
  79. else{
  80. for(int i=;i<=n;i++)
  81. printf("%d %d\n",i,d[i]);
  82. }
  83. printf("\n");
  84. }
  85. return ;
  86. }

再贴一个SPFA的:

  1. //218MS 456K 1791 B G++
  2. #include<iostream>
  3. #include<vector>
  4. #include<queue>
  5. #define N 1005
  6. #define inf 0x7ffffff
  7. using namespace std;
  8. struct node{
  9. int v,w;
  10. node(int a,int b){
  11. v=a;w=b;
  12. }
  13. };
  14. vector<node>V[N];
  15. int a[N];
  16. int d[N],in[N],vis[N];
  17. int n;
  18. bool spfa()
  19. {
  20. memset(in,,sizeof(in));
  21. memset(vis,,sizeof(vis));
  22. for(int i=;i<=n;i++) d[i]=-inf;
  23. queue<int>Q;
  24. Q.push();
  25. vis[]=;
  26. in[]=;
  27. d[]=;
  28. while(!Q.empty()){
  29. int u=Q.front();
  30. Q.pop();
  31. if(in[u]>n) return false;
  32. vis[u]=;
  33. int n0=V[u].size();
  34. for(int i=;i<n0;i++){
  35. int v=V[u][i].v;
  36. int w=V[u][i].w;
  37. if(d[v]<d[u]+w){
  38. d[v]=d[u]+w;
  39. if(!vis[v]){
  40. in[v]++;
  41. Q.push(v);
  42. vis[v]=;
  43. }
  44. }
  45. }
  46. }
  47. return true;
  48. }
  49. int main(void)
  50. {
  51. string opr;
  52. int x,y;
  53. int k=;
  54. while(cin>>n)
  55. {
  56. if(!n) break;
  57. for(int i=;i<=n;i++) V[i].clear();
  58. for(int i=;i<=n;i++){
  59. cin>>a[i];
  60. V[].push_back(node(i,));
  61. }
  62. while(cin>>opr){
  63. if(opr=="#") break;
  64. cin>>x>>y;
  65. if(opr=="SAS") V[y].push_back(node(x,));
  66. if(opr=="SAF") V[y].push_back(node(x,a[y]));
  67. if(opr=="FAS") V[y].push_back(node(x,-a[x]));
  68. if(opr=="FAF") V[y].push_back(node(x,a[y]-a[x]));
  69. }
  70. cout<<"Case "<<k++<<":"<<endl;
  71. if(!spfa()) cout<<"impossible"<<endl;
  72. else{
  73. for(int i=;i<=n;i++)
  74. cout<<i<<" "<<d[i]<<endl;
  75. }
  76. cout<<endl;
  77. }
  78. return ;
  79. }

hdu 1534 Schedule Problem (差分约束)的更多相关文章

  1. HDOJ 1534 Schedule Problem 差分约束

    差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...

  2. HDU 3666 THE MATRIX PROBLEM (差分约束)

    题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...

  3. hdu 1531 king(差分约束)

    King Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)

    You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...

  5. HDU3666-THE MATRIX PROBLEM(差分约束-不等式解得存在性判断 对数转化)

    You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...

  6. hdu 1384 Intervals (差分约束)

    Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...

  7. hduTHE MATRIX PROBLEM(差分约束)

    题目请戳这里 题目大意:给一个n*m的矩阵,求是否存在这样两个序列:a1,a2...an,b1,b2,...,bm,使得矩阵的第i行乘以ai,第j列除以bj后,矩阵的每一个数都在L和U之间. 题目分析 ...

  8. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

  9. ZOJ 1455 Schedule Problem(差分约束系统)

    // 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...

随机推荐

  1. SpringBoot学习13:springboot异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)

    问题:使用@ExceptionHandle注解需要在每一个controller代码里面都添加异常处理,会咋成代码冗余 解决方法:新建一个全局异常处理类,添加@ControllerAdvice注解即可 ...

  2. Oracle 的jdbc方法

    package com.swift.jdbc_oracle; import java.sql.CallableStatement; import java.sql.Connection; import ...

  3. Linux - bashrc之alias

    1. cd ~ 2. touch .bashrc // 若该文件不存在的话 3. vim .bashrc ----------------复制粘贴如下文本--------------- # alias ...

  4. CentOS 6.5通过yum安装 MySQL-5.5

    1.安装mysql-5.5的yum源 rpm -ivh http://repo.mysql.com/yum/mysql-5.5-community/el/6/x86_64/mysql-communit ...

  5. 想学习一下node.js,重新安装配置了node

    根据这个网站上的教程安装配置的,还不错一次就成功了.觉得安装没什么,就是配置路径的时候容易错. http://www.runoob.com/nodejs/nodejs-install-setup.ht ...

  6. 在Ubuntu下安装gcc编译器+测试

    1.输入命令: sudo apt-get install gcc libc6-dev 2.创建文件hello.c使用命令: touch hello.c 3.在hello.c中写入:  #include ...

  7. 小明的存钱计划 南阳acm54

    小明的存钱计划 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 小明的零花钱一直都是自己管理.每个月的月初妈妈给小明300元钱,小明会预算这个月的花销,并且总能做到实际花 ...

  8. C——可变参数

    1.要学可变参数,需要先了解C编译器对栈的管理 做个实验可以得到 #include <stdio.h> void func(int a, char b, int c, int d) { i ...

  9. POJ:2100-Graveyard Design(尺取)

    Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 8504 Accepted: 2126 Cas ...

  10. PHP.15-mysqli

    从PHP5.0开始可以使用mysql(i), 是一个面向对象的技术(新加功能都会以对象形式添加) i:表示改进,1. 功能增加了, 2,效率大大增加(以后的PHP项目改成mysqli),3,更稳定 m ...