【差分约束/超级源点/汇点】 Schedule Problem HDU图论01最短路

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)

#include <bits/stdc++.h>

using namespace std;

const int mn = 100010;
const int inf = 0x3f3f3f3f;

int N, t[mn];

int to[5 * mn], cost[5 * mn], nx[5 * mn], fr[5 * mn];
int d[mn];
int ad[mn];
bool vis[mn];

int B;

void add ( int a, int b, int c )
{
	B++;
	to[B] = b;
	cost[B] = c;
	nx[B] = fr[a];
	fr[a] = B;
}

void SPFA (int st)
{
	queue<int>q;

	memset ( ad, 0, sizeof ( ad ) );
	memset ( vis, 0, sizeof ( vis ) );
	for ( int i = 0; i <= N + 1; i++ )
		d[i] = -inf;
	d[st] = 0;

	q.push ( st );
	vis[st] = 1;

	while ( !q.empty() )
	{
		int p = q.front();
		q.pop();
		vis[p] = 0;

		ad[p]++;

		if ( ad[p] > ( N + 10 ) ) //从队列中取出的次数
		{
			printf ( "impossible\n" );
			return;  //存在负环
		}

		for ( int i = fr[p]; i != -1; i = nx[i] )
		{
			if ( d[to[i]] < d[p] + cost[i] )
			{
				d[to[i]] = d[p] + cost[i];
				if ( !vis[to[i]] )
				{
					q.push ( to[i] );
					vis[to[i]] = 1;
				}
			}
		}
	}
	for ( int i = 1; i <= N; i++ )
		printf ( "%d %d\n", i, d[i] );
	return;
}

int main()
{
	int T = 0;
	bool flag = 0;
	while ( ~scanf ( "%d", &N ) && N )
	{
		if ( flag ) printf ( "\n" );
		flag = 1;

		B = -1;
		memset ( fr, -1, sizeof ( fr ) );

		for (int i = 1; i <= N; i++ )
			scanf ( "%d", &t[i] );
		
        //超级源点
		for (int i = 1; i <= N + 1; i ++)
			add (0, i, 0);
        //超级汇点
		for (int i = 0; i <= N; i ++)
			add (i, N + 1, t[i]);

		string ch;
		while (cin >> ch && ch != "#" )
		{

			int a, b;
			scanf ( "%d%d", &a, &b );
			if ( ch == "FAS" )
				add ( b, a, -t[a] );
			else if ( ch == "FAF" )
				add ( b, a, t[b] - t[a] );
			else if ( ch == "SAF" )
				add ( b, a, t[b] );
			else if ( ch == "SAS" )
				add ( b, a, 0 );
		}

		printf ( "Case %d:\n", ++T );
		SPFA (0);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/80291139