所以这样建图后,整个图一定是连通的,在没有负环的情况下一定有可行解,然后就是spfa求最短路,顺便判断负环,有负环就没有可行解,否则就有。
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include <queue> #include <vector> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int maxn = 205; struct Edge { int from, to, dist; Edge(int u,int v,int d) { from = u; to = v; dist = d; } }; struct Bellman { int n, m; vector<Edge> edges; vector<int> G[maxn]; int d[maxn]; int p[maxn]; int inq[maxn], cnt[maxn]; void init(int n) { this->n = n; for(int i = 0; i <= n; i++) { G[i].clear(); } edges.clear(); } void addEdge(int from, int to,int dist) { edges.push_back(Edge(from, to, dist)); m = edges.size(); G[from].push_back(m-1); } bool bellman_ford(int s) { queue<int> Q; memset(inq, 0, sizeof(inq)); memset(cnt, 0, sizeof(cnt)); for(int i = 0; i <= n; i++) { d[i] = INF; } d[s] = 0; inq[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = false; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(d[u] < INF && d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; p[e.to] = G[u][i]; if(!inq[e.to]) { Q.push(e.to); inq[e.to] = true; if(++cnt[e.to] > n) { return false; } } } } } return true; } }; int main() { Bellman bell; char op[4]; int n, m; while(scanf("%d", &n) != EOF && n) { bell.init(n+2); scanf("%d", &m); while(m--) { int s, len, k; scanf("%d %d %s %d", &s, &len, op, &k); if(op[0] == 'g') { bell.addEdge(s-1, s+len, -k - 1); }else { bell.addEdge(s+len, s-1, k - 1); } } for(int i = 0; i <= n; i++) { bell.addEdge(n+1, i, 0); } if(bell.bellman_ford(n+1)) { puts("lamentable kingdom"); }else { puts("successful conspiracy"); } } return 0; }