参考:https://www.cnblogs.com/neighthorn/p/6705785.html

并不是黑白染色而是三色染色(还有四色的,不过是一个意思



仔细观察一下不合法情况,可以发现都是特殊边两边有格子并且两个黑格子都在的时候黄蓝不能同在,所以(黄---黑)(黑---蓝)(黑---黑)都是最大权闭合子图中的依赖边

直接按照模型建就行,把黄蓝当成黑白染色。

……但是为什么不能用struct代替map呢QAQ

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int N=100005,mv[2][3][2]={-1,0,1,0,0,1,-1,0,1,0,0,-1},inf=1e9;
int c,r,n,s,t,le[N],h[N],cnt=1;
vector<int>v[N];
// struct que
// {
// int x,y;
// que(int X=0,int Y=0)
// {
// x=X,y=Y;
// }
// bool operator < (const que &a) const
// {
// return x>a.x;
// }
// };
// map<que,que>mp;
map< pair<int,int>,pair<int,int> > mp;
struct qwe
{
int ne,to,va;
}e[N*20];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
void ins(int u,int v,int w)
{//cout<<u<<" "<<v<<" "<<w<<endl;
add(u,v,w);
add(v,u,0);
}
int bfs()
{
queue<int>q;
memset(le,0,sizeof(le));
le[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=h[u];i;i=e[i].ne)
if(e[i].va>0&&!le[e[i].to])
{
le[e[i].to]=le[u]+1;
q.push(e[i].to);
}
}
return le[t];
}
int dfs(int u,int f)
{
if(!f||u==t)
return f;
int us=0;
for(int i=h[u];i;i=e[i].ne)
if(e[i].va>0&&le[e[i].to]==le[u]+1)
{
int t=dfs(e[i].to,min(e[i].va,f-us));
e[i].va-=t;
e[i^1].va+=t;
us+=t;
}
if(!us)
le[u]=0;
return us;
}
int dinic()
{
int re=0;
while(bfs())
re+=dfs(s,inf);
return re;
}
int main()
{
c=read(),r=read(),n=read();
s=0,t=n+1;
for(int i=1;i<=n;i++)
{
int y=read(),x=read(),w=read();
// mp[que(x,y)]=que(i,w);
// v[x].push_back(y);
mp[make_pair(x,y)]=make_pair(i,w);
v[x].push_back(y);
}
for(int i=1;i<=r;i++)
sort(v[i].begin(),v[i].end());
for(int i=1;i<=r;i++)
for(int j=0;j<v[i].size();j++)
{
int x=i,y=v[i][j];
// if((x&1)&&y%4==1)
// {
// if(j<v[i].size()-1&&v[i][j+1]==y+1)
// ins(mp[que(x,y)].x,mp[que(x,y+1)].x,min(mp[que(x,y)].y,mp[que(x,y+1)].y));
// }
// else if((x&1)&&y%4==2)
// {
// for(int k=0;k<3;k++)
// if(mp.find(que(x+mv[0][k][0],y+mv[0][k][1]))!=mp.end())
// ins(mp[que(x,y)].x,mp[que(x+mv[0][k][0],y+mv[0][k][1])].x,inf);
// }
// else if((x&1)==0&&y%4==0)
// {
// if(j>0&&v[i][j-1]==y-1)
// ins(mp[que(x,y)].x,mp[que(x,y-1)].x,min(mp[que(x,y)].y,mp[que(x,y-1)].y));
// }
// else if((x&1)==0&&y%4==3)
// {
// for(int k=0;k<3;k++)
// if(mp.find(que(x+mv[1][k][0],y+mv[1][k][1]))!=mp.end())
// ins(mp[que(x,y)].x,mp[que(x+mv[1][k][0],y+mv[1][k][1])].x,inf);
// }
// else if(((x+y)&1)&&(x&1))
// {
// for(int k=0;k<3;k++)
// if(mp.find(que(x+mv[0][k][0],y+mv[0][k][1]))!=mp.end())
// ins(mp[que(x,y)].x,mp[que(x+mv[0][k][0],y+mv[0][k][1])].x,inf);
// ins(s,mp[que(x,y)].x,mp[que(x,y)].y);
// }
// else if((x&1)&&((x+y)&1)==0)
// {
// for(int k=0;k<3;k++)
// if(mp.find(que(x+mv[1][k][0],y+mv[1][k][1]))!=mp.end())
// ins(mp[que(x+mv[1][k][0],y+mv[1][k][1])].x,mp[que(x,y)].x,inf);
// ins(mp[que(x,y)].x,t,mp[que(x,y)].y);
// }
// else if(((x+y)&1)&&(x&1)==0)
// {
// for(int k=0;k<3;k++)
// if(mp.find(que(x+mv[1][k][0],y+mv[1][k][1]))!=mp.end())
// ins(mp[que(x,y)].x,mp[que(x+mv[1][k][0],y+mv[1][k][1])].x,inf);
// ins(s,mp[que(x,y)].x,mp[que(x,y)].y);
// }
// else
// {
// for(int k=0;k<3;k++)
// if(mp.find(que(x+mv[0][k][0],y+mv[0][k][1]))!=mp.end())
// ins(mp[que(x+mv[0][k][0],y+mv[0][k][1])].x,mp[que(x,y)].x,inf);
// ins(mp[que(x,y)].x,t,mp[que(x,y)].y);
// }
if((x&1)&&y%4==1)
{
if(j<v[i].size()-1&&v[i][j+1]==y+1)
ins(mp[make_pair(x,y)].first,mp[make_pair(x,y+1)].first,min(mp[make_pair(x,y)].second,mp[make_pair(x,y+1)].second));
}
else if((x&1)&&y%4==2)
{
for(int k=0; k<3; k++)
if(mp.find(make_pair(x+mv[0][k][0],y+mv[0][k][1]))!=mp.end())
ins(mp[make_pair(x,y)].first,mp[make_pair(x+mv[0][k][0],y+mv[0][k][1])].first,inf);
}
else if((x&1)==0&&y%4==0)
{
if(j>0&&v[i][j-1]==y-1)
ins(mp[make_pair(x,y)].first,mp[make_pair(x,y-1)].first,min(mp[make_pair(x,y)].second,mp[make_pair(x,y-1)].second));
}
else if((x&1)==0&&y%4==3)
{
for(int k=0; k<3; k++)
if(mp.find(make_pair(x+mv[1][k][0],y+mv[1][k][1]))!=mp.end())
ins(mp[make_pair(x,y)].first,mp[make_pair(x+mv[1][k][0],y+mv[1][k][1])].first,inf);
}
else if(((x+y)&1)&&(x&1))
{
for(int k=0; k<3; k++)
if(mp.find(make_pair(x+mv[0][k][0],y+mv[0][k][1]))!=mp.end())
ins(mp[make_pair(x,y)].first,mp[make_pair(x+mv[0][k][0],y+mv[0][k][1])].first,inf);
ins(s,mp[make_pair(x,y)].first,mp[make_pair(x,y)].second);
}
else if((x&1)&&((x+y)&1)==0)
{
for(int k=0; k<3; k++)
if(mp.find(make_pair(x+mv[1][k][0],y+mv[1][k][1]))!=mp.end())
ins(mp[make_pair(x+mv[1][k][0],y+mv[1][k][1])].first,mp[make_pair(x,y)].first,inf);
ins(mp[make_pair(x,y)].first,t,mp[make_pair(x,y)].second);
}
else if(((x+y)&1)&&(x&1)==0)
{
for(int k=0; k<3; k++)
if(mp.find(make_pair(x+mv[1][k][0],y+mv[1][k][1]))!=mp.end())
ins(mp[make_pair(x,y)].first,mp[make_pair(x+mv[1][k][0],y+mv[1][k][1])].first,inf);
ins(s,mp[make_pair(x,y)].first,mp[make_pair(x,y)].second);
}
else
{
for(int k=0; k<3; k++)
if(mp.find(make_pair(x+mv[0][k][0],y+mv[0][k][1]))!=mp.end())
ins(mp[make_pair(x+mv[0][k][0],y+mv[0][k][1])].first,mp[make_pair(x,y)].first,inf);
ins(mp[make_pair(x,y)].first,t,mp[make_pair(x,y)].second);
}
}
printf("%d\n",dinic());
return 0;
}

最新文章

  1. Mac上idea快捷键
  2. VS2012 InstallShield2013LimitedEdition激活
  3. 【HDU3652】B-number 数位DP
  4. MQ使用几个命令
  5. (转载)目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结
  6. devexpress中gridcontrol 一些样式改变
  7. java ,js获取web工程路径
  8. FFT(快速傅里叶变换):HDU 5307 He is Flying
  9. Go语言中怎样判断数据类型_不懂的木匠_新浪博客
  10. Unity 3D Framework Designing(8)——使用ServiceLocator实现对象的注入
  11. Python初识--基础
  12. mac-os安装autojump
  13. 购物车自己sql错误
  14. JS监听对象属性改变
  15. partition分区
  16. caffe分类
  17. 导入tensorflow时DLL load failed: 找不到指定的模块
  18. Python第6天
  19. Create C++ Class
  20. Codeforces 1045B Space Isaac

热门文章

  1. 从零开始写STL—模板元编程之tuple
  2. MongoDB学习day09--Mongoose数据校验
  3. zoom to raster resolution
  4. Meteor ToDo App实例
  5. POJ 2545+2591+2247+1338简单水题
  6. 一起talk C栗子吧(第一百二十四回:C语言实例--内置宏)
  7. shell选择语句、循环语句
  8. java基础入门-建立能够多client链接的ServerSocket
  9. 提高比特率 有损 无损 Video-and-Audio-file-format-conversion 视频声音转码
  10. Supporting IPv6-only Networks