# Uvalive 4885 task difference Constraint

Source: Internet
Author: User

Question:

There are n tasks and M restrictions

1. task I starts at least a minutes later than task J

Indicates I-j> =

2. task I starts within a minutes of the starting time of task J

Indicates I-j <=

Q: the start time of each task. Find an arbitrary solution

Ideas:

Difference constraint. For an inequality, such:

Point U, V: constant C

Yes: U-v <= C

Then, an edge with a length of C is connected from V-> U.

If a negative ring exists, the difference constraint is not resolved. Otherwise, an arbitrary solution can be obtained.

`#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <queue>#include <string.h>using namespace std;#define inf 100000000#define N 200#define M 200005struct node{int from, to, dis, nex;}edge[M];int head[N], edgenum;void init(){memset(head, -1, sizeof head); edgenum = 0;}void add(int u, int v, int d){node E = {u, v, d, head[u]};edge[edgenum] = E;head[u] = edgenum++;}int n, m;int dis[N], inq[N], tim[N];bool spfa(){memset(tim, 0, sizeof tim);memset(inq, 0, sizeof inq);dis[0] = 0;for(int i = 1; i <= n; i++){dis[i] = inf;add(0, i, 0);}queue<int>q; q.push(0);while(!q.empty()){int u = q.front(); q.pop(); inq[u] = 0;for(int i = head[u]; ~i; i = edge[i].nex){int v = edge[i].to;if(dis[v] > dis[u] + edge[i].dis){dis[v] = dis[u] + edge[i].dis;if(!inq[v]){inq[v] = 1; tim[v]++; q.push(v);if(tim[v]>n)return false;}}}}return true;}char s[100];void eat(int x){while(x--)scanf("%s",s);}void build(){    scanf("%d", &m);init();int a, b, x, i;while(m--){eat(1);scanf("%d", &a);eat(2);if(s[0] == 'a'){eat(1);scanf("%d", &x);eat(4);scanf("%d", &b);add(a, b, -x);}else{scanf("%d", &x);eat(7);scanf("%d", &b);add(b, a, x);}add(a, b, 0);}}void solve(){build();if(spfa() == false) { puts("Impossible."); return ;}int minn = dis[1];for(int i = 2; i <= n; i++) minn = min(minn, dis[i]);minn = -minn +1;for(int i = 1; i <= n; i++)printf("%d%c", dis[i]+minn, i==n?'\n':' ');}int main() {int a, b, x, i;while(scanf("%d", &n), n){solve();}return 0;}/*22task 1 starts at least 5 minutes later than task 2task 1 starts within 5 minutes of the starting time of task 222task 1 starts at least 6 minutes later than task 2task 1 starts within 5 minutes of the starting time of task 222task 1 starts at least 5 minutes later than task 2task 1 starts within 6 minutes of the starting time of task 222task 2 starts at least 5 minutes later than task 1task 1 starts within 5 minutes of the starting time of task 222task 2 starts at least 0 minutes later than task 1task 1 starts within 0 minutes of the starting time of task 2ans:*/`

Related Keywords:

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.