poj 169 分类: poj 2015-04-03 17:04 39人阅读 评论(0) 收藏
Dinic网络流,建图很简单,主要是搞个模板。。。
#include<map>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
const int EdgeNum = 1e5+5 , NodeNum = 1e3+5 ,INF = 2e8;
const int ni[2] = {-1,1};
const int MAXD = 55, MAXW = 55;
int n , m ,ind = 0 , sum = 0;
int pot[MAXW*7] = {0}, pl;
int head[NodeNum] = {0}, el = 0;
struct FlowEdge{int v,cap,next;}edge[EdgeNum<<1] = {0},emp = {0};
int NewNode(){head[++ind] = 0;return ind;}
void NewEdge(int u,int v,int cap)
{
++el, edge[el].v = v;
edge[el].cap = cap;
edge[el].next = head[u];
head[u] = el;
}
void NewFlowEdge(int u,int v,int cap)
{
NewEdge(u,v,cap); NewEdge(v,u,0);
}
struct TwoNode{int in,out;};
struct DinicNetWork
{
int start , end;
int dep[NodeNum];
int line[NodeNum], f, r;
bool BFS()
{
f = r = 0;
for(int i = 0; i <= ind; i++) dep[i] = 0;
line[r++] = start;
dep[start] = 1;
while(f!=r)
{
int now = line[f++];
for(int i = head[now]; i ; i = edge[i].next)
if(edge[i].cap)
{
int p = edge[i].v;
if(dep[p])continue;
else
{
dep[p] = dep[now]+1;
line[r++] = p;
}
}
}
if(dep[end])return true;
else return false;
}
int DFS(int a,int flow)
{
if(!flow || a == end)return flow;
int ret = 0;
for(int i = head[a]; i && flow; i = edge[i].next)
{
int p = edge[i].v;
if(dep[p] != dep[a] + 1 || !edge[i].cap)continue;
int ff = DFS(p,std::min(flow,edge[i].cap));
edge[i].cap -= ff ;
edge[i+ni[i&1]].cap += ff;
flow -= ff, ret += ff;
}
return ret;
}
int mainwork()
{
int totflow = 0;
while(1)//dinic
{
if(BFS() == false)break;
// memset(hash,false,sizeof(hash));
totflow += DFS(start,INF);
}
return totflow;
}
}Net = {0};
int main()
{
int T;
#ifndef ONLINE_JUDGE
freopen("poj1698.in","r",stdin);
freopen("poj1698.out","w",stdout);
#endif
scanf("%d",&T);
while(T--)
{
sum = ind = el = pl = 0;
memset(pot,0,sizeof(pot));
scanf("%d",&m);
Net.start = NewNode();
Net.end = NewNode();
for(int i = 1; i <= m; i++)
{
int d, w;static int fi[10];
for(int j = 1; j <= 7; j++)
scanf("%d",&fi[j]);
scanf("%d%d",&d,&w); sum += d;
int miv = NewNode(); NewFlowEdge(Net.start,miv,d);
for(int j = 1; j <= 7; j++)
{
if(fi[j] == 0)continue;
for(int k = 0; k < w; k++)
{
int si = k*7+j;
if(!pot[si]){pot[si] = NewNode();}
pl = std::max(pl,si);
NewFlowEdge(miv,pot[si],1);
}
}
}
for(int i = 1; i <= pl; i++)
if(pot[i])NewFlowEdge(pot[i],Net.end,1);
if(Net.mainwork() == sum)puts("Yes");
else puts("No");
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。