差分的题,建图还是挺纠结的,虽然这个题没有卡建图,所有不等式都给你了,没有隐含条件,只是注意把 < (>)转化成 <= ( >= )就可以了,但是还是逗留在套模板的那个层面,没有深刻理解其中的理论意义,以后还要继续努力。
代码:
#include<cstdio> #include<cstring> #include<queue> using namespace std; const int N = 110; struct Edge{ int s,e,v,next; }edge[10010]; int n,e_num,head[N],vis[N],dist[N],countx[N]; queue <int>q; void AddEdge(int a,int b,int c){ edge[e_num].s=a; edge[e_num].e=b; edge[e_num].v=c; edge[e_num].next=head[a]; head[a]=e_num++; } void getmap(){ int m,a,b,c; char ch[3]; scanf("%d",&m); e_num=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(countx,0,sizeof(countx)); while(m--){ scanf("%d%d%s%d",&a,&b,ch,&c); if(strcmp(ch,"gt")==0) AddEdge(a+b+1,a,-c-1); else AddEdge(a,a+b+1,c-1); if(!vis[a]){ q.push(a); vis[a]=1; countx[a]++; } if(!vis[a+b+1]){ q.push(a+b+1); vis[a+b+1]=1; countx[a+b+1]++; } } } int spfa(){ while(!q.empty()){ int cur=q.front(); q.pop(); vis[cur]=0; if(countx[cur]>n)return 0; for(int i=head[cur];i!=-1;i=edge[i].next){ int u=edge[i].e; if(dist[u]>dist[cur]+edge[i].v){ dist[u]=dist[cur]+edge[i].v; if(!vis[u]){ q.push(u); vis[u]=1; countx[u]++; } } } } return 1; } void solve(){ memset(dist,0,sizeof(dist)); int tmp=spfa(); if(tmp)puts("lamentable kingdom"); else puts("successful conspiracy"); } int main() { while(scanf("%d",&n),n) { getmap(); solve(); } return 0; }