HDU1534 Schedule Problem 【差分约束系统】
来源:互联网 发布:算法的时间复杂度 编辑:程序博客网 时间:2024/05/22 00:18
Schedule Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1283 Accepted Submission(s): 534
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
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.
A blank line should appear following the output for each project.
Sample Input
3234SAF 2 1FAF 3 2#3111SAF 2 1SAF 3 2SAF 1 3#0
Sample Output
Case 1:1 02 23 1Case 2:impossible
Source
Asia 1996, Shanghai (Mainland China)
题意:给定n个任务的执行时间以及相互之间开始执行时的顺序,求如何安排才能使用时最少。
题解:用SPFA求差分约束系统的最长路,要判断是否有“负环”。
#include <stdio.h>#include <string.h>#include <queue>#define maxn 1002#define maxm maxn * maxn#define inf 0x3f3f3f3fusing std::queue;int head[maxn], t[maxn], id;struct Node{ int to, w, next;} E[maxm];int dist[maxn], out[maxn], in[maxn];bool vis[maxn];void addEdge(int u, int v, int w){ E[id].to = v; E[id].w = w; E[id].next = head[u]; head[u] = id++;}bool SPFA(int n){ int i, u, v, tmp; for(i = 0; i <= n; ++i){ vis[i] = out[i] = 0; dist[i] = -inf; } u = 0; vis[u] = 1; dist[u] = 0; queue<int> Q; Q.push(u); while(!Q.empty()){ u = Q.front(); Q.pop(); vis[u] = 0; if(++out[u] > n) return false; for(i = head[u]; i != -1; i = E[i].next){ tmp = dist[u] + E[i].w; v = E[i].to; if(tmp > dist[v]){ dist[v] = tmp; if(!vis[v]){ vis[v] = 1; Q.push(v); } } } } return true;}int main(){ int n, cas = 1, u, v, i; char str[5]; while(scanf("%d", &n), n){ for(i = 1; i <= n; ++i) scanf("%d", &t[i]); memset(head, -1, sizeof(head)); id = 0; while(scanf("%s", str), str[0] != '#'){ scanf("%d%d", &u, &v); if(!strcmp(str, "SAS")) addEdge(v, u, 0); else if(!strcmp(str, "SAF")) addEdge(v, u, t[v]); else if(!strcmp(str, "FAS")) addEdge(v, u, -t[u]); else addEdge(v, u, t[v] - t[u]); } printf("Case %d:\n", cas++); for(i = 1; i <= n; ++i) addEdge(0, i, 0); if(!SPFA(n)){ printf("impossible\n\n"); continue; } for(i = 1; i <= n; ++i) printf("%d %d\n", i, dist[i]); printf("\n"); } return 0;}
0 0
- HDU1534 Schedule Problem 【差分约束系统】
- hdu1534 Schedule Problem (spfa差分约束)
- HDU1534 Schedule Problem【SPFA】【差分约束】
- hdu1534 Schedule Problem--单源最短路径&差分约束
- (hdu1534)Schedule Problem(差分约束+SPFA算法)
- hdu1534差分约束
- hdu1534 差分约束
- hdu1534 差分约束系统
- HDU 1534 Schedule Problem(差分约束系统)
- Schedule Problem 【ZJU1455】 差分约束
- HDU--1534[Schedule Problem] 差分约束
- HDU 1534 Schedule Problem 差分约束
- HDU 1534 Schedule Problem 差分约束
- 【差分约束】 HDOJ 1534 Schedule Problem
- 【HDU】1534 Schedule Problem 差分约束
- HDU 1535--Schedule Problem【差分约束】
- HDOJ 1534 Schedule Problem 差分约束
- HDU1534 Schedule Problem
- 拼图游戏的步骤求解
- Matlab GUI界面 (转)
- Linux下实现自动设置SSH代理
- HDFS Snapshots
- NULL指针
- HDU1534 Schedule Problem 【差分约束系统】
- 10-0. 说反话 (20)
- [LeetCode]-Spiral Matrix I&II 螺旋矩阵
- 【Android】Android开源项目分类汇总
- OpenGL教程
- 10-1. 在字符串中查找指定字符(15)
- poj 2762 Going from u to v or from v to u?(tarjan+拓扑排序)
- Codeforces #261 (Div. 2) D. Pashmak and Parmida's problem(数据结构:离散化+线段树+逆序数+二分查找)
- linux开发的一些记录