Hdu1534 schedule problem [difference constraint system]

Source: Internet
Author: User

Schedule problemtime limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 1283 accepted submission (s): 534 Special Judge
Problem descriptiona project can be divided into several parts. each part shoshould be completed continuously. this means if a part shocould take 3 days, we shocould 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 shoshould 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.
Inputthe 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 shoshould 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
 
Outputoutput shocould be a list of lines, each line should des a part number and the time it shocould start. time shocould be a non-negative integer, And the start time of first part shocould be 0. if there is no answer for the problem, you shoshould give a non-line output containing "impossible ".

A blank line shoshould appear following the output for each project.

 
Sample Input
3234SAF 2 1FAF 3 2#3111SAF 2 1SAF 3 2SAF 1 3#0
 
Sample output
Case 1:1 02 23 1Case 2:impossible
 
Sourceasia 1996, Shanghai. Question: If spfa is used to calculate the longest path of the differential constraint system, it is necessary to determine whether there is a "negative ring ".

#include <stdio.h>#include <string.h>#include <queue>#define maxn 1002#define maxm maxn * maxn#define inf 0x3f3f3f3fusing std::queue;int head[maxn], t[maxn], id;struct Node{    int to, w, next;} E[maxm];int dist[maxn], out[maxn], in[maxn];bool vis[maxn];void addEdge(int u, int v, int w){    E[id].to = v; E[id].w = w;    E[id].next = head[u]; head[u] = id++;}bool SPFA(int n){    int i, u, v, tmp;    for(i = 0; i <= n; ++i){        vis[i] = out[i] = 0; dist[i] = -inf;    }    u = 0; vis[u] = 1; dist[u] = 0;    queue<int> Q; Q.push(u);    while(!Q.empty()){        u = Q.front(); Q.pop(); vis[u] = 0;        if(++out[u] > n) return false;        for(i = head[u]; i != -1; i = E[i].next){            tmp = dist[u] + E[i].w;            v = E[i].to;            if(tmp > dist[v]){                dist[v] = tmp;                if(!vis[v]){                    vis[v] = 1; Q.push(v);                }            }        }    }    return true;}int main(){    int n, cas = 1, u, v, i;    char str[5];    while(scanf("%d", &n), n){        for(i = 1; i <= n; ++i)            scanf("%d", &t[i]);        memset(head, -1, sizeof(head)); id = 0;        while(scanf("%s", str), str[0] != '#'){            scanf("%d%d", &u, &v);            if(!strcmp(str, "SAS")) addEdge(v, u, 0);            else if(!strcmp(str, "SAF")) addEdge(v, u, t[v]);            else if(!strcmp(str, "FAS")) addEdge(v, u, -t[u]);            else addEdge(v, u, t[v] - t[u]);        }        printf("Case %d:\n", cas++);        for(i = 1; i <= n; ++i)            addEdge(0, i, 0);        if(!SPFA(n)){            printf("impossible\n\n");            continue;        }        for(i = 1; i <= n; ++i)            printf("%d %d\n", i, dist[i]);        printf("\n");    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.