[关闭]
@suixinhita 2017-10-25T11:55:56.000000Z 字数 4919 阅读 795

代码

图论


1

  1. #include<queue>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N=105;
  7. const int M=105;
  8. struct data{
  9. int next,to;
  10. }e[1000005];
  11. int head[N],gs;
  12. char ans[N];
  13. int in[N],n;
  14. int len[M],cnt;
  15. char s[M][M];
  16. void add(int fr,int to)
  17. {
  18. e[++gs].to=to;e[gs].next=head[fr],head[fr]=gs;in[to]++;
  19. }
  20. bool topsort()
  21. {
  22. queue<int> q;
  23. for(int i=1;i<=26;++i) if(!in[i]) q.push(i);
  24. while(!q.empty())
  25. {
  26. int u=q.front();q.pop();ans[++cnt]='a'+u-1;
  27. for(int i=head[u];i;i=e[i].next)
  28. {
  29. int v=e[i].to;
  30. if(--in[v]==0) q.push(v);
  31. }
  32. }
  33. for(int i=1;i<=26;++i) if(in[i]) return 0;
  34. return 1;
  35. }
  36. void solve()
  37. {
  38. for(int i=1;i<n;++i)
  39. {
  40. for(int j=i+1;j<=n;++j)
  41. {
  42. int l1=len[i],l2=len[j];
  43. for(int k=1;k<=l1;++k)
  44. if(k>l2)
  45. {
  46. puts("Impossible");return;
  47. }
  48. else if(s[i][k]!=s[j][k])
  49. {
  50. add(s[i][k]-'a'+1,s[j][k]-'a'+1);
  51. break;
  52. }
  53. }
  54. }
  55. if(!topsort())
  56. {
  57. puts("Impossible");return;
  58. }
  59. for(int i=1;i<=cnt;++i) printf("%c",ans[i]);
  60. }
  61. int main()
  62. {
  63. // freopen("1.txt","r",stdin);
  64. scanf("%d",&n);
  65. for(int i=1;i<=n;++i)
  66. {
  67. scanf("%s",s[i]+1);
  68. len[i]=strlen(s[i]+1);
  69. }
  70. solve();
  71. return 0;
  72. }

2

  1. #include<queue>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #define ll long long
  5. using namespace std;
  6. const int N=1e6+5;
  7. struct data{
  8. int to,next;
  9. ll v;
  10. }e[N<<1];
  11. int head[N],gs,n,q,x,y,v;
  12. int in[N],out[N];
  13. ll c[N],U[N];
  14. void add(int fr,int to,ll v)
  15. {
  16. e[++gs].to=to,e[gs].next=head[fr],head[fr]=gs,e[gs].v=v,in[to]++,out[fr]++;
  17. }
  18. void topsort()
  19. {
  20. queue<int> q;
  21. for(int i=1;i<=n;++i)
  22. if(in[i]==0) q.push(i);
  23. while(!q.empty())
  24. {
  25. int u=q.front();q.pop();
  26. for(int i=head[u];i;i=e[i].next)
  27. {
  28. int v=e[i].to;
  29. c[v]+=e[i].v*c[u];
  30. if(--in[v]==0) q.push(v),c[v]-=U[v],c[v]=c[v]<0?0:c[v];
  31. }
  32. }
  33. int flag=1;
  34. for(int i=1;i<=n;++i)
  35. if(out[i]==0&&c[i]>0)
  36. flag=0,printf("%d %lld\n",i,c[i]);
  37. if(flag) puts("NULL");
  38. }
  39. inline int read()
  40. {
  41. int x=0,y=1;char c=getchar();
  42. while(c<'0'||c>'9')
  43. {
  44. if(c=='-') y=-1;
  45. c=getchar();
  46. }
  47. while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
  48. return x*y;
  49. }
  50. int main()
  51. {
  52. // freopen("1.txt","r",stdin);
  53. n=read(),q=read();
  54. for(int i=1;i<=n;++i) c[i]=read(),U[i]=read();
  55. for(int i=1;i<=q;++i)
  56. {
  57. x=read(),y=read(),v=read();
  58. add(x,y,v);
  59. }
  60. topsort();
  61. return 0;
  62. }

3

  1. #include<stack>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N=1005;
  7. const int inf=0x3f3f3f3f;
  8. stack<int> s[2];
  9. struct data{
  10. int to,next;
  11. }e[N];
  12. int head[N],gs;
  13. int a[N],col[N],n,mn[N];
  14. void add(int fr,int to)
  15. {
  16. //printf("%d %d\n",fr,to);
  17. e[++gs].to=to,e[gs].next=head[fr],head[fr]=gs;
  18. }
  19. void init()
  20. {
  21. a[n+1]=inf;
  22. for(int i=1;i<=n+1;++i) mn[i]=inf;
  23. for(int i=n;i>=1;--i)
  24. mn[i]=min(mn[i+1],a[i]);
  25. for(int i=1;i<=n;++i)
  26. for(int j=i+1;j<=n;++j)
  27. if(a[i]<a[j]&&mn[j+1]<a[i]) add(i,j),add(j,i);
  28. memset(col,-1,sizeof(col));
  29. }
  30. bool dfs(int u,int c)
  31. {
  32. col[u]=c;
  33. for(int i=head[u];i;i=e[i].next)
  34. {
  35. int v=e[i].to;
  36. bool flag=1;
  37. if(col[v]==-1)
  38. {
  39. flag=dfs(v,c^1);
  40. if(!flag) return 0;
  41. }
  42. else if(col[v]==c) return 0;
  43. }
  44. return 1;
  45. }
  46. inline int read()
  47. {
  48. int x=0;char c=getchar();
  49. while(c<'0'||c>'9') c=getchar();
  50. while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
  51. return x;
  52. }
  53. int main()
  54. {
  55. // freopen("1.txt","r",stdin);
  56. // freopen("11.txt","w",stdout);
  57. n=read();
  58. for(int i=1;i<=n;++i) a[i]=read();
  59. init();
  60. for(int i=1;i<=n;++i)
  61. if(col[i]==-1) if(dfs(i,0)==0)
  62. {
  63. puts("0");return 0;
  64. }
  65. int now=1;s[0].push(inf),s[1].push(inf);
  66. for(int i=1;i<=n;++i)
  67. {
  68. if(col[i]==0) printf("a "),s[0].push(a[i]);
  69. else if(col[i]==1) printf("c "),s[1].push(a[i]);
  70. while(s[0].top()==now||s[1].top()==now)
  71. {
  72. if(s[0].top()==now) printf("b "),s[0].pop();
  73. else printf("d "),s[1].pop();
  74. now++;
  75. }
  76. }
  77. return 0;
  78. }

4

  1. #include<queue>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N=10005;
  7. const int M=1000005;
  8. const int inf=0x3f3f3f3f;
  9. struct data{
  10. int to,next,f;
  11. }e[M];
  12. int head[N],gs=1,s,t;
  13. bool map[105][105];
  14. int l[105][105],r[105][105],m,n;
  15. int fx[5],fy[5];
  16. char S[105];
  17. void add(int fr,int to,int f)
  18. {
  19. // printf("%d %d\n",fr,to);
  20. e[++gs].to=to,e[gs].f=f,e[gs].next=head[fr],head[fr]=gs;
  21. e[++gs].to=fr,e[gs].f=0,e[gs].next=head[to],head[to]=gs;
  22. }
  23. int dis[N],now[N];
  24. bool bfs()
  25. {
  26. queue<int> q;
  27. memset(dis,-1,sizeof(dis));
  28. dis[s]=0,q.push(s);
  29. while(!q.empty())
  30. {
  31. int u=q.front();q.pop();
  32. for(int i=head[u];i;i=e[i].next)
  33. {
  34. int v=e[i].to;
  35. if(!e[i].f) continue;
  36. if(dis[v]==-1)
  37. {
  38. dis[v]=dis[u]+1;
  39. q.push(v);
  40. }
  41. }
  42. }
  43. if(dis[t]==-1) return 0;
  44. else return 1;
  45. }
  46. int dfs(int u,int f)
  47. {
  48. if(u==t) return f;
  49. int ff,k,su=0;
  50. for(int i=now[u];i;i=e[i].next)
  51. {
  52. int v=e[i].to;
  53. if(dis[v]==dis[u]+1)
  54. {
  55. ff=min(e[i].f,f-su);
  56. k=dfs(v,ff);
  57. su+=k;
  58. e[i].f-=k,e[i^1].f+=k;
  59. if(e[i].f) now[u]=i;
  60. if(su==f) return su;
  61. }
  62. }
  63. if(!su) dis[u]=-1;
  64. return su;
  65. }
  66. int dinic()
  67. {
  68. int an=0;
  69. while(bfs())
  70. {
  71. for(int i=s;i<=t;++i) now[i]=head[i];
  72. an+=dfs(s,inf);
  73. }
  74. return an;
  75. }
  76. void build()
  77. {
  78. int cntl=0,cntr=n*m;
  79. for(int i=1;i<=n;++i)
  80. for(int j=1;j<=m;++j)
  81. {
  82. l[i][j]=++cntl;
  83. r[i][j]=++cntr;
  84. }
  85. for(int i=1;i<=n;++i)
  86. for(int j=1;j<=m;++j)
  87. for(int k=1;k<=4;++k)
  88. {
  89. int x=i+fx[k],y=j+fy[k];
  90. if(x<1||x>n) continue;
  91. if(y<1||y>m) continue;
  92. if((!map[i][j])&&(!map[x][y])) add(l[i][j],r[x][y],1);
  93. }
  94. s=0,t=++cntr;
  95. for(int i=1;i<=n;++i)
  96. for(int j=1;j<=m;++j)
  97. if(!map[i][j]) add(s,l[i][j],1),add(r[i][j],t,1);
  98. }
  99. int main()
  100. {
  101. // freopen("1.txt","r",stdin);
  102. // freopen("11.txt","w",stdout);
  103. int R,C;int tot=0;
  104. scanf("%d%d%d%d",&n,&m,&R,&C);
  105. for(int i=1;i<=n;++i)
  106. {
  107. scanf("%s",S+1);
  108. for(int j=1;j<=m;++j) if(S[j]=='x') map[i][j]=1,tot++;
  109. }
  110. fx[1]=R,fx[2]=C,fx[3]=R,fx[4]=C;
  111. fy[1]=C,fy[2]=R,fy[3]=-C,fy[4]=-R;
  112. build();
  113. int ans=dinic();
  114. printf("%d\n",m*n-tot-ans);
  115. return 0;
  116. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注