首页 > 其他分享 >POI2006SZK-Schools

POI2006SZK-Schools

时间:2024-04-15 19:44:45浏览次数:17  
标签:MCMF int eg edges Schools dis POI2006SZK define

费用流 #POI #Year2006

费用流模板题,暴力建图,限制一下流量

// Author: xiaruize
#ifndef ONLINE_JUDGE
#define debug(x) cerr << "On Line:" << __LINE__ << #x << "=" << x << endl
bool start_of_memory_use;
#else
#define debug(x)
#endif
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
clock_t start_clock = clock();
#endif
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define rep(i, a, n) for (int i = (a); i <= (n); ++i)
#define per(i, n, a) for (int i = (n); i >= (a); --i)
int max(int a, int b)

{
	if (a > b)
		return a;
	return b;
}
int min(int a, int b)
{
	if (a < b)
		return a;
	return b;
}
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

namespace MCMF
{
	const int MAXN = 1e3, MAXM = 1e6, INF = 0x7fffffff;
	int head[MAXN], cnt = 1;
	struct Edge
	{
		int to, w, c, next;
	} edges[MAXM * 2];
	inline void add(int from, int to, int w, int c)
	{
		edges[++cnt] = {to, w, c, head[from]};
		head[from] = cnt;
	}
	inline void addEdge(int from, int to, int w, int c)
	{
		add(from, to, w, c);
		add(to, from, 0, -c);
	}
	int s, t, dis[MAXN], cur[MAXN];
	bool inq[MAXN], vis[MAXN];
	queue<int> Q;
	bool SPFA()
	{
		while (!Q.empty())
			Q.pop();
		copy(head, head + MAXN, cur);
		fill(dis, dis + MAXN, INF);
		dis[s] = 0;
		Q.push(s);
		while (!Q.empty())
		{
			int p = Q.front();
			Q.pop();
			inq[p] = 0;
			for (int e = head[p]; e != 0; e = edges[e].next)
			{
				int to = edges[e].to, vol = edges[e].w;
				if (vol > 0 && dis[to] > dis[p] + edges[e].c)
				{
					dis[to] = dis[p] + edges[e].c;
					if (!inq[to])
					{
						Q.push(to);
						inq[to] = 1;
					}
				}
			}
		}
		return dis[t] != INF;
	}
	int dfs(int p = s, int flow = INF)
	{
		if (p == t)
			return flow;
		vis[p] = 1;
		int rmn = flow;
		for (int eg = cur[p]; eg && rmn; eg = edges[eg].next)
		{
			cur[p] = eg;
			int to = edges[eg].to, vol = edges[eg].w;
			if (vol > 0 && !vis[to] && dis[to] == dis[p] + edges[eg].c)
			{
				int c = dfs(to, min(vol, rmn));
				rmn -= c;
				edges[eg].w -= c;
				edges[eg ^ 1].w += c;
			}
		}
		vis[p] = 0;
		return flow - rmn;
	}
	int maxflow, mincost;
	inline void run(int s, int t)
	{
		MCMF::s = s, MCMF::t = t;
		while (SPFA())
		{
			int flow = dfs();
			maxflow += flow;
			mincost += dis[t] * flow;
		}
	}
} // namespace MCMF

int n;

void solve()
{
	cin >> n;
	rep(i, 1, n)
	{
		MCMF::addEdge(0, i, 1, 0);
		int m, a, b, k;
		cin >> m >> a >> b >> k;
		rep(j, a, b)
		{
			MCMF::addEdge(i, j + n, 1, abs(j - m) * k);
		}
	}
	rep(i, 1, 200)
	{
		MCMF::addEdge(i + n, 600, 1, 0);
	}
	MCMF::run(0, 600);
	if (MCMF::maxflow != n)
		cout << "NIE" << endl;
	else
		cout << MCMF::mincost << endl;
}

#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif

signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
		solve();
#ifndef ONLINE_JUDGE
	cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
	cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
	return 0;
}

标签:MCMF,int,eg,edges,Schools,dis,POI2006SZK,define
From: https://www.cnblogs.com/xiaruize/p/18136792

相关文章

  • P2746 [USACO5.3] 校园网Network of Schools 题解
    题目链接:校园网NetworkofSchools这个题得翻译下题目意思才知道在干嘛,题目一开始表明了这个是一个有向图,因为边是单向的。其次关于第一个问题:基于一个事实,如果有\(x\rightarrowy\rightarrowz\),那么只需要\(x\)接受协议,它所在的\(scc\)强连通分量上的点一定都能不需要......
  • P2746 [USACO5.3] 校园网Network of Schools
    原题链接题解把奶牛看成点,赠送列表关系看成有向边,这样这道题就成了对强连通分量缩点,然后找出这个新图中入度为零的点有几个,出度为零的点有几个code#include<bits/stdc++.h>usingnamespacestd;vector<int>G[105];intlen=0,cnt=0;intbelong[105]={0};intin[105]={0},......
  • 2019考研英语(一)小作文真题解析与参考 Aiding Rural Primary Schools 答复信
    2019考研英语(一)小作文真题解析与参考Directions:Supposeyouareworkingforthe“AidingRuralPrimarySchools” projectofyouruniversity.Writeanemailtoanswertheinquiryfromaninternationalschoolvolunteer,specifyingthedetailsoftheproject.Yous......
  • PHP的验证码实现(w3schools推荐)
    本文使用PHP一些可用的特性实现了验证码功能。该教程非常的简单,使用可以改变的字体生成了验证码图片,正如我们所了解的,验证码是用于避免垃圾评论或者自动提交的。本验证码程......
  • 缩点 P2812 校园网络【[USACO]Network of Schools加强版】
    首先找出图中的强连通分量,用tarjan算法。强连通分量内部强联通,所以将其看成一个点是不影响的。进行缩点之后,整张图变成了一个有向无环图。首先对于每一条边进行检测,如果......