标签:style blog class code java color
上次我说用STL超时了,而用数组为0ms,其实不然,这个题STL依然不超时,代码如下
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; const double pi=acos(-1.0); const double eps=1e-8; typedef pair<int,int>pii; const int maxn=2500+10; struct Edge { int from,to,cap,flow; }; int n,m,s,t;//n是定点数,m是边数,s,t分别是源汇点 vector<Edge>edges; vector<int>G[maxn]; int d[maxn],cur[maxn]; bool vis[maxn]; void AddEdge(int from,int to,int cap) { Edge temp; temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to; edges.push_back(temp); temp.cap=0; temp.flow=0; temp.from=to; temp.to=from; edges.push_back(temp); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { memset(vis,0,sizeof(vis)); queue<int>Q; Q.push(s); d[s]=0; vis[s]=1; while(!Q.empty()) { int x=Q.front();Q.pop(); for (int i=0;i<G[x].size();i++) { Edge& e=edges[G[x][i]]; if (!vis[e.to] && e.cap>e.flow) { vis[e.to]=1; d[e.to]=d[x]+1; Q.push(e.to); } } } return vis[t]; } int DFS(int x,int a) { if (x==t || a==0) return a; int flow=0,f; for (int& i=cur[x];i<G[x].size();i++) { Edge& e=edges[G[x][i]]; if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0) { e.flow+=f; edges[G[x][i]^1].flow-=f; flow+=f; a-=f; if (a==0) break; } } return flow; } int Dinic() { int flow=0; while (BFS()) { memset(cur,0,sizeof(cur)); flow+=DFS(s,inf); } return flow; } void init() { for (int i=0;i<=maxn;i++) G[i].clear(); edges.clear(); } int main() { //freopen("in.txt","r",stdin); int N,M,a,sum; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; while (scanf("%d%d",&N,&M)!=EOF) { init(); s=0;t=N*M+1; sum=0; for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) { scanf("%d",&a); sum+=a; int x=(i-1)*M+j; if ((i+j)%2==0) { AddEdge(s,x,a); for (int k=0;k<4;k++) { int xx=i+dx[k]; int yy=j+dy[k]; if (xx>=1 && xx<=N && yy>=1 && yy<=M) { int y=(xx-1)*M+yy; AddEdge(x,y,inf); } } } else { AddEdge(x,t,a); for (int k=0;k<4;k++) { int xx=i+dx[k]; int yy=j+dy[k]; if (xx>=1 && xx<=N && yy>=1 && yy<=M) { int y=(xx-1)*M+yy; AddEdge(y,x,inf); } } } } printf("%d\n",sum-Dinic()); } //fclose(stdin); return 0; }
hdu 1569 方格取数(2)再解,布布扣,bubuko.com
标签:style blog class code java color
原文地址:http://www.cnblogs.com/chensunrise/p/3705563.html