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;
}