就是简单求以下这个式子,所以直接按照公式枚举就好了。
maxe∈path(sp(V,E))sp(V,E/{e})
#include <iostream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <deque>
using namespace std;
#define edge(i, u) for (int i = head[u]; i != -1; i = graph[i].next)
#define range(i, a, b, step) for (int i = a; i != (b) && ((i - (b)) ^ (step)) < 0 ; i += step)
const int maxv = 1002;
const int maxe = maxv * maxv;
struct node {
int v, w, next;
} graph[maxe];
int graph_cnt, head[maxv];
void graph_clear(void) {
graph_cnt = 0;
memset(head, -1, sizeof head);
}
void graph_add(int u, int v, int w) {
graph[graph_cnt].v = v;
graph[graph_cnt].w = w;
graph[graph_cnt].next = head[u];
head[u] = graph_cnt++;
}
int edge[maxv], vert[maxv];
struct _spfa {
int dis[maxv];
bool inq[maxv];
deque<int> queue;
static const int inf = 0x7f7f7f7f;
void clear(void) {
queue.clear();
memset(dis, inf, sizeof dis);
memset(inq, false, sizeof inq);
}
void update(int u, int v, int w, int e, bool flag) {
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
if (flag) {
edge[v] = e;
vert[v] = u;
}
if (inq[v] == false) {
inq[v] = true;
queue.push_back(v);
}
}
}
int operator () (int src, int des, int avoid) {
clear();
update(-1, src, 0, -1, avoid == -1);
while (!queue.empty()) {
int u = queue.front(); queue.pop_front(); inq[u] = false;
edge(i, u) if (i != avoid && i != (avoid ^ 1))
update(u, graph[i].v, graph[i].w, i, avoid == -1);
}
return dis[des];
}
} spfa;
int n, m, u, v, w;
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("aaa.txt", "r", stdin);
#endif
while (scanf("%d %d", &n, &m) != EOF) {
graph_clear();
range(i, 0, m, 1) {
scanf("%d %d %d", &u, &v, &w);
graph_add(u, v, w);
graph_add(v, u, w);
}
int ans = spfa(1, n, -1);
for (int now = n; now != 1; now = vert[now]) {
ans = max(ans, spfa(1, n, edge[now]));
}
printf("%d\n", ans);
}
return 0;
}