您的位置:首页 > 其它

hdu1534 Schedule Problem--单源最短路径&差分约束

2016-04-02 19:04 337 查看
原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1534
一:原题内容

[align=left]Problem Description[/align]
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.
 
[align=left]Input[/align]
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

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

3
2
3
4
SAF 2 1
FAF 3 2
#
3
1
1
1
SAF 2 1
SAF 3 2
SAF 1 3
#
0

[align=left]Sample Output[/align]

Case 1:
1 0
2 2
3 1

Case 2:
impossible

二:分析理解

安排计划,有4种约束方式,给出你这些时间的n个约束..

  如果计划是可行的,求出每一件事发生的最早时间..否则输出“impossible”..

  

  ①. FAF a b a要在b完成后完成..

  ②. FAS a b a要在b开始前完成..

  ③. SAS a b a要在b开始前开始..

  ④. SAF a b a要在b结束前开始..

记s[i]为第i个问题开始的时间,a[i]是完成第i个问题的时间,则

FAS:s[u]+a[u]>=s[v]

FAF:s[u]+a[u]>=s[v]+a[v]

SAF:s[u]>=s[v]+a[v]

SAS:s[u]>=s[v]

题目要求最小值,需要转化为:

s[u]--s[v]>=-a[u]

s[u]-s[v]>=a[v]-a[u]

s[u]-s[v]>=a[v]

s[u]-s[v]>=0

三:AC代码

#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

struct Edge
{
int v;
int w;
int next;
};

Edge edge[300005];
int a[10005];
int cnt[10005];
int sta[10005];
bool visited[10005];
int dis[10005];
int head[10005];
int num;
bool flag;
int N;

void AddEdge(int u, int v, int w)
{
edge[num].v = v;
edge[num].w = w;
edge[num].next = head[u];
head[u] = num++;
}

void Spfa()
{
dis[0] = 0;
int top = 1;
sta[top] = 0;
visited[0] = 1;

while (top)
{
int u = sta[top--];
visited[u] = 0;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if (dis[v] < dis[u] + edge[i].w)
{
dis[v] = dis[u] + edge[i].w;
if (!visited[v])
{
sta[++top] = v;
visited[v] = 1;
cnt[v]++;
}

if (cnt[v] > N)
{
flag = false;
return;
}
}
}
}

}

int main()
{
int cas = 1;

while (~scanf("%d", &N) && N)
{
flag = true;
memset(visited, 0, sizeof(visited));
memset(head, -1, sizeof(head));
memset(cnt, 0, sizeof(cnt));
fill(dis, dis + 10005, -99999999);

for (int i = 1; i <= N; i++)
scanf("%d", a + i);

char ch[5];
int u, v;
scanf("%s", ch);
while (ch[0] != '#')
{
scanf("%d%d", &u, &v);
if (strcmp(ch, "FAS") == 0)
AddEdge(v, u, -a[u]);
else if (strcmp(ch, "FAF") == 0)
AddEdge(v, u, a[v] - a[u]);
else if (strcmp(ch, "SAF") == 0)
AddEdge(v, u, a[v]);
else
AddEdge(v, u, 0);

scanf("%s", ch);
}

for (int i = 1; i <= N; i++)
AddEdge(0, i, 0);

Spfa();

printf("Case %d:\n", cas++);

if (flag)
{
for (int i = 1; i <= N; i++)
printf("%d %d\n", i, dis[i]);
}
else
printf("impossible\n");

printf("\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: