Zoj 1455 Schedule Problem difference Constraint

Source: Internet
Author: User

Set d [I] to the start time of the I-th job:

Fas a B: d [a] + time [a]> = d [B]

Faf a B: d [a] + time [a]> = d [B] + time [B]

Saf a B: d [a]> = d [B] + time [B]

Sas a B: d [a]> = d [B]

Also, d [I]> = 0, that is, d [I]-d [0]> = 0

Start searching with 0 as the Source Vertex, and 0 as the supersource Vertex

 

# Include <iostream> # include <queue> # include <cstring> # include <cstdio> using namespace STD; const int max = 100000; const int n = 10000; const int INF = 1000000000; struct node {int V; int cost; int next ;}; node [Max]; int CNT [N]; int adj [N]; bool in_q [N]; int d [N], times [N]; int N; int size; void add_edge (int u, int V, int cost) {node [size]. V = V; node [size]. cost = cost; node [size]. next = adj [u]; adj [u] = size ++;} Bool spfa () {queue <int> q; memset (CNT, 0, sizeof (CNT); memset (in_q, false, sizeof (in_q )); for (INT I = 0; I <= N; I ++) d [I] =-INF; d [0] = 0; q. push (0); in_q [0] = true; int U, V, W; while (! Q. Empty () {u = Q. Front (); q. Pop (); in_q [u] = false; For (INT I = adj [u]; I! =-1; I = node [I]. next) {v = node [I]. v; W = node [I]. cost; If (d [v] <D [u] + W) {d [v] = d [u] + W; If (! In_q [v]) {in_q [v] = true; q. push (V); If (++ CNT [v]> N) return false ;}}}return true;} int main () {char C [20]; int A, B; INT cases = 1; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; size = 0; For (INT I = 0; I <= N; I ++) adj [I] =-1; for (INT I = 1; I <= N; I ++) scanf ("% d", & Times [I]); while (1) {getchar (); scanf ("% s", c); If (strcmp (C, "#") = 0) break; else if (strcmp (C, "fas") = 0) {scanf ("% d", & A, & B); add_edge (B,, -Times [a]);} else if (strcmp (C, "FAF") = 0) {scanf ("% d", & A, & B ); add_edge (B, A, times [B]-Times [a]);} else if (strcmp (C, "SAF") = 0) {scanf ("% d", & A, & B); add_edge (B, A, times [B]);} else if (strcmp (C, "SAS") = 0) {scanf ("% d", & A, & B); add_edge (B, A, 0 );}} for (INT I = 1; I <= N; I ++) add_edge (0, I, 0); printf ("case % d: \ n ", cases ++); If (spfa () {for (INT I = 1; I <= N; I ++) printf ("% d \ n ", i, d [I]);} elseprintf ("impossible \ n"); 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.