hdu 1534 Schedule Problem (差分约束)
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
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
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
//0MS 248K 1537 B G++
/* 题意:
给出完成作业需要的时间,以及它们间完成的先后关系,问是否可行,可行输出每个作业的开始时间 差分约束: 有n个作业,第 i 个作业所需的时间是 a[i]; SAS u v 表示 v开始后 u 才能开始;f(u)>=f(v); SAF u v 表示 v结束后 u 才能开始;f(u)+a[u]>=f(v); FAF u v 表示 v结束后 u 才能结束;f(u)+a[u]>=f(v)+a[v]; FAS u v 表示 v开始后 u 才能结束;f(u)>=f(v)+a[v] 这里使用bellman_ford算法,建立反向边,求最长路径 */
#include<stdio.h>
#include<string.h>
#define N 1005
#define inf 0x7ffffff
struct node{
int u,v,w;
}edge[*N];
int d[N];
int a[N];
int n,edgenum;
bool bellman_ford()
{
memset(d,,sizeof(d));
bool flag=true;
for(int i=;i<=n;i++){
if(!flag) break;
flag=false;
for(int j=;j<edgenum;j++){
if(d[edge[j].v]<d[edge[j].u]+edge[j].w){
d[edge[j].v]=d[edge[j].u]+edge[j].w;
flag=true;
}
}
}
return flag; //如果执行n次后还能松弛证明有正权环
}
int main(void)
{
char opr[];
int x,y;
int k=;
while(scanf("%d",&n),n)
{
edgenum=;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
while(scanf("%s",opr)){
if(strcmp(opr,"#")==) break;
scanf("%d%d",&x,&y);
edge[edgenum].u=y;
edge[edgenum].v=x;
if(strcmp(opr,"SAS")==){
edge[edgenum].w=;
}
if(strcmp(opr,"SAF")==){
edge[edgenum].w=a[y];
}
if(strcmp(opr,"FAS")==){
edge[edgenum].w=-a[x];
}
if(strcmp(opr,"FAF")==){
edge[edgenum].w=a[y]-a[x];
}
edgenum++;
}
printf("Case %d:\n",k++);
if(bellman_ford()) puts("impossible");
else{
for(int i=;i<=n;i++)
printf("%d %d\n",i,d[i]);
}
printf("\n");
}
return ;
}
再贴一个SPFA的:
//218MS 456K 1791 B G++
#include<iostream>
#include<vector>
#include<queue>
#define N 1005
#define inf 0x7ffffff
using namespace std;
struct node{
int v,w;
node(int a,int b){
v=a;w=b;
}
};
vector<node>V[N];
int a[N];
int d[N],in[N],vis[N];
int n;
bool spfa()
{
memset(in,,sizeof(in));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) d[i]=-inf;
queue<int>Q;
Q.push();
vis[]=;
in[]=;
d[]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(in[u]>n) return false;
vis[u]=;
int n0=V[u].size();
for(int i=;i<n0;i++){
int v=V[u][i].v;
int w=V[u][i].w;
if(d[v]<d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
in[v]++;
Q.push(v);
vis[v]=;
}
}
}
}
return true;
}
int main(void)
{
string opr;
int x,y;
int k=;
while(cin>>n)
{
if(!n) break;
for(int i=;i<=n;i++) V[i].clear();
for(int i=;i<=n;i++){
cin>>a[i];
V[].push_back(node(i,));
}
while(cin>>opr){
if(opr=="#") break;
cin>>x>>y;
if(opr=="SAS") V[y].push_back(node(x,));
if(opr=="SAF") V[y].push_back(node(x,a[y]));
if(opr=="FAS") V[y].push_back(node(x,-a[x]));
if(opr=="FAF") V[y].push_back(node(x,a[y]-a[x]));
}
cout<<"Case "<<k++<<":"<<endl;
if(!spfa()) cout<<"impossible"<<endl;
else{
for(int i=;i<=n;i++)
cout<<i<<" "<<d[i]<<endl;
}
cout<<endl;
}
return ;
}
hdu 1534 Schedule Problem (差分约束)的更多相关文章
- HDOJ 1534 Schedule Problem 差分约束
差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...
- HDU 3666 THE MATRIX PROBLEM (差分约束)
题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...
- hdu 1531 king(差分约束)
King Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 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 ...
- 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 ...
- hdu 1384 Intervals (差分约束)
Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...
- hduTHE MATRIX PROBLEM(差分约束)
题目请戳这里 题目大意:给一个n*m的矩阵,求是否存在这样两个序列:a1,a2...an,b1,b2,...,bm,使得矩阵的第i行乘以ai,第j列除以bj后,矩阵的每一个数都在L和U之间. 题目分析 ...
- HDU 1384 Intervals【差分约束-SPFA】
类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...
- ZOJ 1455 Schedule Problem(差分约束系统)
// 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...
随机推荐
- JS浏览器的三种弹框:
1.alert:使用alert弹框提示信息,最后都会被转化为字符串输出(因为调用了toString这个方法).比如alert(1+1)弹出的结果应该是字符串形式的“2”. 2.Confirm:在ale ...
- lintcode_93_平衡二叉树
平衡二叉树 描述 笔记 数据 评测 给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1. 您在真实的面试中是否遇到过 ...
- java 基础面试
1. &和&&的区别 答: &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and) 2.int 和 Integer 有什么区别 答: Ja ...
- webpack最小化lodash
lodash作为一个比较常用的前端开发工具集,在使用webpack进行vendor分离的实践中,会遇到将整个lodash文件分离到vendor.js的问题.这样会使vendor.js文件变得特别大. ...
- linux防火墙的管理和策略控制
iptables 一:IPtables防火墙的简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 ...
- dede后台添加优酷等视频iframe链接时被替换成了图片
添加文章时 添加优酷视频 :<iframe height=498 width=510 src='http://player.youku.com/embed/XNDAzNTAzODE4OA==' ...
- 内置函数系列之 sorted排序
sorted排序函数语法: sorted(可迭代对象,key=函数(默认为None),reverse=False) 将可 迭代对象的每一个元素传进key后面的函数中,根据函数运算的结果(返回值)进行排 ...
- 在唯一密钥属性“fileExtension”设置为“.”时,无法添加类型为“mimeMap”的重复集合项
在ASP.NET 网站的配置文件中添加了MIME类型,但是运行网站后在IIS上和页面上提示"在唯一密钥属性“fileExtension”设置为“.woff”时,无法添加类型为“mimeMap ...
- 关于 PHP 程序员技术职业生涯规划
原文地址:http://rango.swoole.com/archives/570 看到很多 PHP 程序员职业规划的文章,都是直接上来就提 Linux.PHP.MySQL.Nginx.Redis.M ...
- strak组件(9):关键字搜索
效果图: 在列表视图函数增加搜索功能. 新增函数 def get_search_list(self) 钩子方法,获取搜索条件 一.strak组件 strak/service/core_func.py ...