Description
给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价。起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权
Solution
直接在中转站枚举下一条边是 O ( n 2 ) O(n^2) O(n2)的,我们对于每一个中转站,将边从小到大排序,将排名相邻的边小的向大的连上它们的差,大的向小的连0即可。
Code
/************************************************
* Au: Hany01
* Prob: illness
* Email: hany01dxx@gmail.com & hany01@foxmail.com
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<LL, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 4e5 + 5, maxm = 1e7 + 5;
int n, m, e, beg[maxn], nex[maxm], v[maxm], te, w[maxm], ty[maxn];
vector<int> G[maxn];
struct Edge { int u, v, w; } E[maxm];
inline void add(int uu, int vv, int ww) { v[++ e] = vv, w[e] = ww, nex[e] = beg[uu], beg[uu] = e; }
inline bool cmp(int x, int y) { return E[x].w < E[y].w; }
inline void Dij() {
static priority_queue<PII, vector<PII>, greater<PII> > q;
static LL dis[maxn];
static int vis[maxn];
Set(dis, 127);
rep(i, SZ(G[1]))
if (E[G[1][i]].v > 1) dis[G[1][i] << 1 | 1] = E[G[1][i]].w * 2, q.push(mp(E[G[1][i]].w * 2, G[1][i] << 1 | 1));
else dis[G[1][i] << 1] = E[G[1][i]].w * 2, q.push(mp(E[G[1][i]].w * 2, G[1][i] << 1));
while (!q.empty()) {
register int u = q.top().y; q.pop();
if (vis[u]) continue; else vis[u] = 1;
for (register int i = beg[u]; i; i = nex[i])
if (chkmin(dis[v[i]], dis[u] + w[i])) q.push(mp(dis[v[i]], v[i]));
}
LL Ans = 1e18;
rep(i, SZ(G[n])) {
if (E[G[n][i]].v == n) chkmin(Ans, dis[G[n][i] << 1 | 1]);
else chkmin(Ans, dis[G[n][i] << 1]);
}
printf("%lld\n", Ans);
}
int main()
{
#ifdef hany01
freopen("illness.in", "r", stdin);
freopen("illness.out", "w", stdout);
#endif
n = read(), m = read();
For(i, 1, m)
E[i].u = read(), E[i].v = read(), E[i].w = read(), G[E[i].u].pb(i), G[E[i].v].pb(i),
add(i << 1, i << 1 | 1, E[i].w), add(i << 1 | 1, i << 1, E[i].w);
For(i, 1, n) {
sort(ALL(G[i]), cmp);
rep(j, SZ(G[i])) if (E[G[i][j]].u != i)
ty[j] = 1; else ty[j] = 0;
rep(j, SZ(G[i]) - 1)
add(G[i][j] << 1 | ty[j], G[i][j + 1] << 1 | ty[j + 1], E[G[i][j + 1]].w - E[G[i][j]].w),
add(G[i][j + 1] << 1 | ty[j + 1], G[i][j] << 1 | ty[j], 0);
}
Dij();
return 0;
}