dinic模板:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL long long
typedef pair<int,int> pii;
typedef pair<LL,int> pli;
typedef pair<LL,LL> pll;
const int inf = 0x3f3f3f3f;
const int maxn = 1000+5;
LL mod = 1e9+7;
double eps = 0.00000001;
double PI = acos(-1);
int n,p,w[60],in[60][12],out[60][12];
struct edge{
int from,to,cap,flow;
};
struct Dinic{ //最大费用流模板
int n,m,s,t;
vector<edge> edges;
vector<int> g[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n){
this->n=n;
edges.clear();
for(int i=0;i<=n;i++)
g[i].clear();
}
void addedge(int from,int to,int cap){
edges.push_back((edge){from,to,cap,0});
edges.push_back((edge){to,from,0,0});
m=edges.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
bool bfs(){
memset(vis,0,sizeof(vis));
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
vis[s]=true;
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]=true;
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 maxflow(int s,int t){
this->s=s,this->t=t;
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,inf);
}
return flow;
}
};
int check(int i,int j) {
for(int k = 1;k <= p;k++) {
if(in[j][k] != 2 && out[i][k] != in[j][k]) return 0;
}
return 1;
}
int main() {
while(scanf("%d%d",&p,&n) != EOF) {
Dinic din;
int s = 0,t = 2*n+1;
din.init(t);
for(int i = 1;i <= n;i++) {
scanf("%d",&w[i]);
for(int j = 1;j <= p;j++) scanf("%d",&in[i][j]);
for(int j = 1;j <= p;j++) scanf("%d",&out[i][j]);
}
for(int i = 1;i <= n;i++) {
din.addedge(i,i+n,w[i]);
int ff1 = 0,ff2 = 0;
for(int j = 1;j <= p;j++) {
if(in[i][j] == 1) ff1 = 1;
if(out[i][j] != 1) ff2 = 1;
}
if(ff1 == 0) din.addedge(s,i,inf);
if(ff2 == 0) din.addedge(i+n,t,inf);
for(int j = 1;j <= n;j++) {
if(i == j) continue;
if(check(i,j)) din.addedge(i+n,j,inf);
}
}
int flow = din.maxflow(s,t),cnt = 0;
for(int i = 0;i < din.edges.size();i++) {
edge u = din.edges[i];
if(u.from == s || u.to == s || u.from == t || u.to == t) continue;
if(u.from == (u.to+n) || u.from == (u.to-n)) continue;
if(u.flow < 0) cnt++;
}
printf("%d %d\n",flow,cnt);
//printf("%d------%d\n",s,t);
for(int i = 0;i < din.edges.size();i++) {
edge u = din.edges[i];
if(u.from == s || u.to == s || u.from == t || u.to == t) continue;
if(u.from == u.to+n || u.from == u.to-n) continue;
if(u.flow < 0) printf("%d %d %d\n",u.to-n,u.from,-u.flow);
}
}
}