hdoj1531

Problem : 1531 ( King )     Judge Status : Accepted
RunId : 2661436    Language : G++    Author : huwenbiao
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
/***************************************************************\
*Author:Hu Wenbiao
*Created Time: Thu 22 Jul 2010 04:20:35 PM CST
*File Name: main.cpp
*Description:差分约束系统.S[si+ni]-S[si-1]>=ki+1>ki;S[si+ni]-
*S[si-1]<=ki-1<ki.其中S[i]=S_1+S_2+S_3+...+S_i;
\***************************************************************/

//*========================*Head File*========================*\\

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*----------------------*Global Variable*----------------------*/
struct
E{
//边
int s,e,len;
}
edges[
110];
int
n,m,si,ni,ki,_min,_max,dist[
110];
char
oi;

//*=======================*Main Program*=======================*//
using namespace std;

bool
Bellman_Ford(){
//Bellman-Ford求最长路径
int s,e,len;
for
(int i=
0;i<_max-_min;i++){
bool
flag=
true;
for
(int j=
0;j<m;j++){
s=edges[j].s,e=edges[j].e,len=edges[j].len;
if
(dist[e]<dist[s]+len){
//这儿如果是if(dist[s]!=-1&&...)
//就会出错,有大牛路过的话麻烦指点一下。

dist[e]=dist[s]+len;
flag=
false;
}
}

if
(flag)
return
true;
}

for
(int j=
0;j<m;j++){//验证是否有解
s=edges[j].s,e=edges[j].e,len=edges[j].len;
if
(dist[e]<dist[s]+len)
return
false;
}

return
true;
}


int
main(){

//freopen("input","r",stdin);
while((scanf("%d",&n)==1)&&n>0){
scanf(
"%d",&m);
_max=-
1;
_min=
99999999;
for
(int i=
0;i<m;i++){//建图
scanf("%d %d %ct %d",&si,,&oi,&ki);
if
(_min>si-
1)
_min=si-
1;
if
(_max<si+ni)
_max=si+ni;
if
(oi==
'g'){//gt
edges[i].s=si-1;
edges[i].e=si+ni;
edges[i].len=ki+
1;
}

else
{
//lt
edges[i].s=si+ni;
edges[i].e=si-
1;
edges[i].len=
1-ki;
}
}

memset(dist,-
1,sizeof(dist));
dist[_min]=
0;
if
(Bellman_Ford())
printf(
"lamentable kingdom\n");
else

printf(
"successful conspiracy\n");
}
}

你可能感兴趣的:(OJ)