Poj1364/zoj1260 King (difference constraint, spfa)

Source: Internet
Author: User

The difference question is still quite tangled. Although this question is not shown in a diagram, all the inequalities are given to you, and there are no implicit conditions, just pay attention to <(>) it can be converted to <= (> =), but it remains at the level of the Set template, without a deep understanding of the theoretical significance, and will continue to work hard in the future.

Code:

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.