poj 169
来源:互联网 发布:网络晒长腿大赛 编辑:程序博客网 时间:2024/04/30 17:00
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;}
0 0
- poj 169
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- js 文件压缩
- Java线程池
- Flash and Scalform CLIK
- Android4.4 SystemUI分析之Clock时钟显示
- 再看数据库——(7)游标
- poj 169
- 51小东西霍尔计数器
- LeetCode --- 82. Remove Duplicates from Sorted List II
- 第四周程序阅读(4)
- 判断./configure,make等长输出命令是否执行成功
- 网络编程socket之listen函数
- win7 Qt-5.3.1 cmake-2.8.11.2 opencv-2.4.11平台搭建
- rabbitmq最大连接数(Socket Descriptors)(转)
- 拷贝构造函数