HDU 1535--Schedule Problem【差分约束】
2015-07-24 09:29
267 查看
Schedule Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1499 Accepted Submission(s): 644
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
分析:有n个作业,第 i 个作业所需的时间是 a[i];
f(i)代表第 i 个作业开始的时间。
现在给定一些约束条件:
SAS u v 表示 v开始后 u 才能开始;f(u)>=f(v);
SAF u v 表示 v结束后 u 才能开始;f(u)>=f(v)+a[v];
FAF u v 表示 v结束后 u 才能结束;f(u)+a[u]>=f(v)+a[v];
FAS u v 表示 v开始后 u 才能结束;f(u)+a[u]>=f(v);
约束条件很明显了,差分约束,我使用的是SPFA;
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 1000100 #define INF 0x3f3f3f3f using namespace std; struct node{ int u, v, w, next; }; int head[MAXN], cnt, N; int du[MAXN]; int time[MAXN], dist[MAXN]; bool vis[MAXN]; node edge[MAXN]; void init(){ cnt = 0; memset(head, -1, sizeof(head)); } void add(int u, int v, int w){ node edge1 = {u, v, w, head[u]}; edge[cnt] = edge1; head[u] = cnt++; } bool SPFA(){ queue<int>q; for(int i = 0; i <= N; ++i){ vis[i] = 0; du[i] = 0; dist[i] = -INF;//最长路初始化为- INF } dist[0] = 0; vis[0] = 1; q.push(0); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0;//不是入队一次,是多次入队,和BFS有不同 for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; int w = edge[i].w; if(dist[v] < dist[u] + w){//求最长路 dist[v] = dist[u] + w; if(!vis[v]){ vis[v] = 1; du[v] ++; if(du[v] > N) return 0; q.push(v); } } } } return 1; } int main (){ int k = 1; while(scanf("%d", &N),N){ init(); for(int i = 1;i <=N; ++i){ scanf("%d", &time[i]); add(0, i , 0); } char str[10]; while(scanf("%s", str)){ int u, v; if(!strcmp(str, "#")) break; scanf("%d%d", &u, &v); if(!strcmp(str, "SAS")) add(v, u, 0); if(!strcmp(str, "SAF")) add(v, u, time[v]); if(!strcmp(str, "FAF")) add(v, u, time[v] - time[u]); if(!strcmp(str, "FAS")) add(v, u, -time[u]); } printf("Case %d:\n", k++); if(SPFA()){ for(int i = 1; i <= N; ++i) printf("%d %d\n", i ,dist[i]); } else printf("impossible\n"); printf("\n"); } return 0; }
相关文章推荐
- 链表,配合critical section
- 一个用户加入多个组
- 【system】利用Diskpart命令(cmd)解决装机分区格式不对,增大C盘空间问题
- 应用程序配置文件中的连接字符串不包含必需的 providerName 特性
- 事情高大谈话(智能家居)技术和平台
- Android开发学习笔记二Android Studio
- 搭建高可用的MongoDB集群(上):MongoDB的配置与副本集----- 个人记录
- 简单的新闻列表鼠标移入背景变色功能
- 图像处理------快速均值模糊(Box Blur)
- 图像处理------快速均值模糊(Box Blur)
- Linux下进程绑定多CPU运行
- 用结构体变量和指向结构体变量的指针构成链表
- Spring读取配置文件,获取bean的几种方式
- 图像处理------快速均值模糊(Box Blur)
- 使用su后如何返回原来的用户
- UVA 810 - A Dicey Problem(BFS)
- Log4Net使用指南(转)
- 图论:匹配与覆盖+独立集 团与支配集
- 图像处理------快速均值模糊(Box Blur) 分类: 视频图像处理 2015-07-24 09:28 30人阅读 评论(0) 收藏
- hihoCoder #1051 补提交卡