UVA 515 - King (SPFA)
2013-11-29 13:48
330 查看
King |
birth to a nice son.
Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a
given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence.
The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had
to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some
hope that his son would be able to make some decisions.
After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were
wrong.
Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences
![](http://uva.onlinejudge.org/external/5/515img1.gif)
of
a sequence
![](http://uva.onlinejudge.org/external/5/515img2.gif)
. The king thought a minute and then decided, i.e. he set for the sum
![](http://uva.onlinejudge.org/external/5/515img3.gif)
of
each subsequence Si an integer constraint ki (i.e.
![](http://uva.onlinejudge.org/external/5/515img4.gif)
or
![](http://uva.onlinejudge.org/external/5/515img5.gif)
resp.)
and declared these constraints as his decisions.
After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors
to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not.
Input
The input file consists of blocks of lines. Each block except the last corresponds to one set of problems and king's decisions about them. In the first line of the block there are integers n,and m where
![](http://uva.onlinejudge.org/external/5/515img6.gif)
is
length of the sequence S and
![](http://uva.onlinejudge.org/external/5/515img7.gif)
is
the number of subsequences Si. Next m lines
contain particular decisions coded in the form of quadruples si, ni, oi, ki,
where oi represents operator > (coded as gt)
or operator < (coded as lt) respectively. The symbols si, ni and ki have
the meaning described above. The last block consists of just one line containing 0.
Output
The output file contains the lines corresponding to the blocks in the input file. A line contains textsuccessful conspiracy when such a sequencedoes not exist. Otherwise it contains text lamentable kingdom. There is no line in the output file corresponding to the last ``null'' block of the input file.
Sample Input
4 2 1 2 gt 0 2 2 lt 2 1 2 1 0 gt 0 1 0 lt 0 0
Sample Output
lamentable kingdom successful conspiracy
题意:差分约束系统,不太懂,看的题解。http://www.verydemo.com/demo_c378_i79852.html
思路:SPFA找负环。
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
#define INF 0x3f3f3f3f
using namespace std;
const int N = 205, M = 205;
int n, m, a, b, value, first
, next[M], u[M], v[M], w[M], E;
char str[10];
queue<int>q;
void build(int U, int V, int W) {
u[E] = U;
v[E] = V;
w[E] = W;
next[E] = first[U];
first[U] = E ++;
}
void init() {
scanf("%d", &m);
E = 0; int i;
memset(first, -1, sizeof(first));
for (i = 0; i < m; i ++) {
scanf("%d%d%s%d", &a, &b, str, &value);
if (strcmp(str, "gt") == 0) {
int u = a + b, v = a - 1, w = -value - 1;
build(u, v, w);
}
else {
int u = a - 1, v = a + b, w = value - 1;
build(u, v, w);
}
}
for (i = 0; i <= n; i ++)
build(n + 1, i, 0);
}
bool SPFA() {
int vis
, d
, time
;
memset(d, INF, sizeof(d));
memset(vis, 0, sizeof(vis));
memset(time, 0, sizeof(time));
vis[n + 1] = 1;
time[n + 1] = 1;
d[n + 1] = 0;
q.push(n + 1);
while (!q.empty()) {
int u = q.front(); q.pop();
vis[u] = 0;
for (int e = first[u]; e != -1; e = next[e]) {
if (d[v[e]] > d[u] + w[e]) {
d[v[e]] = d[u] + w[e];
if (!vis[v[e]]) {
vis[v[e]] = 1;
time[v[e]] ++;
if (time[v[e]] > n + 1) return true;
q.push(v[e]);
}
}
}
}
return false;
}
void solve() {
init();
if (!SPFA()) printf("lamentable kingdom\n");
else printf("successful conspiracy\n");
}
int main() {
while (~scanf("%d", &n) && n) {
solve();
}
return 0;
}
相关文章推荐
- UVa 515 - King (差分约束系统 + SPFA求带负权最短路)
- King - UVa 515 差分约束系统
- UVa 515 King(差分约束系统)
- UVA 515 King
- UVa 515 King (差分约束+线性规划+判负圈)
- UVa 515 King (差分约束系统)
- UVa 515 - King
- UVA 515 - King(差分约束系统)
- UVA515 King
- UVALive 5532 King(差分约束,spfa)
- UVA 515 - King 差分约束系统
- UVA 515 King
- uva 515 King(差分约束)
- UVA 515 King
- UVA 515 King
- UVA 515 - King 差分约束系统
- UVA 515 King
- POJ 1364 King (UVA 515) 差分约束
- POJ 1364 King (UVA 515) 差分约束
- uva 515 King