POJ 1364差分约束

给出n个变量,m个约束公式 Sa + Sa+1 + .... + Sa+b < ki or > ki ,叫你判断是否存在着解满足这m组约束公式。

Sa + Sa+1   +   .+ Sa+b =  Sum[a+b] - Sum[a-1]  . 注意加入源点n+1 。


public class Main {
	public static void main(String[] args) throws UnsupportedEncodingException {
		new Task().solve();
	}
}

class Task {
	class Node {
		int w;
		int v;

		Node(int v, int w) {
			this.w = w;
			this.v = v;
		}
	}

	List<Node>[] adj;
	final int inf = 1000000000;

	boolean spfa(int start, int n) {
		int[] dist = new int[n + 1];
		int[] inqTime = new int[n + 1];
		boolean[] inq = new boolean[n + 1];
		Arrays.fill(dist, inf);
		Arrays.fill(inqTime, 0);
		Arrays.fill(inq, false);
		dist[start] = 0;
		inq[start] = true;
		Queue<Integer> q = new LinkedList<Integer>();
		q.offer(start);
		while (! q.isEmpty()) {
			int u = q.poll();
			inq[u] = false;
			for (Node next : adj[u]) {
				int v = next.v;
				int w = next.w;
				if (dist[u] + w < dist[v]) {
					dist[v] = dist[u] + w;
					if (!inq[v]) {
						inq[v] = true;
						if (++inqTime[v] > n)
							return false;
						q.offer(v) ;
					}
				}
			}
		}
		return true;
	}

	void solve() {
		while (true) {
			int n = in.nextInt();
			if (n == 0)
				break;
			int m = in.nextInt();
			adj = new List[n + 2];
			for (int i = 0; i <= n+1; i++)
				adj[i] = new ArrayList<Node>();
			while (m-- > 0) {
				int si = in.nextInt();
				int ni = in.nextInt();
				String oi = in.next();
				int ki = in.nextInt();
				if ("gt".equals(oi)) {
					adj[si + ni].add(new Node(si - 1, -ki - 1));
				} else {
					adj[si - 1].add(new Node(si + ni, ki - 1));
				}
			}
			for (int i = 0; i <= n; i++)
				adj[n+1].add(new Node(i, 0));
			out.println(spfa(n+1, n+2) ? "lamentable kingdom"
					: "successful conspiracy");
		}
		out.flush();
	}

	InputReader in = new InputReader(System.in);
	PrintWriter out = new PrintWriter(System.out);
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = new StringTokenizer("");
	}

	private void eat(String s) {
		tokenizer = new StringTokenizer(s);
	}

	public String nextLine() {
		try {
			return reader.readLine();
		} catch (Exception e) {
			return null;
		}
	}

	public boolean hasNext() {
		while (!tokenizer.hasMoreTokens()) {
			String s = nextLine();
			if (s == null)
				return false;
			eat(s);
		}
		return true;
	}

	public String next() {
		hasNext();
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public BigInteger nextBigInteger() {
		return new BigInteger(next());
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值