http://acm.hdu.edu.cn/showproblem.php?pid=1531
差分约束关键是建立模型,而这是最难的~~~~
感觉理解还是不深。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int MAXM=20005;
const int MAXN=105;
struct node
{
int u,v,w;
};
node edge[MAXM];
int first[MAXN],next[MAXM],cc;
int inq[MAXN],dist[MAXN],cnt[MAXN];
inline void add_edge(int u,int v,int w)
{
edge[cc].u=u;
edge[cc].v=v;
edge[cc].w=w;
next[cc]=first[u];
first[u]=cc;
cc++;
}
bool SPFA(int s,int n)
{
int u,v,i,w;
memset(dist,inf,sizeof(dist));
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
dist[s]=0;
queue<int> q;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
inq[u]=0;
for(i=first[u];i!=-1;i=next[i])
{
v=edge[i].v;
w=edge[i].w;
if(dist[v]>dist[u]+w)
{
dist[v]=dist[u]+w;
if(!inq[v])
{
cnt[v]++;
if(cnt[v]>n)
return false;
inq[v]=1;
q.push(v);
}
}
}
}
return true;
}
int main()
{
int n,m;
while(scanf("%d",&n),n)
{
int i;
scanf("%d",&m);
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
cc=0;
for(i=0;i<m;i++)
{
int u,v,w;
char word[10];
scanf("%d%d%s%d",&u,&v,word,&w);
if(word[0]=='l')
{
add_edge(u-1,u+v,w-1);
}
else
{
add_edge(u+v,u-1,-w-1);
}
}
int s=n+1;
for(i=0;i<=n;i++)
add_edge(s,i,0);
int ans=SPFA(s,n+2);
if(!ans)
printf("successful conspiracy\n");
else
printf("lamentable kingdom\n");
}
return 0;
}