标签:网络流
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18057 | Accepted: 8210 |
Description
Input
Output
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
Source
/* *********************************************** Author :xdlove Created Time :2015年07月14日 星期二 14时34分27秒 File Name :a.cpp ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; typedef long long ll; const int MAXN = 3000; const int MAXM = 1e5 + 5; const int INF = 0x3f3f3f3f; struct Node { int from,to,next; int cap; }edge[MAXM * 2]; int tol; int Head[MAXN]; int que[MAXN]; int dep[MAXN]; //dep为点的层次 int stack[MAXN];//stack为栈,存储当前增广路 int cur[MAXN];//存储当前点的后继 void Init() { tol = 0; memset(Head,-1,sizeof(Head)); } void add_edge(int u, int v, int w) { edge[tol].from = u; edge[tol].to = v; edge[tol].cap = w; edge[tol].next = Head[u]; Head[u] = tol++; edge[tol].from = v; edge[tol].to = u; edge[tol].cap = 0; edge[tol].next = Head[v]; Head[v] = tol++; } int BFS(int start, int end) { int front, rear; front = rear = 0; memset(dep, -1, sizeof(dep)); que[rear++] = start; dep[start] = 0; while (front != rear) { int u = que[front++]; if (front == MAXN)front = 0; for (int i = Head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (edge[i].cap > 0 && dep[v] == -1) { dep[v] = dep[u] + 1; que[rear++] = v; if (rear >= MAXN)rear = 0; if (v == end)return 1; } } } return 0; } int dinic(int start, int end) { int res = 0; int top; while (BFS(start, end)) { memcpy(cur, Head, sizeof(Head)); int u = start; top = 0; while (true) { if (u == end) { int min = INF; int loc; for (int i = 0; i < top; i++) if (min > edge[stack[i]].cap) { min = edge[stack[i]].cap; loc = i; } for (int i = 0; i < top; i++) { edge[stack[i]].cap -= min; edge[stack[i] ^ 1].cap += min; } res += min; top = loc; u = edge[stack[top]].from; } for (int i = cur[u]; i != -1; cur[u] = i = edge[i].next) if (edge[i].cap != 0 && dep[u] + 1 == dep[edge[i].to]) break; if (cur[u] != -1) { stack[top++] = cur[u]; u = edge[cur[u]].to; } else { if (top == 0)break; dep[u] = -1; u = edge[stack[--top]].from; } } } return res; } int a[MAXN],Hash[MAXN]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n,m; while(~scanf("%d %d",&m,&n)) { Init(); int s = 0,t = n + m + 1; int node = t; for(int i = 1; i <= m; i++) { scanf("%d",&a[i]); Hash[i] = i + n; add_edge(i + n,t,a[i]); } for(int i = 1; i <= n; i++) { int x,v; scanf("%d",&x); node++; while(x--) { scanf("%d",&v); add_edge(node,Hash[v],INF); Hash[v] = node; } scanf("%d",&x); add_edge(s,i,x); add_edge(i,node,INF); } printf("%d\n",dinic(s,t)); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:网络流
原文地址:http://blog.csdn.net/zsgg_acm/article/details/47101789