HDOJ 1534 Schedule Problem 差分约束

差分约数:

求满足不等式条件的尽量小的值---->求最长路---->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

Asia 1996, Shanghai (Mainland China)

/* ***********************************************
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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 10:51:09

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

HDU1534 Schedule Problem 差分约束

囧,还是暴露出了对差分约束理解的不透彻... 一开始根据开始和结束的关系建边,然后建立一个超级源点,连接每一个其他节点,先把这个点入队.本质上相当于把一开始所有的节点都入队了,然后做一遍最长路(最短路,怎么建边的怎么来),相当于把每一个点都作为起点做了一遍最短路,每个点的d取最大的那个. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include &l

hdu3666 THE MATRIX PROBLEM --- 差分约束

这要是碰上现场赛我得被搞死 从RE到TLE到WA已疯.. 这题建图没有那么直接,通过给出的不等式关系一时想不到怎么建图 所以要对题目给的条件一定程度化简,将不等式两边取对数化简得到Sa-Sb<=c的形式 要注意w取double类型 其次,这题卡时间,根据经验加剪枝: 1.出队次数>sqrt(n)则判断有负环 2.统计总的入队次数,>2n则判断有负环 一般情况下不用这个,因为不严谨 下面两个spfa都是对的,手写队列稍快一点,上面第二个剪枝效果明显 #include<iostream

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 problem is that if there exist N numbers a1, a2, … an and M numbers b1, b2, …, bm, which satisfies that each elements in row-i multiplied with ai and e

HDU 3666 THE MATRIX PROBLEM (差分约束)

题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l<=cij*(ai/bj)<=u (1<=i<=n,1<=j<=m)成立. 首先把cij先除到两边去,就变成了l'<=ai/bj<=u',由于差分约束要是的减,怎么变成减法呢?取对数呗,两边取对数得到log(l')<=log(ai)-log(bj)<=l

HDOJ 3666 THE MATRIX PROBLEM 差分约束

根据题意有乘除的关系,为了方便构图,用对数转化乘除关系为加减关系..... THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7486    Accepted Submission(s): 1914 Problem Description You have been given a matrix CN

HDOJ 3592 World Exhibition 差分约束

World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1373    Accepted Submission(s): 673 Problem Description Nowadays, many people want to go to Shanghai to visit the World Exhibiti

hdu 差分约束题集

[HDU]1384 Intervals 基础差分约束★1529 Cashier Employment 神级差分约束★★★★ 1531 King 差分约束★1534 Schedule Problem 差分约束输出一组解★3440 House Man 比较好的差分约束★★3592 World Exhibition 简单★3666 THE MATRIX PROBLEM 中等★★4274 Spy's Work [先处理出欧拉序列,然后就是差分约束了...] [POJ]1201 Intervals1275

【转载】夜深人静写算法(四)——差分约束

[转载]夜深人静写算法(四) - 差分约束  目录     一.引例       1.一类不等式组的解   二.最短路       1.Dijkstra       2.图的存储       3.链式前向星       4.Dijkstra + 优先队列       5.Bellman-Ford       6.SPFA       7.Floyd-Warshall   三.差分约束        1.数形结合        2.三角不等式        3.解的存在性        4.最大值

【HDOJ1534】【差分约束+SPFA】

http://acm.hdu.edu.cn/showproblem.php?pid=1534 Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2196    Accepted Submission(s): 994Special Judge Problem Description A project can