ZOJ 2281 Way to Freedom

15 篇文章 0 订阅

dijkstra算法变形

dis[u]存的是到u的最小权值中的最大值.

松弛条件则变成了dis[v]  = max(min(dis[u], g[u][v]), dis[v])

#include <iostream>
#include <cstdio>
#include <queue>
#include <memory.h>
using namespace std;
const int maxn = 100001;
struct edge{
	int v, w,next;
}es[maxn * 10];
struct state{
	int v, i;
	state(int ii = 0, int vv = 0):v(vv), i(ii){}
	bool operator<(const state & rhs)const{
		return v < rhs.v;
	}
};
int head[maxn], dis[maxn],n ,m;
bool vis[maxn];
int mmin(int a, int b){
	if(a == -1) return b;
	return a < b ? a : b;
}
int dijkstra(int s, int e){
	memset(dis, -1, sizeof(dis));
	priority_queue<state> q;
	q.push(state(s, -1));
	while (q.size()){
		state tmp = q.top();q.pop();
		int u = tmp.i;
		if(u == e)return dis[u];
		for (int ne = head[u]; ne != -1; ne = es[ne].next){
			int v = es[ne].v, w = es[ne].w;
			if(mmin(tmp.v, w) > dis[v]){
				dis[v] = mmin(tmp.v, w);
				q.push(state(v, dis[v]));
			}
		}

	}
	return 0;
}
void addEdge(int u, int v, int w,int eidx){
	es[eidx].v = v;
	es[eidx].w =w;
	es[eidx].next = head[u];
	head[u] = eidx;
}
int main(){
	while (scanf("%d %d", &n, &m) == 2){
		memset(head, -1, sizeof(head));
		int eidx = 0;
		for (int i =0; i < m; ++i){
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			addEdge(u, v, w, eidx++);
			addEdge(v, u, w, eidx++);
		}
		int s, e;
		scanf("%d %d", &s, &e);
		printf("%d\n",dijkstra(s, e));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值