The following:
Given a non-direction graph, the maximum value of the minimum value from the path of 1 to n is obtained.
That is, there may be more than one path from 1 to n, and each path has an edge with the smallest weight, asking the maximum value of those edges.
idea:
The previously SPFA dist Array records the distance, and now only changes to the minimum value from the source point to I.
The original SPFA extension node:
for (int i = 0; i < edges[cur].size (); i++) {
Edge e = edges[cur][i];
if (<strong>dist[cur] + e.v < dist[e.to]</strong>) {
dist[e.to] = dist[cur] + e.v
if (!vis[e.to)) {
vis[e.to] = 1;
Q.push (e.to);}}
Now just change it to:
for (int i = 0; i < edges[cur].size (); i++) {
Edge e = edges[cur][i];
if (<strong>min (Dist[cur], e.v) > dist[e.to]</strong>) {
dist[e.to] = min (dist[cur), e.v);
if (!vis[e.to]) {
vis[e.to] = 1;
Q.push (e.to);}}
Can.
Code (1836K,282MS):
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <
Vector> #include <queue> using namespace std;
struct edge{int to, V;
Edge (int A, int b): to (a), V (b) {}};
int T;
int n, m; Vector<edge> edges[1005];
adjacency table int vis[1005];
int dist[1005];
int SPFA () {queue<int> q;
memset (Vis, 0, sizeof (VIS));
memset (Dist, 0, sizeof (Dist));
Q.push (1);
VIS[1] = 1;
DIST[1] = 0x3f3f3f3f;
while (!q.empty ()) {int cur = q.front ();
Q.pop ();
for (int i = 0; i < edges[cur].size (); i++) {Edge e = edges[cur][i];
if (min (dist[cur], e.v) > Dist[e.to]) {dist[e.to] = min (dist[cur), e.v);
if (!vis[e.to]) {vis[e.to] = 1;
Q.push (e.to);
}} Vis[cur] = 0;
return dist[n];
int main () {scanf ("%d", &t);
int cas = 1;
while (t--) {scanf ("%d%d", &n, &m);
for (int i = 0; I <= N; i++) edges[i].clear ();
int A, b, C; for (int i = 0; i < m; I+ +) {scanf ("%d%d%d", &a, &b, &c);
Edges[a].push_back (Edge (b, c));
Edges[b].push_back (Edge (A, c));
int ans = SPFA ();
printf ("Scenario #%d:\n%d\n\n", cas++, ans);
return 0;
}