给出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());
}
}