#include<bits/stdc++.h> #define il inline #define ll long long #define RE register #define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++) #define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--) #define debug printf("%d %s\n",LINE,FUNCTION) using namespace std; const int N=100005,inf=233333333,dx[4]={0,0,1,-1},dy[4]={1,-1,0,0}; int n,m,s,t,dis[N],to[N],net[N],w[N],h[N],cnt=1; int mp[105][105],ans; il int id(int x,int y){return (x-1)m+y;} il void add(int u,int v,int c){ to[++cnt]=v,net[cnt]=h[u],w[cnt]=c,h[u]=cnt; to[++cnt]=u,net[cnt]=h[v],w[cnt]=0,h[v]=cnt; } queue<int>q; il bool bfs(){ memset(dis,-1,sizeof(dis)); q.push(s),dis[s]=0; while(!q.empty()){ RE int u=q.front();q.pop(); for(RE int i=h[u];i;i=net[i]) if(dis[to[i]]==-1&&w[i]) dis[to[i]]=dis[u]+1,q.push(to[i]); } return dis[t]!=-1; } int dfs(int u,int op){ if(u==t)return op; int flow=0,used=0; for(RE int i=h[u];i;i=net[i]){ int v=to[i]; if(dis[to[i]]==dis[u]+1&&w[i]){ used=dfs(to[i],min(op,w[i])); if(!used)continue; flow+=used,op-=used; w[i]-=used,w[i^1]+=used; if(!op)break; } } if(!flow) dis[u]=-1; return flow; } il void init(){ scanf("%d%d",&n,&m),t=nm+1; For(i,1,n) For(j,1,m) { scanf("%d",&mp[i][j]),ans+=mp[i][j]; (i+j)&1?add(s,id(i,j),mp[i][j]):add(id(i,j),t,mp[i][j]); } For(i,1,n) For(j,1,m) if((i+j)&1) { For(k,0,3){ RE int xx=i+dx[k],yy=j+dy[k]; if(xx>0&&xx<=n&&yy>0&&yy<=m) add(id(i,j),id(xx,yy),inf); } } while(bfs()) ans-=dfs(s,inf); cout<<ans; } int main(){ init(); return 0; }改为C语言代码
时间: 2023-07-16 17:11:51 浏览: 125
把 C++ 的头文件换成对应的 C 头文件,把 inline 函数改为普通函数,去掉 using namespace std,再把 cin 和 cout 改为 scanf 和 printf 就可以了。
以下是改完的代码:
```c
#include <stdio.h>
#include <string.h>
#define ll long long
#define RE register
#define For(i, a, b) for (RE int (i) = (a); (i) <= (b); (i)++)
#define Bor(i, a, b) for (RE int (i) = (b); (i) >= (a); (i)--)
#define il inline
const int N = 100005, inf = 233333333, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int n, m, s, t, dis[N], to[N], net[N], w[N], h[N], cnt = 1;
int mp[105][105], ans;
il int id(int x, int y) { return (x - 1) * m + y; }
il void add(int u, int v, int c)
{
to[++cnt] = v, net[cnt] = h[u], w[cnt] = c, h[u] = cnt;
to[++cnt] = u, net[cnt] = h[v], w[cnt] = 0, h[v] = cnt;
}
int bfs()
{
memset(dis, -1, sizeof(dis));
dis[s] = 0;
int head = 0, tail = 0, que[N];
que[tail++] = s;
while (head < tail)
{
int u = que[head++];
for (int i = h[u]; i; i = net[i])
{
int v = to[i];
if (dis[v] == -1 && w[i])
{
dis[v] = dis[u] + 1;
que[tail++] = v;
}
}
}
return dis[t] != -1;
}
int dfs(int u, int op)
{
if (u == t)
return op;
int flow = 0;
for (int i = h[u]; i && op; i = net[i])
{
int v = to[i];
if (dis[v] == dis[u] + 1 && w[i])
{
int tmp = dfs(v, op < w[i] ? op : w[i]);
if (!tmp)
dis[v] = -1;
w[i] -= tmp, w[i ^ 1] += tmp;
op -= tmp, flow += tmp;
}
}
return flow;
}
int main()
{
scanf("%d%d", &n, &m), t = n * m + 1;
s = 0;
For(i, 1, n)
{
For(j, 1, m)
{
scanf("%d", &mp[i][j]);
ans += mp[i][j];
if ((i + j) & 1)
add(s, id(i, j), mp[i][j]);
else
add(id(i, j), t, mp[i][j]);
}
}
For(i, 1, n)
{
For(j, 1, m)
{
if ((i + j) & 1)
{
For(k, 0, 3)
{
int xx = i + dx[k], yy = j + dy[k];
if (xx > 0 && xx <= n && yy > 0 && yy <= m)
add(id(i, j), id(xx, yy), inf);
}
}
}
}
while (bfs())
ans -= dfs(s, inf);
printf("%d\n", ans);
return 0;
}
```
阅读全文