ABC 395

目录

        D. Pigeon Swap

        E. Flip Edge 


D. Pigeon Swap

        给每个巢加一个牌子,在第二个操作的时候,对两个巢只要换牌子就可以了。因此每一次询问的时候,都是输出鸽子所在巢的牌子,而不是巢的编号。

        任何时候换巢的索引都是牌子。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5, INF = 1e18;

int T, n, q, ans, f[N], h[N], r[N];
string s;

signed main()
{
	cin >> n >> q;
	for (int i = 1; i <= n; i ++)
		f[i] = i, h[i] = i, r[i] = i;    // 鸟对应巢 巢对应牌号 牌号对应巢
	while (q --)
	{
		int opt, a, b;
		cin >> opt;
		if (opt == 1)
		{
			cin >> a >> b;
			f[a] = r[b];
		}
		if (opt == 2)
		{
			cin >> a >> b;
			swap(h[r[a]], h[r[b]]);
			swap(r[a], r[b]);
		}
		if (opt == 3)
		{
			cin >> a;
			cout << h[f[a]] << '\n';
		}
	}
	return 0;
}

E. Flip Edge 

        分层图最短路,建图方式是在前 n 个点是第一张图,n 到 2n 个点是第二张图。同时两张图的同一个点要连起来,第二张图的边要反过来。 

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5, INF = 1e18;

struct node
{
	int v, w;
};

int T, n, m, x, cnt, ans, d[N * 2], vis[N * 2];
vector<node> G[N * 2];
priority_queue<pair<int, int> > pq;

signed main()
{
	cin >> n >> m >> x;
	for (int i = 1; i <= m; i ++)
	{
		int u, v;
		cin >> u >> v;
		G[u].push_back({v, 1});
		G[v + n].push_back({u + n, 1});
	}
	for (int i = 1; i <= n; i ++)
	{
		G[i].push_back({i + n, x});
		G[i + n].push_back({i, x});
	}
	for (int i = 1; i <= 2 * n; i ++)
		d[i] = INF;
	d[1] = 0;
	pq.push({0, 1});
	while (!pq.empty())
	{
		auto t = pq.top();	pq.pop();
		int u = t.second;
		if (vis[u] != 0)
			continue;
		vis[u] = 1;
		for (auto ed : G[u])
		{
			int v = ed.v, w = ed.w;
			if (d[v] > d[u] + w)
			{
				d[v] = d[u] + w;
				pq.push({-d[v], v});
			}
		}
	}
	ans = min(d[n], d[2 * n]);
	cout << ans;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值