poj1364
初次尝试查分约束,遇见问题很多,总的来说这题还是比较裸地
View Code
1 #include <stdio.h>
2 #include <string.h>
3 #include <queue>
4 using std::queue;
5 #define MAXN 110
6 #define INF (1<<29)
7 int n,m;
8 struct node
9 {
10 int d,c,next;
11 }edge[2*MAXN];
12 int head[MAXN],p,dist[MAXN],cnt[MAXN];
13 queue<int> q;
14 bool v[MAXN];
15 void addedge(int a,int b,int c)
16 {
17 edge[p].d=b;
18 edge[p].c=c;
19 edge[p].next=head[a];
20 head[a]=p++;
21 }
22 bool spfa()
23 {
24 dist[n+1]=0;
25 v[n+1]=true;
26 q.push(n+1);
27 int k;
28 while(!q.empty())
29 {
30 k=q.front();
31 q.pop();
32 v[k]=false;
33 for(p=head[k];p!=-1;p=edge[p].next)
34 if(dist[edge[p].d]>dist[k]+edge[p].c)
35 {
36 dist[edge[p].d]=dist[k]+edge[p].c;
37 if(!v[edge[p].d])
38 {
39 if((++cnt[edge[p].d])>(n+1))
40 return true;
41 q.push(edge[p].d);
42 v[edge[p].d]=true;
43 }
44 }
45 }
46 return false;
47 }
48 int main()
49 {
50 while(scanf("%d",&n),n)
51 {
52 memset(head,-1,sizeof(head));
53 memset(v,false,sizeof(v));
54 memset(cnt,0,sizeof(cnt));
55 for(int i=0;i<=n;dist[i++]=INF);
56 p=0;
57 scanf("%d",&m);
58 for(int i=0,a,b,c;i<m;++i)
59 {
60 char s[3];
61 scanf("%d %d %s %d",&a,&b,s,&c);
62 if(s[0]=='g')
63 addedge(a+b,a-1,-c-1);
64 else
65 addedge(a-1,a+b,c-1);
66 }
67 for(int i=0;i<=n;++i)
68 addedge(n+1,i,0);
69 if(spfa())
70 printf("successful conspiracy\n");
71 else
72 printf("lamentable kingdom\n");
73 }
74 return 0;
75 }