当前位置 : 主页 > 编程语言 > java >

hdu 1531 King(差分约束·负环的判断)

来源:互联网 收集:自由互联 发布时间:2022-08-10
题目:​​http://acm.hdu.edu.cn/showproblem.php?pid=1531​​ King Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1277Accepted Submission(s): 570 Problem Description Once, in one k


题目:​​http://acm.hdu.edu.cn/showproblem.php?pid=1531​​

King


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1277    Accepted Submission(s): 570


Problem Description

Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound king.'' After nine months her child was born, and indeed, she gave 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 Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki 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 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 0 < n <= 100 is length of the sequence S and 0 < m <= 100 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 contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. Otherwise it contains text lamentable kingdom. There is no line in the output corresponding to the last ``null'' block of the input.


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

 


 aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki 可以表示成S(asi+ni)-S(asi-1)<ki -->S(asi+ni)-S(asi-1)<=ki-1由此构成了差分系统的雏形。接下来就是用图论的知识来解决这个问题,如果串不存在即存在负环。最开始用邻接表+spfa算法来解决它,结果是无尽的WA。后来发现用矩阵表示,并且从第0点开始遍历至第n点(0点作为S点,n+1作为F点),出队或入队的次数大于n+1才能AC【不是1-->n,if(inque[x]>n) return 1;】。


outque版的:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const int N=1010,N2=1e5+5;
LL n,m,sum;
bool vis[N];
LL dis[N],head[N],g[N][N],outque[N];
bool spfa(int s){
LL stack[N],outque[N];
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(outque,0,sizeof(outque));
queue<int> q;
vis[s]=1;
dis[s]=0;
q.push(s);
while(!q.empty()){
LL temp=q.front();
q.pop();
vis[temp]=0;
outque[temp]++;
if(outque[temp]>n+1) return 0;
for(LL i=0;i<=n;i++){
if(dis[i]>g[temp][i]+dis[temp]){
dis[i]=g[temp][i]+dis[temp];
if(vis[i]==0){
vis[i]=1;
q.push(i);
}
}
}
}
return 1;
}
int main()
{
//freopen("cin.txt","r",stdin);
while(cin>>n&&n){
sum=0;
memset(head,-1,sizeof(head));
scanf("%lld",&m);
memset(g,0x3f,sizeof(g));
memset(g[n+1],0,sizeof(g[n+1]));
while(m--){
LL s,n,k;
char o[5];
scanf("%lld %lld %s %lld",&s,&n,o,&k);
if(o[0]=='g') g[s-1][s+n]=-k-1;
else g[s+n][s-1]=k-1;
}
if(spfa(n+1))puts("lamentable kingdom");
else puts("successful conspiracy");
}
return 0;
}

inque版的:


LL dis[N],head[N],g[N][N],inque[N];
bool spfa(int s){
LL stack[N],outque[N];
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(inque,0,sizeof(inque));
queue<int> q;
vis[s]=1;
dis[s]=0;
q.push(s);
inque[s]++;
while(!q.empty()){
LL temp=q.front();
q.pop();
vis[temp]=0;
for(LL i=0;i<=n;i++){
if(dis[i]>g[temp][i]+dis[temp]){
dis[i]=g[temp][i]+dis[temp];
if(vis[i]==0){
vis[i]=1;
q.push(i);
inque[i]++;
if(inque[i]>n+1) return 0;
}
}
}
}
return 1;
}


更好理解的bellman_ford算法,松弛操作大于n即返回结果。


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=105;
struct Edge{
int u,v,w;
}edge[N*N];
int dis[N];
int n,m,sum;
void addedge(int u,int v,int w){
edge[sum].u=u;
edge[sum].v=v;
edge[sum++].w=w;
}
void relax(int u,int v,int w){
if(dis[v]>dis[u]+w) dis[v]=dis[u]+w;
}
bool bellman_ford(int s){
memset(dis,0x3f,sizeof(dis));
dis[s]=0;
for(int i=0;i<n;i++){
for(int j=0;j<sum;j++) relax(edge[j].u,edge[j].v,edge[j].w);
}
for(int i=0;i<sum;i++){
if(dis[edge[i].v]>dis[edge[i].u]+edge[i].w) return 1;
}
return 0;
}
int main()
{
//freopen("cin.txt","r",stdin);
while(cin>>n&&n){
sum=0;
scanf("%d",&m);
while(m--){
int si,ni,ki;
char oi[5];
scanf("%d%d%s%d",&si,&ni,oi,&ki);
if(oi[0]=='g') addedge(si-1,si+ni,-ki-1);
else addedge(si+ni,si-1,ki-1);
}
if(bellman_ford(0))puts("successful conspiracy");
else puts("lamentable kingdom");
}
return 0;
}

向量的方向是不会影响结果的。


主函数内:


scanf("%d%d%s%d",&si,&ni,oi,&ki);
if(oi[0]=='l') addedge(si-1,si+ni,ki-1);
else addedge(si+ni,si-1,-ki-1);






上一篇:python module
下一篇:没有了
网友评论