牛客练习赛93 C(树形dp? E(欧拉路径

题解

点权
思路:
写法类似迪杰斯特拉算法
首先叶子节点肯定是花费 0 0 0
开一个优先队列,然后从叶子节点开始拓展,对于每个节点维护两个值,花费的最小值和次小值,如果一个节点的点权可以变成 2 2 2 或者 变成 2 2 2 的花费更小了,它就可以加入优先队列去更新其他节点
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
vector <pair<int,int> > e[maxn];
int ans[maxn], f[maxn][2];

void work()
{	
	cin >> n;
	if(n == 1){
		cout << 0 << endl;return;
	}
	for(int i = 1; i < n; ++i){
		int x, y, z;cin >> x >> y >> z;
		e[x].push_back({y, z});
		e[y].push_back({x, z});
	}
	mem(f, 0x3f);mem(ans, 0x3f);
	priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > q;
	for(int i = 1; i <= n; ++i){
		if(int(e[i].size()) == 1){
			f[i][0] = f[i][1] = ans[i] = 0;
			q.push({0, i});
		}
	}
	while(!q.empty())
	{
		auto now = q.top();q.pop();
		int w = now.first, x = now.second;
		if(w > ans[x]) continue;
		for(auto d : e[x]){
			int ww = d.second, to = d.first;
			if(w + ww < f[to][0]){
				f[to][1] = f[to][0];
				f[to][0] = w + ww;
			}
			else if(w + ww < f[to][1]){
				f[to][1] = w + ww;
			}
			if(f[to][0] + f[to][1] < ans[to]){
				ans[to] = f[to][0] + f[to][1];
				q.push({ans[to], to});
			}
		}
	}
	for(int i = 1; i <= n; ++i)
		cout << (ans[i] == inf ? -1 : ans[i]) << " ";
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

牛牛选路径
题意:
牛牛有一张无向连通图,图上有 n n n 个点和 m m m 条边,每个点有一个权值 a i a_i ai
在这些路径覆盖每条边为奇数次的情况下,最小化所有路径价值和
一条路径的价值被定义为起点和终点两个节点的乘积
注意图中可能有重边,连接 a , b a,b a,b 两个顶点的所有重边是独立的
思路:
前置知识:
欧拉路径的判定
度数为奇数的点为奇点,称度数为偶数的点为偶点。
任意无向图中的奇点一定是偶数个:证明

没有奇点时,可以直接提取一条欧拉回路。
存在奇点时,由于奇点是成对存在的,所以我们可以把奇数度的点两两配对,局部形成欧拉回路
那么问题就变成了将奇点两两匹配,最小化乘积和 ∑ a i ∗ a j \sum a_i*a_j aiaj
显然做法是拿最小的去配对最大的,次小的配对次大的
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int a[maxn], deg[maxn];

void work()
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i){
		cin >> a[i];		
	}
	for(int i = 1, x, y; i <= m; ++i){
		cin >> x >> y;
		deg[x]++;deg[y]++;
	}
	vector <int> v;
	for(int i = 1; i <= n; ++i) if(deg[i] & 1)
		v.push_back(a[i]);
	if(v.empty()){
		int ans = inf;
		for(int i = 1; i <= n; ++i) ans = min(ans, a[i]);
		cout << ans * ans % mod << endl;
	}
	else
	{
		sort(all(v));
		int d = v.size();
		ll ans = 0;
		for(int i = 0; i < v.size() / 2; ++i)
			(ans += 1ll * v[i] * v[d - i - 1]) %= mod;
		cout << ans << endl;
	}
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值