码迷,mamicode.com
首页 > 其他好文 > 详细

E. Cheap Dinner from Educational Codeforces Round 104 (Rated for Div. 2)

时间:2021-02-23 14:09:14      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:add   cto   code   rate   str   else   node   fas   space   

题意:有4种菜,每种菜有n1,n2,n3,n4个,每个菜有一个价格。

需要你每种菜选1个,问最小价格。但1和2、2和3、3和4之间有些菜有1对1的互斥关系,如果有互斥则不能选。

做法:把每两组贪心线性预处理,最后合并即可

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define vll vector<ll>
#define vpll vector<pll>
#define fastio ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
double pi = acos(-1);
const double eps = 1e-9;
const int inf = 1e9 + 7;
const ll lnf = 1e18 + 7;
const int maxn = 2e5+ 10;
ll mod = 1e9 + 7;

int head[maxn], edge_cnt = 0;

struct edge {
	int to, next;
}e[maxn << 1];

inline void add(int from, int to)
{
	e[++edge_cnt] = { to,head[from] };
	head[from] = edge_cnt;
}
struct Node
{
	int vue;
	int id;
	friend bool operator < (Node x,Node y)
	{
		return x.vue < y.vue;
	}
}a[5][maxn];

set<int>s[5][maxn];

int main()
{
	fastio;
		int n1, n2, n3, n4;
		cin >> n1 >> n2 >> n3 >> n4;
		for (int i = 1; i <= n1; i++)cin >> a[1][i].vue,a[1][i].id=i ,s[1][i].clear();
		for (int i = 1; i <= n2; i++)cin >> a[2][i].vue,a[2][i].id=i ,s[2][i].clear();
		for (int i = 1; i <= n3; i++)cin >> a[3][i].vue,a[3][i].id=i ,s[3][i].clear();
		for (int i = 1; i <= n4; i++)cin >> a[4][i].vue,a[4][i].id=i ,s[4][i].clear();

		int m;
		cin >> m;
		while (m--)
		{
			int x, y;
			cin >> x >> y;
			s[1][y].insert(x);
		}
		cin >> m;
		while (m--)
		{
			int x, y;
			cin >> x >> y;
			s[2][y].insert(x);
		}
		cin >> m;
		while (m--)
		{
			int x, y;
			cin >> x >> y;
			s[3][y].insert(x);
		}
		sort(a[1] + 1, a[1] + n1 + 1);
		for (int i = 1; i <= n2; i++)
		{
			bool flag = 0;
			for (int cnt = 1; cnt <= n1; cnt++)
			{
				int id = a[1][cnt].id;
				if (s[1][i].find(id) == s[1][i].end())
				{
					a[2][i].vue += a[1][cnt].vue;
					flag = 1;
					break;
				}
			}
			if (!flag)a[2][i].vue = inf;
		}
		sort(a[2] + 1, a[2] + n2 + 1);
		for (int i = 1; i <= n3; i++)
		{
			bool flag = 0;
			for (int cnt = 1; cnt <= n2; cnt++)
			{
				int id = a[2][cnt].id;
				if (a[2][cnt].vue == inf)break;
				if (s[2][i].find(id) == s[2][i].end())
				{
					a[3][i].vue += a[2][cnt].vue;
					flag = 1;
					break;
				}
			}
			if (!flag)a[3][i].vue = inf;
		}
		sort(a[3] + 1, a[3] + n3 + 1);
		int ans = inf;
		for (int i = 1; i <= n4; i++)
		{
			bool flag = 0;
			for (int cnt = 1; cnt <= n3; cnt++)
			{
				if (a[3][cnt].vue == inf)break;
				int id = a[3][cnt].id;
				if (s[3][i].find(id) == s[3][i].end())
				{
					a[4][i].vue += a[3][cnt].vue;
					flag = 1;
					break;
				}
			}
			if (flag)ans = min(ans, a[4][i].vue);
		}
		if (ans == inf)
			cout << -1 << endl;
		else cout << ans << endl;

	return 0;

}

E. Cheap Dinner from Educational Codeforces Round 104 (Rated for Div. 2)

标签:add   cto   code   rate   str   else   node   fas   space   

原文地址:https://www.cnblogs.com/ruanbaiQAQ/p/14429230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!