【差分约束】 HDOJ 1534 Schedule Problem

来源:互联网 发布:vb下载官方 编辑:程序博客网 时间:2024/05/11 23:31

不难的差分约束。。列出四类不等式,建好图就可以了。。。

#include <iostream>  #include <queue>  #include <stack>  #include <map>  #include <set>  #include <bitset>  #include <cstdio>  #include <algorithm>  #include <cstring>  #include <climits>  #include <cstdlib>#include <cmath>#include <time.h>#define maxn 100005#define maxm 300005#define eps 1e-10#define mod 998244353#define INF 999999999#define lowbit(x) (x&(-x))#define mp mark_pair#define ls o<<1#define rs o<<1 | 1#define lson o<<1, L, mid  #define rson o<<1 | 1, mid+1, R  typedef long long LL;//typedef int LL;using namespace std;LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}// headint inq[maxn], outq[maxn], dis[maxn], t[maxn];int h[maxn], next[maxm], v[maxm], cost[maxm];queue<int> q;char ss[maxn];int cnt, n, m;void init(void){memset(h, -1, sizeof h);memset(inq, 0, sizeof inq);memset(outq, 0, sizeof outq);for(int i = 0; i <= n; i++) dis[i] = -INF;}void read(void){for(int i = 1; i <= n; i++) scanf(t[i]);}bool spfa(int s){while(!q.empty()) q.pop();dis[s] = 0, q.push(s), inq[s] = 1;while(!q.empty()) {int u = q.front(); q.pop(), inq[u] = 0, outq[u]++;if(outq[u] > n+1) return false;for(int e = h[u]; ~e; e = next[e])if(dis[u] + cost[e] > dis[v[e]]) {dis[v[e]] = dis[u] + cost[e];if(!inq[v[e]]) q.push(v[e]), inq[v[e]] = 1;}}return true;}void addedges(int u, int vv, int c){next[cnt] = h[u], h[u] = cnt, v[cnt] = vv, cost[cnt] = c, cnt++;}void work(void){int a, b;while(scanf("%s", ss), ss[0] != '#') {//scanf("%d%d", &a, &b);scanf(a), scanf(b);if(ss[0] == 'S' && ss[2] == 'S') addedges(b, a, 0);if(ss[0] == 'S' && ss[2] == 'F') addedges(b, a, t[b]);if(ss[0] == 'F' && ss[2] == 'S') addedges(b, a, -t[a]);if(ss[0] == 'F' && ss[2] == 'F') addedges(b, a, t[b] - t[a]);}for(int i = 1; i <= n; i++) addedges(0, i, 0);}int main(void){int _ = 0;while(scanf("%d", &n), n != 0) {read();init();work();printf("Case %d:\n", ++_);if(!spfa(0)) printf("impossible\n");else for(int i = 1; i <= n; i++) printf("%d %d\n", i, dis[i]);printf("\n");}return 0;}


0 0
原创粉丝点击