Hdoj 1534 Schedule problem differential constraint

Source: Internet
Author: User


Differential approximate:

To satisfy the minimum value of the inequality condition----> the longest road---->a-b>=c----> B->a (c)


Schedule problemTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1503 Accepted Submission (s): 647
Special Judge


Problem Descriptiona project can be divided into several parts. Each part should is completed continuously. This means if a-should take 3 days, we should the use a continuous 3 days does complete it. There is four types of constrains among these parts which is 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 are start after start. Assume there is enough people involved in the projects, which means we can does any number of parts concurrently. You is to write a program to give a schedule of a given project, which have 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 should is taken to complete these parts, each time occupies one line

A list of FAS, FAF, SAF or SAS and both part number indicates a constrain of the parts

A line only contains a ' # ' indicates the end of project

Outputoutput should is a list of lines, each line includes a part number and the time it should start. Time should is a non-negative integer, and the start time of first part should is 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
3234SAF 2 1FAF 3 2#3111saf 2 1SAF 3 2SAF 1 3#0

Sample Output
Case 1:1 1Case 2:impossible

Sourceasia 1996, Shanghai (Mainland China)

/* ***********************************************author:ckbosscreated time:2015 July 29 Wednesday 16:20 17 seconds 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 roadint Dist[maxn],cq[maxn];bool inq[maxn];bool SPFA () {memset (dist,0xcf,sizeof); Dist (Memset, SiZeof (CQ)); memset (Inq,false,sizeof (INQ));d ist[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 ();p rintf ("Case%d:\n", cas++), if (Fg==false) {puts ("impossible" );} Else{int mx=0;for (int i=1;i<=n;i++) {if (DIST[I]&LT;MX) mx=dist[i];} for (int i=1;i<=n;i++) {printf ("%d%d\n",I,DIST[I]-MX);}}        Putchar (10);} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 1534 Schedule problem differential constraint

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.