/* **********************************************Author : JayYeCreated Time: 2013/10/7 15:26:16File Name : Orz.cpp*********************************************** */#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int maxn = 100 + 5;const int INF = 1<<30;struct Edge{ int u, to, next, val;}edge[maxn*maxn];int head[maxn], d[maxn], vis[maxn], T[maxn], q[maxn*maxn], n, E;void init() { memset(head, -1, sizeof(head)); E = 0;}void newedge(int u, int to, int val) { edge[E].u = u; edge[E].to = to; edge[E].val = val; edge[E].next = head[u]; head[u] = E++;}bool SPFA() { for(int i = 0;i <= n + 1; i++) { d[i] = INF; vis[i] = T[i] = 0; } T[n+1] = 1; d[n+1] = 0; vis[n+1] = 1; int st = 0, ed = 0; q[ed++] = n+1; while(st < ed) { int u = q[st++]; vis[u] = 0; for(int i = head[u];i != -1;i = edge[i].next) { int to = edge[i].to, val = edge[i].val; if(d[to] > d[u] + val) { d[to] = d[u] + val; if(!vis[to]) { vis[to] = 1; T[to]++; if(T[to] > n + 1) return true; q[ed++] = to; } } } } return false;}int main() { int m; while(scanf("%d", &n) != -1 && n) { init(); scanf("%d", &m); for(int i = 0;i < m; i++) { int fr, len, k; char op[4]; scanf("%d%d%s%d", &fr, &len, op, &k); int u = fr + len, to = fr-1; if(op[0] == 'g') newedge(u, to, -1 - k); else newedge(to, u, k - 1); } for(int i = 0;i <= n; i++) newedge(n+1, i, 0); printf("%s\n", SPFA() ? "successful conspiracy" : "lamentable kingdom"); } return 0;}
Bellman code:
/* **********************************************Author : JayYeCreated Time: 2013/10/7 15:26:16File Name : Orz.cpp*********************************************** */#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int maxn = 100 + 5;struct Edge{ int u, to, next, val;}edge[maxn*maxn*2];int head[maxn], d[maxn], n, E;void init() { memset(head, -1, sizeof(head)); E = 0;}void newedge(int u, int to, int val) { edge[E].u = u; edge[E].to = to; edge[E].val = val; edge[E].next = head[u]; head[u] = E++;}bool Bellman() { memset(d, 0, sizeof(d)); for(int i = 0;i < n+1; i++) { for(int j = 0;j < E; j++) { int u = edge[j].u, to = edge[j].to, val = edge[j].val; if(d[to] > d[u] + val) { d[to] = d[u] + val; if(i == n) return true; } } } return false;}int main() { int m; while(scanf("%d", &n) != -1 && n) { init(); scanf("%d", &m); for(int i = 0;i < m; i++) { int fr, len, k; char op[4]; scanf("%d%d%s%d", &fr, &len, op, &k); int u = fr + len, to = fr-1; if(op[0] == 'g') newedge(u, to, - 1 - k); else newedge(to, u, k - 1); } printf("%s\n", Bellman() ? "successful conspiracy" : "lamentable kingdom"); } return 0;}