hdu 1534(差分约束)
Schedule Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1715 Accepted Submission(s): 757
Special Judge
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.
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
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.
2
3
4
SAF 2 1
FAF 3 2
#
3
1
1
1
SAF 2 1
SAF 3 2
SAF 1 3
#
0
1 0
2 2
3 1
Case 2:
impossible
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int INF = ;
const int N = ;
struct Edge{
int v,w,next;
}edge[];
int head[N];
int n,tot;
int val[N];
void init(){
memset(head,-,sizeof(head));
tot = ;
}
void addEdge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u] = k++;
}
int low[N],time[N];
bool vis[N];
int spfa(int s){
for(int i=;i<=n;i++){
vis[i] = false;
low[i] = -INF;
time[i] = ;
}
low[s] = ;
time[s]++;
queue<int> q;
q.push(s);
int num = ((int)sqrt(n)+); ///改成根号 n 可以AC...
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(low[v]<low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(time[v]++>num) return ;
}
}
}
}
return ;
}
int main(){
int t = ;
while(scanf("%d",&n)!=EOF,n){
init();
int MAX = -;
for(int i=;i<=n;i++){
scanf("%d",&val[i]);
}
char str[];
int super = ;
while(scanf("%s",str)){
if(strcmp(str,"#")==) break;
int a,b;
scanf("%d%d",&a,&b);
if(strcmp(str,"SAF")==){
addEdge(b,a,val[b],tot);
}else if(strcmp(str,"FAF")==){
addEdge(b,a,-(val[a]-val[b]),tot);
}else if(strcmp(str,"FAS")==){
addEdge(b,a,-val[a],tot);
}else{
addEdge(b,a,,tot);
}
}
for(int i=;i<=n;i++){
addEdge(super,i,,tot);
}
printf("Case %d:\n",t++);
if(spfa(super)){
for(int i=;i<=n;i++){
printf("%d %d\n",i,low[i]);
}
}else{
printf("impossible\n");
}
printf("\n");
}
return ;
}
hdu 1534(差分约束)的更多相关文章
- hdu 1534(差分约束+spfa求最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...
- hdu 1531(差分约束)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...
- I - 动物狂想曲 HDU - 6252(差分约束)
I - 动物狂想曲 HDU - 6252 雷格西桑和路易桑是好朋友,在同一家公司工作.他们总是一起乘地铁去上班.他们的路线上有N个地铁站,编号从1到N.1站是他们的家,N站是公司. 有一天,雷格西桑起 ...
- hdu 4598 差分约束
思路:首先就是判断是否有奇环,若存在奇环,则输出No. 然后用差分约束找是否符合条件. 对于e(i,j)属于E,并且假设顶点v[i]为正数,那么v[i]-v[j]>=T--->v[j]-v ...
- hdu 3666(差分约束,手动栈解决超时问题)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1364(差分约束)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12056 Accepted: 4397 Description ...
- hdu 3440 差分约束
看完题目第一遍,感觉很简单.当写完程序跑测试用例的时候,发现第二个总是过不了,然后好好研究了一下测试用例,才知道原来不是程序有问题,而是我的建图方式错了.对于这些无序的点,如果高的在右边,不等式是di ...
- hdu 3440(差分约束好题)
House Man Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 1534 Schedule Problem (差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Uva 294 Divisors(唯一分解定理)
题意:求区间内正约数最大的数. 原理:唯一分解定义(又称算术基本定理),定义如下: 任何一个大于1的自然数 ,都可以唯一分解成有限个质数的乘积 ,这里 均为质数,其诸指数 是正整数.这样的分解称 ...
- eclipse安装问题
eclipse安装之前需要安装JDK. 注意:eclipse和JDK需要一致,如都是64位或者都是32位. 不然会报错.
- 页面加载时给的子元素的第一个元素加class
HTML代码: <div id="xiao"> <ul> <li></li> </ul> </div> js ...
- Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON
1.前言 对于优化SQL语句或存储过程,以前主要是用如下语句来判断具体执行时间,但是SQL环境是复杂多变的,下面语句并不能精准判断性能是否提高:如果需要精确知道CPU.IO等信息,就无能为力了. ), ...
- Intellij IDEA 系统路径配置
在使用IDEA启动Tomcat的时候,会读取系统路径,默认路径可能不是我们想要的,可以修改 C:\MyPrograms\IntelliJ IDEA 14.0.1\bin\idea.properties ...
- hihocoder 1465 循环串匹配问题(后缀自动机)
后缀自动机感觉好万能 tries图和ac自动机能做的,后缀自动机很多也都可以做 这里的循环匹配则是后缀自动机能做的另一个神奇功能 循环匹配意思就是S是abba, T是abb 问'abb', 'bba' ...
- SPOJ Repeats(后缀数组+RMQ-ST)
REPEATS - Repeats no tags A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...
- 【题解】HNOI2017大佬
哎……做了几个小时最后还是没能想到怼大佬的合法性到底怎么搞.写暴力爆搜感觉复杂度爆炸就没敢写 bfs / dfs 一类,后来发现在种种的约束条件下(远小于所给的 \(n, m\))复杂度完全是可以承受 ...
- [POJ1631]Bridging signals
题目大意:不知,根据样例猜测为最长上升子序列(竟然还对了) 题解:$O(n log_2 n)$,求二维偏序,(q为存答案的序列,a存原序列,len为答案) for(int i = 1; i <= ...
- 对web开发从业者的发展方向的思考
最近在读子柳的<淘宝技术这十年>,“牛P列传”这一章中介绍了很多淘宝技术发展史上做出重要贡献的“牛P人物”(阿里的技术岗按能力分级,从P1~P10). 读到采访小马的这一段: 子柳:畅想一 ...