UVa 515 King (差分约束+线性规划+判负圈)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
#include<string>
using namespace std;

const int INF=100000000;
const int maxn=100+10;
const int maxm=100*100+10;
int n,m;
int first[maxn];
int u[maxm],v[maxm],nnext[maxm],w[maxm];
int V,E;
int d[maxn];

bool Bellman_Ford()
{
	queue<int> q;
	bool inq[maxn];
	int c[maxn];
	V=0;
	for(int i=0;i<=n+1;i++)d[i]=(i==n+1 ? 0:INF);
	memset(inq,0,sizeof(inq));
	memset(c,0,sizeof(c));
	q.push(n+1);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		inq[x]=false;
		for(int e=first[x];e!=-1;e=nnext[e])if(d[v[e]]>d[x]+w[e])
		{
			d[v[e]]=d[x]+w[e];
			if(!inq[v[e]])
			{
				inq[v[e]]=true;
				q.push(v[e]);
				c[v[e]]++;
				if(c[v[e]]==n+2)return true;
			}
		}
	}
	return false;
}

int main()
{
	while(~scanf("%d",&n))
	{
		if(n==0)break;
		scanf("%d",&m);
		for(int i=0;i<=n+1;i++)
			first[i]=-1;
		E=0;
		for(int i=0;i<m;i++)
		{
			char op[10];
			int si,ni,ki;
			scanf("%d %d %s %d",&si,&ni,op,&ki);
			if(op[0]=='g')
			{
				u[E]=si+ni;
				v[E]=si-1;
				w[E]=-ki-1;
				nnext[E]=first[u[E]];
				first[u[E]]=E;
				E++;
			}
			else
			{
				u[E]=si-1;
				v[E]=si+ni;
				w[E]=ki-1;
				nnext[E]=first[u[E]];
				first[u[E]]=E;
				E++;
			}
		}
		for(int i=0;i<n+1;i++)
		{
			    u[E]=n+1;
                v[E]=i;
                w[E]=0;
                nnext[E]=first[u[E]];
                first[u[E]]=E;
                E++;
		}
		if(Bellman_Ford())printf("successful conspiracy\n");
		else printf("lamentable kingdom\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值