我正在尝试使用最大流量(Edmonds-Karp)算法来解决this problem(将类别中的某些类别中的问题分配给类别,以便每个类别包含所请求的问题数)回答错误
我的所作所为:
然后我得到了WA。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include<sstream>
#include<queue>
#include<vector>
#include<stdio.h>
#include<deque>
#include<string>
#include<cstdio>
#include<memory.h>
using namespace std;
int f,C, P;
vector<vector<int> > AdjList;
int Flow[1500][1500], p[1500];
void updatePath(int t, int minEdge) {
if (t == 1)
{
f = minEdge;
return;
}
else if (p[t] != -1) {
updatePath(p[t], min(minEdge, Flow[p[t]][t]));
Flow[p[t]][t] -= f;
Flow[t][p[t]] += f;
}
}
int Ed() {
// Note That The Source Equal ( 1 ) And The Sink Equal ( 2 )
int mf = 0;
while (1) {
memset(p, -1, sizeof p);
f = 0;
queue<int> q;q.push(1);
while (!q.empty()) {
int u = q.front();q.pop();
if (u == 2)break;
for (int i = 0; i < AdjList[u].size(); i++) {
int v = AdjList[u][i];
if (p[v] == -1 && Flow[u][v] > 0) {
p[v] = u;
q.push(v);
}
}
}
updatePath(2, (int)1e9);
if (!f)break;
mf += f;
}
return mf;
}
int main(){
//freopen("src.txt", "r", stdin);
while (scanf("%d%d", &C, &P) && (C || P)) {
AdjList.clear();
AdjList.resize(C + P + 10);
memset(Flow, 0, sizeof Flow);
int total = 0;
for (int i = 3 ;i <= C + 2; i++)
{
int z;scanf("%d", &z);
AdjList[1].push_back(i);
Flow[1][i] = z;
total += z;
}
for (int i = 0 ; i < P ; i++) {
int x;scanf("%d", &x);Flow[i + C + 3][2] = 1;AdjList[i + C + 3].push_back(2);
while (x--) {
int c;scanf("%d", &c);
Flow[c + 2][i + C + 3] = 1;
AdjList[c + 2].push_back(i + C + 3);
}
}
if (Ed() == total) {
printf("1\n");
for (int i = 3; i <= C+2 ; i++) {
bool c = 0;
for (int j = 0 ; j < P ; j++) {
if (Flow[j + C + 3][i]) {
if (c)printf(" ");
c = 1;
printf("%d", j + 1);
}
}
printf("\n");
}
}
else {
printf("0\n");
}
}
return 0;
}