差分约数:

求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c)

Schedule Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1503    Accepted Submission(s): 647

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
 

/* ***********************************************
Author :CKboss
Created Time :2015年07月29日 星期三 16时20分17秒
File Name :HDOJ1534.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int maxn=5000; int n;
int w[maxn]; struct Edge
{
int to,next,cost;
}edge[maxn]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void Add_Edge(int u,int v,int c)
{
edge[Size].to=v;
edge[Size].cost=c;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} void SAF(int u,int v)
{
Add_Edge(v,u,w[v]);
} void SAS(int u,int v)
{
Add_Edge(v,u,0);
} void FAF(int u,int v)
{
Add_Edge(v,u,w[v]-w[u]);
} void FAS(int u,int v)
{
Add_Edge(v,u,-w[u]);
} /// spfa longest road int dist[maxn],cq[maxn];
bool inq[maxn]; bool spfa()
{
memset(dist,0xcf,sizeof(dist));
memset(cq,0,sizeof(cq));
memset(inq,false,sizeof(inq)); dist[0]=0;
queue<int> q;
inq[0]=true; q.push(0); while(!q.empty())
{
int u=q.front(); q.pop(); for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(dist[v]<dist[u]+edge[i].cost)
{
dist[v]=dist[u]+edge[i].cost;
if(!inq[v])
{
inq[v]=true;
cq[v]++;
if(cq[v]>=n) return false;
q.push(v);
}
}
} inq[u]=false;
} return true;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int cas=1;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++) scanf("%d",w+i); init(); char cmd[20];
while(scanf("%s",cmd)!=EOF)
{
if(cmd[0]=='#') break; int u,v;
scanf("%d%d",&u,&v); if(strcmp(cmd,"SAF")==0)
{
SAF(u,v);
}
else if(strcmp(cmd,"FAF")==0)
{
FAF(u,v);
}
else if(strcmp(cmd,"FAS")==0)
{
FAS(u,v);
}
else if(strcmp(cmd,"SAS")==0)
{
SAS(u,v);
}
} for(int i=1;i<=n;i++) Add_Edge(0,i,0);
bool fg=spfa(); printf("Case %d:\n",cas++); if(fg==false)
{
puts("impossible");
}
else
{
int mx=0;
for(int i=1;i<=n;i++)
{
if(dist[i]<mx) mx=dist[i];
}
for(int i=1;i<=n;i++)
{
printf("%d %d\n",i,dist[i]-mx);
}
}
putchar(10);
} return 0;
}

HDOJ 1534 Schedule Problem 差分约束的更多相关文章

  1. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. 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 ...

  3. 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 ...

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

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

  5. hduTHE MATRIX PROBLEM(差分约束)

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

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

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

  7. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  8. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

  9. 【HDOJ1534】【差分约束+SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=1534 Schedule Problem Time Limit: 2000/1000 MS (Java/Other ...

随机推荐

  1. It运维项目整理

    String.prototype.toWeek=function(){ var date = new Date(this); var week = ""; switch (date ...

  2. YYH的王国(NOIP模拟赛Round 6)

    题目描述 YYH拥有一个有n个城市的国家,编号为1~n.其中城市i到城市j的路径长度为i和j的最小公倍数.现在YYH想建一些高速公路,使得任意两座城市都能通过高速公路直接或间接到达.建造一条高速公路的 ...

  3. Linux下使用system()函数一定要谨慎

    转载自:http://my.oschina.net/renhc/blog/53580   linux尽量避免使用system. 曾经的曾经,被system()函数折磨过,之所以这样,是因为对syste ...

  4. shell脚本中各个参数的意思

    文件表达式-e filename 如果 filename存在,则为真-d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真-L ...

  5. HDU 1698.Just a Hook-线段树(成段替换、输出总和tree[1])

    HDU1698.Just a Hook 这个题是最最基础的成段更新的线段数的题目,直接贴代码吧. 代码: #include<iostream> #include<cstring> ...

  6. thymeleaf初步使用

    thymeleaf模板引擎初步使用 #thymelea模板配置 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffi ...

  7. FZU-2271 X(Floyd)

     Problem 2271 X Accept: 303    Submit: 1209Time Limit: 1500 mSec    Memory Limit : 32768 KB  Problem ...

  8. zzc种田

    题目背景 可能以后 zzc就去种田了. 题目描述 田地是一个巨大的矩形,然而zzc 每次只能种一个正方形,而每种一个正方形时zzc所花的体力值是正方形的周长,种过的田不可以再种,zzc很懒还要节约体力 ...

  9. 替换Ubuntu默认的登录背景

    Ubuntu默认的登录背景看起来还是比较高贵的,但是作为一个爱折腾的人,当然要换成自己喜欢的背景图了.一开始跟着百度走,进了不少坑,最后还是自己走出来的.先上一个成品照 百度得到的答案几乎都是安装ub ...

  10. ffmpeg yasm not found, use --disable-yasm for a crippled build

    yasm是汇编编译器,因为ffmpeg中为了提高效率用到了汇编指令,比如MMX和SSE.解决这个问题方面有两个: 1.在网上下载一个yasm.exe并安装在mingw/bin下面,编译代码时你注意看, ...