LOJ 6002「网络流 24 题」最小路径覆盖

最小路径覆盖的模板题。但是我是在hihocoder上才学到这个模版的,hihocoder上面有最小路径覆盖浅显易懂的解释,非常棒!

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 10;
const int maxm = 30005;
int n, m;
vector<int>road[205];
int road_num = 0;

inline int read()
{
	int x=0,t=1,c;
	while(!isdigit(c=getchar()))if(c=='-')t=-1;
	while(isdigit(c))x=x*10+c-'0',c=getchar();
	return x*t;
}
int head[maxn],cur[maxn],nx[maxm<<1],to[maxm<<1],flow[maxm<<1],ppp=0;
struct Dinic
{
	int dis[maxn];
	int s, t;
	long long ans;
	
	void init() {
		memset(head, -1, sizeof(head));
		ppp = 0;
	}
	
	void AddEdge(int u, int v, int c)
	{
		to[ppp]=v;flow[ppp]=c;nx[ppp]=head[u];head[u]=ppp++;swap(u,v);
		to[ppp]=v;flow[ppp]=0;nx[ppp]=head[u];head[u]=ppp++;
	}
	
	bool BFS()
	{
		memset(dis, -1, sizeof(dis));
		dis[s] = 1; 
		queue<int> Q;
		Q.push(s);
		while(!Q.empty())
		{
			int x = Q.front();
			Q.pop();
			for(int i = head[x]; ~i; i = nx[i])
			{
				if(flow[i] && dis[to[i]] == -1)
				{
					dis[to[i]] = dis[x] + 1;
					Q.push(to[i]);
				}
			}
		}
		return dis[t] != -1;
	}
	
	int DFS(int x, int maxflow)
	{
		if(x == t || !maxflow){
			ans += maxflow;
			return maxflow;
		}
		int ret = 0, f;
		for(int &i = cur[x]; ~i; i = nx[i])
		{
			if(dis[to[i]] == dis[x] + 1 && (f = DFS(to[i], min(maxflow, flow[i]))))
			{
				ret += f;
				flow[i] -= f;
				flow[i^1] += f;
				maxflow -= f;
				if(!maxflow)
					break;
			}
		}
		return ret;
	}
	
	long long solve(int source, int tank)
	{
		s = source;
		t = tank;
		ans = 0;
		while(BFS())
		{
			memcpy(cur, head, sizeof(cur));
			DFS(s, INF);
		}
		return ans;
	}
}dinic;

void dfs(int x) {
	for(int i = head[x]; ~i; i = nx[i]) {
		int v = to[i];
		if(flow[i] == 0 && v != 0) {
			road[road_num].push_back(v / 2);
			dfs(v - 1);
		}
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	dinic.init();
	n = read(), m = read(); 
	while(m--) {
		int u = read(), v = read();
		dinic.AddEdge(u * 2, v * 2 + 1, 1);
	}
	for(int i = 1; i <= n; i++) {
		dinic.AddEdge(0, i * 2, 1);
		dinic.AddEdge(i * 2 + 1, 1, 1);
	}
	
	int ans = dinic.solve(0, 1);
	for(int i = 1; i <= n; i++) {
		bool flag = 0;
		for(int j = head[i * 2 + 1]; ~j; j = nx[j]) {
			int v = to[j];
			if(flow[j] && to[j] == 1) {
				flag = 1;
			}
		}
		if(flag) {
			road[road_num].push_back(i);
			dfs(i * 2);
			road_num++;
		}
	}
	
	for(int i = 0; i < road_num; i++) {
		sort(road[i].begin(), road[i].end());
		for(int j = 0; j < road[i].size(); j++) {
			printf("%d%c", road[i][j], j == road[i].size() - 1 ? '\n' : ' ');
		}
	}
	
	cout << n - ans << '\n';
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值