【二进制优化】【Dij+堆优化】险路勿近

19 篇文章 0 订阅

转载于lth

链接

险路勿近

题目描述

给出一张n点m边的图,求一个不经过重边的的经过1的环

思路

在这里插入图片描述
以上是Quant_Ask大佬的题解
以下为补充解释
其实就是枚举一个二进制位,然后把边分为两份,再找出出边,入边,把入边连向一个新点T,然后跑最短路就可以了

感谢Quant_Ask帮忙改题(数组开小也是没谁了

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#define ll long long

using namespace std;

priority_queue<pair<ll , ll> > Q;

ll n, m, ans = 1e9, t, p;
ll h[10005], dis[400005];
ll log[400005], vis[10005];
ll ch[400005], ru[400005];//ch记录出边,ru记录入边
bool vi[400005];//用来防止有重边

struct node
{
	ll fro, to, next, val;
}g[400005];

ll dij(int s, int tt)
{	
	memset(dis, 0x7f, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	dis[s] = 0;
	Q.push(make_pair(0, s));
	while(Q.size())
	{
		int tot = Q.top().second;
		Q.pop();
		if(vis[tot]) continue;
		vis[tot] = 1;
		for(int i = h[tot]; i; i = g[i].next)
		{
			if(g[i].fro == 1 && !ch[i]) continue;
			int to = g[i].to;
			if(to == 1) continue;
			if(dis[to] > dis[tot] + g[i].val && !vis[to])
				dis[to] = dis[tot] + g[i].val,
				Q.push(make_pair(-dis[to], to));
		}
	}
	return dis[tt];
}//Dij+堆优化

void add(int x, int y, ll val, ll vval)
{
	g[++t] = (node){x, y, h[x], val}; h[x] = t;
	g[++t] = (node){y, x, h[y], vval}; h[y] = t;
}

int main()
{
	scanf("%lld%lld", &n, &m);
	log[0] = -1;
	for(int i = 1; i <= 2 * m; ++i)
		log[i] = log[i >> 1] + 1;
	for(int i = 1; i <= m; ++i)	
	{
		int x, y;
		ll w, v;
		scanf("%d%d%lld%lld", &x, &y, &w, &v);
		add(x, y, w, v);
	}
	for(int i = 0; i <= log[t]; ++i) {
		int numm = 0;
		memset(ch, 0, sizeof(ch));
		memset(vi, 0, sizeof(vi));
		for(int j = 1; j <= t; ++j)
		{
			if((j & (1 << i)) && g[j].fro == 1 && !vi[g[j].to]) 
				ch[j] = 1, vi[g[j].to] = 1;
			else if(!(j & (1 << i)) && g[j].to == 1 && !vi[g[j].fro]) {
				ru[++numm] = j;
				g[j].to = n + 1;
				vi[g[j].fro] = 1;
			}
		}//第i位为1为出边,为0为入边
		ans = min(dij(1, n + 1), ans);
		for(int j = 1; j <= numm; ++j)
			g[ru[j]].to = 1;
		memset(vi, 0, sizeof(vi));
		memset(ch, 0, sizeof(ch));
		
		numm = 0;
		for(int j = 1; j <= t; ++j)
		{
			if(!(j & (1 << i)) && g[j].fro == 1 && !vi[g[j].to]) 
				ch[j] = 1, vi[g[j].to] = 1;
			else if((j & (1 << i)) && g[j].to == 1 && !vi[g[j].fro]) {
				ru[++numm] = j;
				g[j].to = n + 1;
				vi[g[j].fro] = 1;
			}
		}
		ans = min(dij(1, n + 1), ans);
		for(int j = 1; j <= numm; ++j)
			g[ru[j]].to = 1;
	}
	printf("%lld", ans);
	return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用堆优化Dijkstra 算法来求解最短径的示例代码,其中使用了 vector 来表示图的邻接表: ```cpp #include <iostream> #include <vector> #include <queue> #include <limits> using namespace std; typedef pair<int, int> pii; const int INF = numeric_limits<int>::max(); vector<int> dijkstra(const vector<vector<pii>>& graph, int source) { int n = graph.size(); vector<int> dist(n, INF); dist[source] = 0; priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push({0, source}); while (!pq.empty()) { int u = pq.top().second; int d = pq.top().first; pq.pop(); if (d > dist[u]) { continue; // 已经找到了更短的径 } for (const auto& edge : graph[u]) { int v = edge.first; int w = edge.second; if (dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pq.push({dist[v], v}); } } } return dist; } int main() { int n = 5; // 图的顶点数 int m = 7; // 图的边数 vector<vector<pii>> graph(n); // 构建图的邻接表 graph[0].push_back({1, 2}); graph[0].push_back({2, 4}); graph[1].push_back({2, 1}); graph[1].push_back({3, 7}); graph[2].push_back({3, 3}); graph[2].push_back({4, 5}); graph[3].push_back({4, 2}); graph[4].push_back({3, 1}); int source = 0; vector<int> dist = dijkstra(graph, source); cout << "Shortest distances from node " << source << ":" << endl; for (int i = 0; i < n; ++i) { cout << "Node " << i << ": " << dist[i] << endl; } return 0; } ``` 上述代码中,我们使用堆优化Dijkstra 算法来找到从源节点到其他节点的最短距离。图的邻接关系使用 vector<vector<pii>> 来表示,其中 pii 表示边的目标节点和权重。你可以根据需要修改图的顶点数、边数和邻接表来适应不同的场景。输出结果将会显示源节点到其他节点的最短距离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值