码风良好,求指出错误
查看原帖
码风良好,求指出错误
1193763
Shepherdzzx楼主2024/10/8 10:59
#include <iostream>
#include <stack>
#include <cmath>
#include <map>
#include <fstream>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstring>
#include <iomanip>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define ld long double
#define vcmp vector<vector<ll>>
#define vec vector<ll>
using namespace std;
const ll INF = 1e9;
const int maxv = 1000;
const int maxe = 100100;
struct edge
{
	int from, to, flow;
	edge *rev;
	edge *next;
};
edge E[maxe];
edge *head[maxv];
edge *cur[maxv];
edge *top = E;
int n, m;
int s = 0, t = 1e5;
int depth[maxv];
int fa[maxe];
int time1;
int k;
int cap[maxe];
int num[maxe];
vector<int> v[maxv];
int find(int x)
{
	if (fa[x] == x)
		return x;
	else
		return fa[x] = find(fa[x]);
}
void unionn(int x, int y)
{
	if (find(x) != find(y))
		fa[find(x)] = find(y);
}
bool bfs(int s, int t)
{
	memset(depth, 0, sizeof(depth));
	queue<int> q;
	q.push(s);
	depth[s] = 1;
	cur[s] = head[s];
	while (!q.empty())
	{
		s = q.front();
		q.pop();
		for (edge *i = head[s]; i != NULL; i = i->next)
		{
			if (i->flow > 0 && depth[i->to] == 0)
			{
				depth[i->to] = depth[s] + 1;
				cur[i->to] = head[i->to];
				if (i->to == t)
					return true;
				q.push(i->to);
			}
		}
	}
	return false;
}
ll dfs(int s, int t, ll flow)
{
	if (s == t || flow < 0)
		return flow;
	ll rest = flow;
	for (edge *&i = cur[s]; i != NULL; i = i->next)
	{
		if (i->flow > 0 && depth[i->to] == depth[s] + 1)
		{
			ll tmp = dfs(i->to, t, min(rest, (ll)i->flow));
			if (tmp <= 0)
				depth[i->to] = 0;
			rest -= tmp;
			i->flow -= tmp;
			i->rev->flow += tmp;
			if (rest <= 0)
				break;
		}
	}
	return flow - rest;
}
void insert(int from, int to, int flow)
{
	top->from = from;
	top->to = to;
	top->flow = flow;
	top->rev = top + 1;
	top->next = head[from];
	head[from] = top++;

	top->from = to;
	top->to = from;
	top->flow = 0;
	top->rev = top - 1;
	top->next = head[to];
	head[to] = top++;
}
ll dinic(int s, int t)
{
	ll ans = 0;
	while (bfs(s, t))
	{
		ans += dfs(s, t, INF);
	}
	return ans;
}
void solve()
{
	cin >> n >> m >> k;
	for (int i = 1; i <= n + 2; i++)
		fa[i] = i;
	for (int i = 1; i <= m; i++)
	{
		cin >> cap[i] >> num[i];
		int index;
		for (int j = 1; j <= num[i]; j++)
		{
			cin >> index;
			if (index == -1)
				index = n + 2;
			if (index == 0)
				index = n + 1;
			v[i].push_back(index);
			if (j != 1)
			{
				unionn(index, v[i][v[i].size() - 2]);
			}
		}
	}
	if (find(n + 1) != find(n + 2))
	{
		cout << 0 << endl;
		return;
	}
	n += 2;
	insert(s, n - 1, INF);
	insert(n, t, INF);
	int sum = 0;
	while (++time1)
	{
		for (int i = 1; i <= m; i++)
		{
			int now = v[i][time1 % num[i]];
			int pre = v[i][(time1 - 1 + num[i]) % num[i]];
			insert(n * (time1 - 1) + pre, n * time1 + now, cap[i]);
		}
		insert(s, n * time1 + n - 1, INF);
		insert(n * time1 + n, t, INF);
		for (int i = 1; i <= n; i++)
			insert((time1 - 1) * n + i, time1 * n + i, INF);
		sum += dinic(s, t);
		if (sum >= k)
		{
			cout << time1;
			return;
		}
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll T = 1;
	// cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}

数据2: 2 3 3 1 2 0 2 1 2 1 2 1 2 1 -1 答案是7。 但是我的代码在残余网络塞边进去后无法更新新的流量,导致得不到答案,想知道我的代码哪里有问题

2024/10/8 10:59
加载中...